What our template project should contain?
- src directory, which will contain the source files of our classes
- tests directory where we will place our tests
- vendor directory where the composer places the loaded dependencies
- composer.json – a file in the configuration of our dependencies and autoload
- composer.lock – an auxiliary file that specifies the specific versions of our dependencies.
There are several ways to create a template project: quick and slow.
Quick way – composer create-project
Using the composer create-project command, you can quickly create a new project for the kata. This method is made simple and efficient by a composer package that contains all the necessary settings.
#create a new project in "kata" folder
composer create-project php-kata-master/kata
#if you create a project in diiferent folder "kata-execise"
composer create-project php-kata-master/kata kata-execise
Slow – create a project yourself
This method allows you to find your way and consider it as a mini kata for creating a new PHP project.
Lets create a folder where we will practice our kata:
mkdir kata
cd kata
Initialize our project with the composer and answer interactive questions. This command will guide you through creating your composer.json config:
composer init
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [gituset/kata]: php-kata-master/kata
Description []: The kata template project
Author [User Name <user.name@test.com>, n to skip]:
Minimum Stability []:dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: MIT
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]?
Search for a package: phpunit/phpunit
Enter the version constraint to require (or leave blank to use the latest version):
Using version ^11.3 for phpunit/phpunit
Search for a package:
Add PSR-4 autoload mapping? Maps namespace "PhpKataMaster\\Kata" to the entered relative path. [src/, n to skip]:
{
"name": "php-kata-master/kata",
"description": "The kata template project",
"type": "project",
"require-dev": {
"phpunit/phpunit": "^11.3"
},
"license": "MIT",
"autoload": {
"psr-4": {
"PhpKataMaster\\\\Kata\\\\": "src/"
}
},
"authors": [
{
"name": "Viktor Kalmuk",
"email": "victor.kalmuk@gmail.com"
}
],
"require": {}
}
Do you confirm generation [yes]?
Would you like to install dependencies now [yes]? yes
Loading composer repositories with package information
Updating dependencies
Lock file operations: 26 installs, 0 updates, 0 removals
........ a lot of text lines
2 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
24 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found.
PSR-4 autoloading configured. Use "namespace PhpKataMaster\\Kata;" in src/
Include the Composer autoloader with: require 'vendor/autoload.php';
As a result, we will have:
- composer.json file
- composer.lock file
- “src” folder – where our classes will be located
- “vendor” folder – contains the dependencies (PHPUnit) that the composer installed
The next step is to create a “tests” folder:
mkdir tests
Then, we need to edit the composer.json file so that we have a short and convenient namespace:
"autoload": {
"psr-4": {
"Kata\\": "src/"
}
}
Run the command to update our autoloader files:
composer dumpautoloader
Happy coding!