Composer

Composer #

Tool to handle PHP dependencies

Composer.json #

Contains the list of the dependencies we want to install

Composer.lock #

  • File generated and updated by composer
  • Containes the list of installed bundles and dependencies, and the version installed for each of them

Requirements #

PHP 5.3.2+

Minimum content #

The require key

{
    "require": {
        "monolog/monolog": "1.0.*"
    }
}

Release operators #

Tilde Version Range (~)

  • ~1.2 is equivalent to >=1.2 and < 2.0.0
  • ~1.2.3 is equivalent to >=1.2.3 and <1.3.0

Caret Version Range (^)

  • ^1.2.3 is equivalent to >=1.2.3 and <2.0.0

Installing dependencies #

php composer.phar install
  • If composer.lock doesn’t exist yet : It will be generated automatically and the dependencies will be installed with the last versions

  • If composer.lock exists : The versions in the composer.lock will be installed

Update to the last version #

composer update

Update only one package #

composer update mon/package [...]

List of all bundles installed in composer #

composer show

Packagist #

Platform packages #

Virtual packages for dependencies installed on the system and not installable by composer. Like PHP and its extensions, and some system libraries.

  • Display list of platform packages available in local
composer show --platform

Autoloader #

For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can simply include this file and start using the classes that those libraries provide without any extra work

You can even add your own code to the autoloader by adding an autoload field to composer.json.

{
    "autoload": {
        "psr-4": {"Acme\\": "src/"}
    }
}

Must see in details : https://getcomposer.org/doc/01-basic-usage.md#autoloading

Autoloader Production optimization #

Optimization Level 1: Class map generation #

Set “optimize-autoloader”: true inside the config key of composer.json

php composer.phar <install | update> --optimize-autoloader
php composer.phar dump-autoload --optimize

Ce que ça fait:

  • Class map generation essentially converts PSR-4/PSR-0 rules into classmap rules
  • The class map returns instantly the path
  • Composer can guarantee the class is in there so there is no filesystem check needed
  • On PHP 5.6+, the class map is also cached in opcache, then it should load almost instantly

Optimization Level 2/A: Authoritative class maps #

Set “classmap-authoritative”: true inside the config key of composer.json

php composer.phar <install | update> --classmap-authoritative
php composer.phar dump-autoload --classmap-authoritative

Ce que ça fait:

  • Enabling this automatically enables Level 1 class map optimizations.
  • It says that if something is not found in the classmap, then it does not exist and the autoloader should not attempt to look on the filesystem according to PSR-4 rules.
  • In case a class is generated at runtime for some reason, it will not be allowed to be autoloaded, then you might experience “class not found” issues in production

! This can not be combined with Level 2/B optimizations.

Optimization Level 2/B: APCu cache Set “apcu-autoloader”: true inside the config key of composer.json

php composer.phar <install | update> ----apcu-autoloader
php composer.phar dump-autoload --apcu

Ce que ça fait:

  • Adds an APCu cache as a fallback for the class map
  • It will not automatically generate the class map though, so you should still enable Level 1 optimizations manually if you so desire.
  • Whether a class is found or not, that fact is always cached in APCu so it can be returned quickly on the next request.
  • Better than Level 2/A

Satis #

Deposit generator for composer

Scripts #

  • PHP Callback ou command line
  • Executes instructions during composer execution process
  • Détails
{
    "scripts": {
        "post-update-cmd": "MyVendor\\MyClass::postUpdate",
        "post-package-install": [
            "MyVendor\\MyClass::postPackageInstall"
        ],
        "post-install-cmd": [
            "MyVendor\\MyClass::warmCache",
            "phpunit -c app/"
        ],
        "post-autoload-dump": [
            "MyVendor\\MyClass::postAutoloadDump"
        ],
        "post-create-project-cmd": [
            "php -r \"copy('config/local-example.php', 'config/local.php');\""
        ]
    }
}

Librairies #

https://getcomposer.org/doc/02-libraries.md