Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Composer Tips & Tricks

Composer Tips & Tricks

Presented at the Bulgaria PHP User Group meeting in March 2016.
https://www.facebook.com/events/1538125716485720/

Harry Dobrev

March 10, 2016
Tweet

More Decks by Harry Dobrev

Other Decks in Programming

Transcript

  1. Composer Tips & Tricks
    Making the most of using Composer

    View full-size slide

  2. What is Composer?
    PHP dependency manager

    View full-size slide

  3. Authors of Composer
    Jordi Boggiano
    @Seldaek
    Nils Adermann
    @naderman

    View full-size slide

  4. ● composer.json
    ● composer init
    ● composer install
    ● composer update
    ● composer require
    ● composer.lock
    Composer basics

    View full-size slide

  5. composer create-project
    ● Bootstraps your project from another package
    ● Installs dependencies
    ● Provide hooks to execute scripts

    View full-size slide

  6. ● composer create-project fabpot/silex-skeleton
    ● composer create-project --repository-url=https://repo.
    magento.com/ magento/project-community-edition
    ● composer create-project / foo
    composer create-project

    View full-size slide

  7. clippings/composer-init
    ● Inspired by grunt-init
    ● composer global require clippings/composer-init
    ● composer-init use clippings/package-template
    ● Learn more at github.com/clippings/composer-init

    View full-size slide

  8. composer update --with-dependencies
    composer update --with-dependencies vendor/package
    Updates only a package with its dependencies. Does not touch the
    rest of the packages.
    composer require --update-with-dependencies

    View full-size slide

  9. composer remove --update-with-dependencies
    --update-with-dependencies is now the default for
    composer remove
    composer remove

    View full-size slide

  10. composer update --prefer-lowest
    composer update --prefer-lowest --prefer-stable
    But when would you want that?
    Testing a library

    View full-size slide

  11. Travis CI Build Matrix
    php:
    - 5.3
    - 5.4
    - 5.5
    - 5.6
    - 7
    - hhvm
    env:
    - DEPS=normal
    - DEPS=highest
    - DEPS=lowest

    View full-size slide

  12. Travis CI Build Matrix
    install:
    - if [ "$DEPS" = "normal" ]; then composer install; fi;
    - if [ "$DEPS" = "highest" ]; then composer update; fi;
    - if [ "$DEPS" = "lowest" ]; then composer update --prefer-lowest --prefer-
    stable; fi;

    View full-size slide

  13. Travis CI Build Matrix

    View full-size slide

  14. config.platform
    {
    "config": {
    "platform": {
    "php": "5.6"
    }
    }
    }

    View full-size slide

  15. composer install --prefer-source
    Differences between:
    ● source
    ● dist
    ● auto
    When to use --prefer-source?

    View full-size slide

  16. config.preferred-install
    {
    "config": {
    "preferred-install": {
    "clippings/*": "source",
    "*": "auto"
    }
    }
    }

    View full-size slide

  17. composer install --prefer-dist --no-dev
    config.preferred-install

    View full-size slide

  18. .gitattributes export-ignore
    tests export-ignore
    .travis.yml export-ignore
    .gitattributes export-ignore
    .gitignore export-ignore

    View full-size slide

  19. composer test
    {
    "scripts": {
    "test": "phpunit"
    }
    }
    composer test

    View full-size slide

  20. composer run-script
    composer run-script lint
    {
    "require-dev": {
    "squizlabs/php_codesniffer": "^2.5"
    },
    "scripts": {
    "lint": "phpcs"
    }
    }

    View full-size slide

  21. composer depends
    Shows which packages depend on a package

    View full-size slide

  22. $ composer depends symfony/yaml
    phpunit/phpunit 4.8.23 requires symfony/yaml (~2.1|~3.0)
    composer depends

    View full-size slide

  23. composer prohibits

    View full-size slide

  24. composer prohibits
    $ composer prohibits symfony/yaml:2.0
    phpunit/phpunit 4.8.23 requires symfony/yaml (~2.1|~3.0)

    View full-size slide

  25. extra.branch-alias
    {
    "extra": {
    "branch-alias": {
    "dev-master": "1.0-dev"
    }
    }
    }

    View full-size slide

  26. VCS repositories
    {
    "repositories": [
    {
    "type": "vcs",
    "url": "https://github.com/clippings/php-gacx"
    }
    ]
    }

    View full-size slide

  27. extra.branch-alias
    {
    "extra": {
    "branch-alias": {
    "dev-master": "1.0-dev"
    }
    }
    }
    Read more at getcomposer.org/doc/articles/aliases.md

    View full-size slide

  28. Bonus: Facebook mention-bot

    View full-size slide

  29. Facebook mention-bot
    Learn more at github.com/facebook/mention-bot

    View full-size slide

  30. Who?
    @hkdobrev
    CTO @ Clippings.com
    clippings.github.io
    Thanks!

    View full-size slide