Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Composer & Drupal

824 views

Published on

Talk at Drupal Meetup Stuttgart, 02/04/2016

Published in: Internet
  • ➤➤ How Long Does She Want You to Last? Here's the link to the FREE report ●●● http://ishbv.com/rockhardx/pdf
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

Composer & Drupal

  1. 1. Composer & Drupal Drupal Meetup Stuttgart 04.02.2016
  2. 2. Composer is a... ● Dependency Manager for PHP projects -> Requirements like frameworks or libraries ● Package Manager for PHP packages -> Globally installed tools, like Drush or Console http://getcomposer.org
  3. 3. Why should we care? ● Avoiding dependency hell ● Getting off the island ● Don’t reinvent the wheel ● Proudly invented / found elsewhere (PIE) ● Everyone does it ● ~85.000 packages for PHP projects using Composer (2/2016)
  4. 4. Basic components composer.json -> Specifies requirements, sources and configuration composer.lock -> Lists installed dependencies with current versions vendor folder -> Dependencies (packages) will be installed here Autoloader -> Makes dependencies available to your project
  5. 5. PHP package repository: http://packagist.org
  6. 6. What’s a package? Package => ● Script ● Library ● Framework identified by "vendor/package name", e.g. "fabpot/php-cs-fixer"
  7. 7. Example composer require andrioli/hello-composer:"@dev" php:">=5.3.0" composer.json: { "require": { "andrioli/hello-composer": "@dev", "php": ">=5.3.0" } } index.php: <?php require 'vendor/autoload.php'; print HelloWorldSay::hello(); Composer defaults to stable versions, here we require dev versions, too!
  8. 8. Requirement: Semantic versioning 3.6.18 [major].[minor].[patch] [Breaking].[Feature].[Bugfix] http://semver.org
  9. 9. Specify required versions [vendor/package]:[version][@[dev|alpha|beta|RC|stable] Composer supported version constraints: Exact: 7.1.0 -> Exact version Comparison: >=1.2 Range: 1.2 - 1.3 Wildcard: 7.1.* Tilde: ~7.1.3 -> Next significant release, >= 7.1.3, < 7.2 Carét: ^7.1.0 -> Next major release, >= 7.1.0, < 8.0.0
  10. 10. One thing to consider “just edit your composer.json and add…” [SeldJsonLintParsingException] "./composer.json" does not contain valid JSON Warning: The lock file is not up to date with the latest changes in composer.json. You may be getting outdated dependencies. Run update to update them. => Use composer commands!
  11. 11. Start a new PHP project: composer init 1. Create your index.php: touch index.php 2. Create composer.json: composer init -n 3. Add requirements/dependencies 4. Add the autoloader to your index.php: require 'vendor/autoload.php';
  12. 12. Specify requirements: composer require composer require [ vendor/package ] [ vendor/package ] ... composer require guzzlehttp/guzzle composer require phpunit/phpunit --dev Adds / updates dependencies in composer.json & composer.lock Package only needed for development? Use --dev !
  13. 13. Install requirements: composer install composer install composer install --no-dev Installs exact versions specified in composer.lock For production environments, use --no-dev !
  14. 14. Update requirements: composer update composer update composer update [ vendor/package ] [ vendor/package ] ... Installs the latest version of the specified packages meeting the requirements in composer.json Updates composer.lock accordingly
  15. 15. Remove requirements: composer remove composer remove [ vendor/package ] [ vendor/package ] ... Removes the specified package(s) Removes requirements from composer.json Updates composer.lock accordingly
  16. 16. More Composer goodies ● Installs from packagist, various vcs (git, svn, …), pear, zip and more ● Applies patches, if needed ● Supports scripts, e.g. "post-package-install" ● Supports plugins (functional extensions) Some useful plugins: composer/installers -> Scaffolding, type-dependent install folders wikimedia/composer-merge-plugin -> merge multiple composer.json files fxp/composer-asset-plugin -> manage NPM- / Bower packages (CSS/JS)
  17. 17. Using composer in Drupal projects Some pitfalls here: ● Semantic versioning: 7.x-3.5, 8.x-4.0 ? ● Different types of packages (modules, themes, libraries) ● Different target folders, not just /vendor ● Dependencies in .info files ● Incompatible project metadata / vcs drupal.org <-> packagist.org Solutions: ● Specialized package repository, packagist.drupal-composer.org ● Use project templates / skeletons!
  18. 18. Create a new Drupal project using a template composer create-project drupal-composer/drupal-project:8.x-dev myproject --stability dev --no-interaction composer create-project drupal-composer/drupal-project:7.x-dev myproject -s dev -n https://github.com/drupal-composer/drupal-project (just a starting point) This means the dev version of the template, not of Drupal!
  19. 19. It’s not Drupal, it’s best practice! composer create-project symfony/framework-standard-edition composer create-project yiisoft/yii2-app-basic composer create-project laravel/laravel … Use it for scaffolding, profiles, install scripts, configuration, ...
  20. 20. Managing Drupal Module & Themes composer require drupal/devel drupal/views composer update drupal/linkit composer remove drupal/webform … Composer takes care of dependencies, so e.g. drupal/views includes drupal/ctools (D7)!
  21. 21. What about custom modules? { "name": "drubb/myownmodule”, "description": "Just a little custom module.", "type": "drubb-custom", "homepage": "https://github.com/drubb/myownmodule/" "require": { "drupal/views": ">=7.3.1" }, } Optionally (if not using packagist), use composer-merge plugin to merge your custom composer.json with the main composer.json of your project ! Remember composer-installers?
  22. 22. But I use Drush / Console / Bash already? Great! Keep on using them! Additionally use them in your Composer scripts! "post-update-cmd": [ "drush updb", "drush cc all", "echo Updates completed!" ],
  23. 23. UI wanted ? Composer Manager Module!
  24. 24. Drush Wrapper: Composer Module! drush composer require drush composer install drush composer update … https://drupal.org/project/composer
  25. 25. GIT Workflow Development git pull composer install composer require / update git push Add composer.json / composer.lock ! Exclude dependencies (/vendor) ! Default for upcoming Drupal 8.1 ! Production git pull / checkout composer install
  26. 26. Read on Official Composer Documentation https://getcomposer.org/doc Composer the Right Way http://www.slideshare.net/rdohms/composer-the-right-way-phpjhb15 Composer Cheat Sheet for Developers http://composer.json.jolicode.com Drupal Composer community initiative http://drupal-composer.org Slides: http://www.slideshare.net/drubb

×