Composer Tips

Published on Author craigzearfossLeave a comment
  • Composer syntax:

    composer require VENDOR_NAME/PACKAGE_NAME OPTIONS
    
  • composer install pulls in all of the packages that have been specified.
  • The Composer directories
    vendor any packages pulled in through Composer
    vendor/bin any executables
  • Composer uses an autoloader so you don’t have to keep typing require.

Install globally whenever possible.

Install packages the right way.

  • Example:
    composer require somepackage/somepackage:someversion
    
  • To add a require dev example.
    composer require phpunit/phpunit --dev
    

Version flags

  • exact matches (1.2.3)
  • ranges with operators (<1.2.3)
  • combinations of operators (>1.2.3 <1.3)
  • best available (1.2.*)
  • tilde (~1.2.3) highest known stable minor version; up to but not included 1.3
  • carat (^1.2.3) only be careful of breaking changes; up to but not including 2.0

composer.lock should almost always be committed to version control.

Configuration and global configuration

  • to force Composer to optimize the classmap after every update/install:
    {
        "config": {
            "optimize-autoloader": true
        }
    }
    
  • use cache-files-maxsize for big projects; can be set globally; either edit ~/.composer/config.json or from the command line enter composer config --global cache-files-maxsize "2048MiB"

Profile and Verbose

  • add --profile and --verbose to composer command for more info

To install from a Github repo when your project isn’t yet on Packagist see Quick Tip: Composer & Github – Develop Packages Interactively

Speeding up Composer

  • to run it on HHVM see Use HHVM to speed up Composer – Mark van Eijik
  • use --prefer-dist to download from a stable, packaged version of a project rather than cloning it from the version control system it’s on (this is the default); user --prefer-source to download the source.

Use .gitattributes file to ignore some files and folders during packaging for --prefer-dist mode; example:

/docs               export-ignore
/tests              export-ignore
/.gitattributes     export-ignore
/.gitignore         export-ignore
/.travis.yml        export-ignore
/phpunit.xml        export-ignore

Use composer show --platform (short -p) or composer show -installed (short -i) to see the version of PHP or it’s extensions or a list of all the projects (and their descriptions).

Use the --dry-run flag to see if an installation of new requirements would go well.

Create Project

  • Use create project command to clone a package and executes composer install inside it; example:
composer create-project laravel/laravel --prefer-dist --profile --verbose

First parameter is path to install (project’s name is used if omitted)

Second parameter is the version.

Use scripts attribute to specify commands to be run before/after a command; example:

"scripts": {
    "post-install-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "php artisan clear-compiled",
        "php artisan optimize"
    ],
    "post-create-project-cmd": [
        "php -r \"copy('.env.example', '.env');\"",
        "php artisan key:generate"
    
    ]
}

https://packagist.org is the main Composer repository.

References:

Leave a Reply

Your email address will not be published. Required fields are marked *