Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Get Composer (WebElement #26)

Get Composer (WebElement #26)

Vladimír Kriška

February 06, 2014
Tweet

More Decks by Vladimír Kriška

Other Decks in Programming

Transcript

  1. Here I am • Vladimír Kriška – common web dev

    guy – working as full stack web developer – or devop? or? it doesn't matter ... • My thoughts – at my blog: blog.ujovlado.sk – or twitter: @ujovlado
  2. Package management • Do you know Pear? Probably yes. •

    Did you ever install some packages through it? • How many? • Which ones? • MDB2, System_Daemon, XML_RPC, PHPUnit? • Installed on system, not project • Where's the list of required packages for current project? • Forget about it ...
  3. Dependency management • List of packages in your project •

    Managing dependencies from project file • Browsing installed packages directly from project • Simple installation and update • That's Composer
  4. Me and Composer • At first I was like –

    Noooo, new thing to my “well-run” development flow – I don't want nothing new, I like my “bubble” • But then – Mother of God, I can't live without it • So, don't be too conservative, try and learn new things. It's the only way how to stay in touch.
  5. Installation • Get .phar archive at https://getcomposer.org – (or) curl

    -sS https://getcomposer.org/installer | php • Make simlink to /usr/local/bin/composer or wherever sudo ln -s ~/composer.phar /usr/local/bin/composer • Nothing complicated, composer command is now available • There's also installer for Windows – Next, Yes, Next, Next, Yes, Finish
  6. Command line I. • Standard options – Help: composer -h

    – Verbose: composer -v (-vvv for more verbosity) – … • Standard exit codes – 0: OK – 1: Generic error code – 2: Dependency solving error code
  7. Command line II. • Most useful commnads: – install –

    update – search – init – self-update – create-project – ...
  8. Your first requirement • Require Yii framework 1.1.14 – Create

    composer.json file in your project root – yiisoft = vendor, yii = package, 1.1.14 = version – Run composer install – Profit!
  9. Semantic versioning • Given a version number MAJOR.MINOR.PATCH, increment the:

    – MAJOR version when you make incompatible API changes (BC breaks) – MINOR version when you add functionality in a backwards-compatible manner, and – PATCH version when you make backwards-compatible bug fixes. • For more information: http://semver.org
  10. Version Numbers I. • Standard operators (>, >=, <, <=,

    !=) – >=3.1.0, <3.2 • Match: 3.1.1, 3.1.20, 3.1.20.5 • Not match: 3.2, – >=1.3, <2 • Match: 1.4, 1.8.25, 1.9.2.16 • Not match: 2.0 • Use comma “,” to do AND • Use pipe “|” to do OR
  11. Version Numbers II. • Next significant release (~) – ~3.1.0

    (>=3.1.0, <3.2) • Match: 3.1.1, 3.1.20, 3.1.20.5 • Not match: 3.2 – ~1.3 (>=1.3, <2) • Match: 1.4, 1.8.25, 1.9.2.16 • Not match: 2.0
  12. Version Numbers III. • Wildcard (*) – 3.1.* (>=3.1.0, <3.2)

    • Match: 3.1.1, 3.1.20, 3.1.20.5 • Not match: 3.2 – 1.* (>=1.3, <2) • Match: 1.4, 1.8.25, 1.9.2.16 • Not match: 2.0
  13. Where the packages came from I. • There's package archiver:

    https://packagist.org/ – Once you register, it will scan your projects (branches and tags) • Composer's repository types: – composer (default) – vcs – pear – package (vcs, zip, tar, ...)
  14. Locking dependencies • Composer creates composer.lock file – If exists,

    will be used, if not, will be created from composer.json – Should be in your VCS • Every developer gets same packages as you
  15. Where the packages came from II. • You can install

    every VCS package • If composer.json exists
  16. Autoloading I. • Every package has own mechanism to perform

    autoloading classes • Is impossible to manage everything manually • Composer has “special” autoloader which require all other autloaders • Only one require in bootstrap file
  17. Autoloading II. • Require files defined in autoload section •

    Or classes • Or using standards (PSR-4, PSR-0)
  18. Autoloading III. • Require files defined in autoload section –

    autoload_files.php • Or classes – autoload_classmap.php
  19. Where the packages came from V. • Your own archive

    of packages – Composer will look for packages.json – http://localhost/composer/
  20. Deployment I. • It's very easy to add Composer to

    your existing deployment process • Example (new project – first deployment): • hg clone REPOSITORY_PATH webelement-REV_ID • cd webelement-REV_ID • hg update 1.0 --clean • composer install –no-dev • cd .. • ln -s ./webelement-REV_ID/webroot ./webroot
  21. Deployment II. • Example (existing project): • hg clone REPOSITORY_PATH

    webelement-REV_ID • cd webelement-REV_ID • hg update 1.0 --clean • composer update –no-dev • cd .. • rm ./webroot && ln -s ./webelement-REV_ID/webroot ./webroot • (optional) rm -rf ./webelement-OLD_REV_ID
  22. Tips • Add SHA hash to .zip, .tar packages •

    --dev is the default behavior – Will install packages in require-dev section – Use --no-dev on production • Test installation before deployment – Next run will be loaded from cache • Set {“packagist”: false} in repositories section if you don't need any public packages • Secure your own package archiver with SSH • Read composer.json on popular PHP projects