Skip to content

Composer

samuelgfeller edited this page Apr 24, 2024 · 12 revisions

Introduction

External dependencies are an essential part and a great help for software development.

The list and reasoning behind the choice of the framework and libraries that were chosen for the slim-example-project can be found in the Libraries and Framework page.

Composer is a tool that manages dependencies for PHP. It allows declaring the libraries the project depends on, and will install and update them in the vendor directory.

Installation

Composer can be downloaded from the official website: getcomposer.org/download

To install composer globally on Windows, I recommend downloading and executing Composer-Setup.exe.

On Mac, Brew on the command line can be used: brew install composer.

Configuration

Composer is configured in the composer.json file in the root directory of the project.

This file contains all the configuration below as well as metadata about the project such as the project name, description, and license information.

Adding dependencies

Dependencies can be added in the composer.json file in the "require" section.

Alternatively, the require command can be used to add the dependency to the composer.json file and install it automatically.

For example: composer require monolog/monolog

Dev-dependencies

Dependencies that are only needed for development and not in production can be added in the "require-dev" section or installed via the command line.
Example: composer require phpunit/phpunit --dev

Version of the dependencies

In the composer.json file, dependencies are added with a version following the semantic versioning scheme. This scheme uses a three-part version number like MAJOR.MINOR.PATCH.

  • MAJOR version increments indicate incompatible changes in the code.
  • MINOR version increments indicate the addition of new features in a backwards-compatible manner.
  • PATCH version increments indicate backwards-compatible bug fixes.

In the composer.json file, versions of dependencies can be specified in several ways:

  • Exact version: For example, "monolog/monolog": "1.0.0" installs version 1.0.0 of the monolog/monolog package.

  • Range: For example, "monolog/monolog": ">=1.0.0 <2.0.0" installs any version of monolog/monolog that is greater than or equal to 1.0.0 and less than 2.0.0.

  • Wildcard: The * wildcard can be used to specify any version where the other numbers are fixed. For example, "monolog/monolog": "1.0.*" installs any version of monolog/monolog that starts with 1.0.

  • Tilde operator: The ~ operator allows the last digit specified to go up. For example, "monolog/monolog": "~1.0" installs any version of monolog/monolog that is greater than or equal to 1.0 and less than 2.0.

  • Caret operator: The ^ operator allows all digits to go up, but it will not allow a change to the left-most non-zero digit. For example, "monolog/monolog": "^1.0.0" installs any version of monolog/monolog that is greater than or equal to 1.0.0 and less than 2.0.0.

Install / update dependencies

To install or update the dependencies listed in the composer.json file, run the following in the command line:

composer update

The versions with the scheme above will be used to determine to which versions the dependencies will be updated to.

Autoload

Composer's autoload is a mechanism that automatically loads PHP classes (dependencies from the vendor folder and the project's own classes) without the need for manual include or require statements. It follows the PSR-4 and PSR-0 standards for autoloaded classes.
In the composer.json file, the "autoload" section specifies the rules for autoloading.

The key "psr-4" specifies the namespace prefix and the directory where the classes are located.

The key "files" specifies the files that should be loaded for each request.

In this example, the namespace prefix App is mapped to the src directory and the file functions.php is always loaded and its functions available globally.

"autoload": {
  "psr-4": {
    "App\\": "src/"
  },
  "files": [
    "config/functions.php"
  ]
},

To access the file src/Example/ExampleClass.php in the project, the following namespace should be declared at the top of the file: App\Example and it can be referenced to in other classes with App\Example\ExampleClass.

The autoloader is included in the bootstrapping process with require 'vendor/autoload.php'; which loads it when the app starts up.

autoload-dev

The "autoload-dev" section is used to autoload classes that are only needed during development, such as test classes.

"autoload-dev": {
  "psr-4": {
    "App\\Test\\": "tests/"
  }
},

This allows using the classes in the tests directory with App\Test\ExampleClass.

Scripts

Composer allows defining custom commands in the "scripts" section of the composer.json file.
They can be used to automate tasks such as running tests, clearing cache, or any other task you want to automate in your development process.

The key is the script-name and the value is the command that should be executed.
The command can also be an array of commands that will be executed in order.
Other scrips can be referenced with @script-name.

This is an example of a set of scripts:

"scripts": {
  "stan": "phpstan analyse -c phpstan.neon --no-progress --ansi",
  "schema:generate": [
    "php bin/console.php DatabaseSqlSchemaGenerator generateMySqlSchema",
    "@add-migrations-to-git"
  ],
  "test": "php ./vendor/bin/phpunit --configuration phpunit.xml --do-not-cache-result --colors=always",
  "test:coverage": "php -d xdebug.mode=coverage -r \"require 'vendor/bin/phpunit';\" -- --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/logs/clover.xml --coverage-html build/coverage",
  "cs:check": "php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi",
  "cs:fix": "php-cs-fixer fix --config=.cs.php --ansi --verbose",
  "migration:create": "phinx create -c config/env/env.phinx.php --ansi",
  "migration:generate": [
    "phinx-migrations generate --overwrite -c config/env/env.phinx.php --ansi",
    "@schema:generate"
  ],
  "migrate": "phinx migrate -c config/env/env.phinx.php --ansi -vvv",
  "add-migrations-to-git": "git add resources/migrations/* && git add resources/schema/*",
  "seed:minimal": "php vendor/bin/phinx seed:run -c config/env/env.phinx.php -s ClientStatusSeeder -s UserRoleSeeder -s AdminUserSeeder",
  "seed": "php vendor/bin/phinx seed:run -c config/env/env.phinx.php"
}

To run a script, the following command can be used in the command line:

composer [script-name]

E.g. composer test to run the tests.

Composer scripts can also be used to respond to certain events triggered by Composer, such as post-install-cmd which is executed after the install command has been executed.

See here how to create a custom command that executes a function from a class instantiated by the DI container.

Clone this wiki locally