Using Composer with Drupal and Drush

  • Published on
    18-Jul-2015

  • View
    654

  • Download
    0

Transcript

<ul><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Using Composerwith Drupal and Drush</p><p>Stanford DrupalCamp 10 April 2015</p><p>Greg Anderson</p><p>+ + =</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>What Is Composer?</p><p>An Installer A Dependency Manager An Autoloader</p><p>{ "require": { "php": "&gt;=5.4.0", "symfony/browser-kit": "~2.1", "symfony/css-selector": "~2.1", "symfony/dom-crawler": "~2.1", "guzzlehttp/guzzle": "&gt;=4,</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Why Use Composer?</p><p>3</p><p>1. Standard</p><p>2. Easiest for developersa. Dependency resolutionb. Code updatesc. Autoloading of classes</p><p>3. Composer is being adopted everywhere</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Using Composer</p><p>Not Using Composer</p><p>What Projects Are Using Composer?</p><p>4</p><p> and many others!</p><p>http/guzzle</p><p>fabpot/goutteDrupal Modules Drush Extensions</p><p>PHP APIs symfony/yaml</p><p>twig/twig</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Installing Drupal 7 with Composer</p><p>Can we do this?</p><p>?+ =</p><p>5</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Composer Parts of the Whole</p><p>Composer Packagist composer.json Custom Installer</p><p>PHP dependency management </p><p>software. </p><p>A software repository manager.</p><p>A structured file that defines a </p><p>project.</p><p>An executable Composer </p><p>component. Optional.</p><p>6</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Parts Needed for Composer + D7</p><p>packagist.drupal-composer.org davidbarratt/custom-installer derhasi/composer-preserve-paths</p><p>A third-party repository containing data on projects from </p><p>drupal.org.</p><p>Allows composer.json files to specify where </p><p>components get installed.</p><p>Allows nested installation of </p><p>composer libraries(e.g. Drupal modules inside of Drupal core).</p><p>7</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Semantic Versioning</p><p>Drupal Module Version Composer Version</p><p>8</p><p>7.x-1.5DRUPAL-x.MODULE.MINOR</p><p>7.1.5MAJOR.MINOR.PATCH</p><p> Versions are converted to semantic versioning before being published on packagist.drupal-composer.org.</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Comparison with Drush Make; Drush make file that uses guzzle; APIapi = 2</p><p>; Corecore = 7.x</p><p>; Drupal project.projects[drupal][type] = coreprojects[drupal][version] = 7.xprojects[drupal][download][type] = gitprojects[drupal][download][branch] = 7.x</p><p>; Modulesprojects[] = composer_managerprojects[] = aws_glacierprojects[] = devel</p><p>{ "name": "organization/project", "description": "Example Drupal composer.json file", "repositories": [ { "type": "composer", "url": "http://packagist.drupal-composer.org/ " } ], "require": { "davidbarratt/custom-installer": "dev-master", "derhasi/composer-preserve-paths": "0.1.*", "drupal/drupal": "7.*", "drupal/composer_vendor": "7.1.*", "http/guzzle": "~5", "drupal/devel": "7.1.*", }, "config": { "vendor-dir": "htdocs/sites/all/vendor" }, "extra": { }, "minimum-stability": "dev", "prefer-stable": true}</p><p>Drush Make composer.json</p><p>9</p><p>Repository and custom installers, as previously explained.</p><p>Drupal module that provides an autoload strategy (next).</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Autoloading in PHP</p><p>10</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Adding Composer Support</p><p>To make this work, we just need to include vendor/autoload.php</p><p>+ =</p><p>11</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Composer Modules for Drupal 7Composer Modules for Drupal 7Composer Modules for Drupal 7composer_autoload composer_manager composer_vendor</p><p>Searches for autoload.php files in every module directory and loads them individually.</p><p>Searches for composer.json files in every module and dynamically merges them.</p><p>Loads the autoload.php file in sites/all/vendor.</p><p>12</p><p>Fragile ComplexWins!</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Directory Structure with D7 + Composer mydrupalsite.org</p><p> composer.json composer.lock htdocs</p><p> sites default</p><p> settings.php files</p><p> all modules vendor</p><p>13</p><p>We create a new top-level directoryfor our project, because composercannot manage dependencies inthe same directory as composer.json.</p><p>We put the vendor directory in sites/all/vendor because that is where the composer_vendor project expects to find it.</p><p>We will put our Drupal root directory in htdocs.</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>How Do We Place the Vendor Directory?</p><p>composer.json</p><p>14</p><p>{ "require": { }, "config": { "vendor-dir": "htdocs/sites/all/vendor" }, }</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>How Do We Place Modules and Themes?</p><p>15</p><p>{ "require": { "davidbarratt/custom-installer": "dev-master", }, "extra": { "custom-installer": { "drupal-module": "htdocs/sites/all/modules/contrib/{$name}/", "drupal-theme": "htdocs/sites/all/themes/contrib/{$name}/" }, }, }</p><p>composer.json</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Can We Use Installation Profiles?</p><p>composer.json</p><p>16</p><p>{ "require": { "davidbarratt/custom-installer ": "dev-master", "drupal/panopoly": "7.1.*", }, "extra": { "custom-installer": { "drupal-profile": " htdocs/profiles/{$name}/ " }, }, }</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>How Do We Download Modules?$ drush dl develProject devel (7.x-1.5) downloaded to sites/all/modules/contrib/devel.Project devel contains 3 modules: devel_generate, devel, devel_node_access.</p><p>$ composer require drupal/devel '7.*'./composer.json has been updatedLoading composer repositories with package informationUpdating dependencies (including require-dev)</p><p>Drush Composer</p><p>17</p><p> Drush will select the right module major version, but composer require must be told which version to use.</p><p> Composer require will update the composer.json file before installing the module.</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Install a Module from a Private Repository</p><p>18</p><p>{ "repositories": [ { "type": "vcs", "url": "https://github.com/your-org/your-module" } ], "require": { "your-org/your-module": "dev-master" }, } https://knpuniversity.com/screencast/question-answer-day/create-composer-package</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Add a Patch to a Module</p><p>19</p><p>{ "require": { "netresearch/composer-patches-plugin": "~1.0" }, "extra": { "patches": { "drupal/features": { "7.2.2":[ { "title": "Remove mtime from .info export (added by Drupal 7.33)", "url": "https://www.drupal.org/files/issues/2381739-features-mtime.patch" } ] } } }, }</p><p>http://cambrico.net/drupal/using-composer-to-build-your-drupal-7-projects</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>How Do We Update a Site?</p><p>$ drush pm-update $ composer update$ drush updatedb</p><p>Drush Composer</p><p>20</p><p>Remember - regardless of how you update your site, always do it on a copy first. Never update directly on the </p><p>production site!</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>How Do We Manage Project Code? mydrupalsite.org</p><p> .git composer.json composer.lock .gitignore htdocs</p><p> sites all</p><p> modules custom contrib</p><p> vendor</p><p>21</p><p>Commit composer.json. and composer.lock to the repository. composer.lock only changes when you run composer update.</p><p>Avoid committing composer-managed directories, such as sites/all/modules/contrib and sites/all/vendor.</p><p>If you have custom modules, you can commit them in the custom directory.</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>How Do We Deploy Code?</p><p>22</p><p>rsyncinstallclone</p><p>1 2 3</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Idea - Using Two Repositories</p><p>23</p><p>clone</p><p>installclone</p><p>commit</p><p>rsync pull</p><p>2 3</p><p>1</p><p>4</p><p>5</p><p>6</p><p>Deploy with git while maintaining a lean working repository by writing a short deploy script.</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Converting an Existing Site</p><p>$ drush dl composer_generate</p><p>$ drush @site composer-generate &gt; composer.json</p><p>$ composer install</p><p># Set up settings.php, copy files </p><p>$ drush site-install</p><p>24</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Installing Drupal 8 with Composer</p><p>Drupal 8 already uses Composer, so this should be easier, right?</p><p>?+ =</p><p>25</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>How it is Supposed to Work</p><p>26</p><p>Why Doesnt this work? Its complicated.</p><p>$ composer create-project drupal/drupal path ~8</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Template composer.json File for Drupal 8</p><p>Utilize a custom installer to set install locations</p><p>27</p><p>{ "require": { "composer/installers": "^1.0.20", "drupal/core": "8.0.x-dev", "drush/drush": "~7", }, "extra": { "installer-paths": { "web/core": ["type:drupal-core"], "web/modules/contrib/{$name}": ["type:drupal-module"] }, }, }</p><p>drupal/core contains only the contents of the Drupal 8 core directory. This is called a split core.</p><p>https://github.com/webflo/drupal-site-template</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Setting Up Drupal 8 for Composer mydrupalsite.org</p><p> composer.json vendor htdocs</p><p> sites modules themes autoload.php index.php .htaccess</p><p>28</p><p>We place the vendor directory in its standard location in the project directory, rather than in the core directory where Drupal 8 expects it.</p><p>Copy all of the other regular files from the Drupal 8 root directory.</p><p>We need to modify Drupals autoload.php file to include the autoloader in our vendor directory.</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Modified autoload.phpautoload.php in our modified site</p><p>autoload.php in Drupal 8 root directory</p><p>29</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>What Does Hacking Core Mean?</p><p>Every time you hack core by Greg Dunlap (heyrocker) DrupalCon Szeged 2008</p><p>If you have to resolve merge conflicts when you update, then you are hacking core.</p><p>30</p></li><li><p>Using Composer with Drupal and DrushUsing Composer with Drupal and Drush</p><p>Where Do We Go From Here?</p><p>31</p><p>https://groups.drupal.org/composer</p><p>http://drupal-composer.org/</p><p>https://github.com/drupal-composer</p><p>https://getcomposer.org/</p><p>@greg_1_anderson</p></li></ul>