Making projects is boring, let's start composing them. – Die drush make die – The PHP reinassance



Making projects is boring, let's start composing them. – Die drush make die – The PHP reinassance

0 0


talk_composer


On Github rodrigoaguilera / talk_composer

Making projects is boring, let's start composing them.

Die drush make die

A talk by Rodrigo Aguilera / @marinero

Because I knew there was something better.

Hello There

I'm from Madrid, now I live in Barcelona. I'm 29. I work as a drupal developer at Ymbra, Citilab. I have a curiosity to improve process. Previously on...

Agenda

  • The PHP reinassance
  • Drush make is great
  • The Drupal island
  • Composer is awesome
  • Your drupal project has dependencies too

Is a new tool, it has a history, is used by drupal core.

The PHP reinassance

Since 2014 we have seen PHP become a much more professional and clean language.

We have too much code

Takes less time to write a solution yourself than to find a component you can reuse.

Is not such a bad idea but it wasn't getting PHP to be any good.

Reusability

Sharing must be easy or it won't happen.

Free software as a feature.

Free software as a practical tool comes into play because you see all this different projects sharing code.

First attemps, aka the way there

  • Pear (PHP 4, required root, lots of includes)
  • Class autoloading (PHP 5.0)
  • Github (As replacement of Sourceforge)
  • Namespaces (PHP 5.3)
  • PHP standars (PSR-x)

Autoloading: how to search files with classes.

Drush make (2010)

It wasn't part of drush and then it got merged into it. answers the problem about not having drupal and modules in repo.

The shopping list

api = 2
core = 7.*
projects[views] = 3.1
projects[ctools] = 1.0-rc1
projects[media] = 2.x-dev

projects[nodequeue][subdir] = contrib
projects[nodequeue][version] = 2.0-alpha1
projects[nodequeue][patch][] = "http://drupal.org/files/issues/1023606-qid-to-name-6.patch"
projects[nodequeue][patch][] = "http://drupal.org/files/issues/nodequeue_d7_autocomplete-872444-6.patch"

libraries[jquery.cycle][download][type] = get
libraries[jquery.cycle][download][url] = http://malsup.com/jquery/cycle/release/jquery.cycle.zip?v2.99
libraries[jquery.cycle][destination] = libraries
          

Brings automation Allows patches, fixing version, external libraries, choice of folder, not all the code in the repository, can get from cvs. This in INI format, it also supports YAML.

Problems

  • Downloads all the code everytime
  • All dependencies listed
  • You have to be specific about the versions
  • Only remote patches
  • Different make files for different purposes

Some projects include a make file. One file for Drupal core, other for modules, libraries and themes, dev modules.

The drupal island

PHP is going to parties and drupal is not invited.

Some brave sailors

Some brave sailors

Drupal makes some friends out there

Just by looking at the composer.json in drupal core

  • A lot of symfony components(dependency injection, events, routing, yaml, etc)
  • Twig
  • Guzzle
  • Composer
  • And much more

Introduces symfony principles.

Introducing Composer - 2012

Ugly logo.

The need

  • 3rd party libraries.
  • Dependency hell.
  • Other PHP projects didn't had drush make.

It was decided to use components from other projects.

Definition

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

When we say libraries it can be other pieces of php code like themes or modules.

What it means

  • No more Copy/Paste of libraries
  • Resolves dependencies
  • Each project defines its own requirements
  • Performs build tasks
  • Autoloading: all code under the same roof:

  • Deps: You don't declare everything that is on your project. Only what you depend on.
  • Build: Downloading of js libraries, binaries, cleaning, scaffolding, tests

Similar to composer in diferent programming languages

  • npm (javascript)
  • pip (python)
  • rubygems (with bundler)

  • Some are package managers and some are dependency managers or both

Get Composer

# Quick-n-easy:
$ curl -sS https://getcomposer.org/installer | php

# Global
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
            
This installs a binary.

Packagist.org

Main repository for packages. There is also https://packagist.drupal-composer.org/ as a mirror of the modules and themes on drupal.org

It had an exponential grow. Declares what is stable It manages outdated packages.

Require

            
composer require <vendor>/<package>
            
            

It will write your json for you.

composer.json

Base manifest file for your project

{
    "name": "rodrigoaguilera/mydrupalsite",
    "description": "This site is awesome.",
    "require": {
        "drupal/honeypot": "8.1.*"
    },
    "require-dev": {
        "drupal/devel": "8.1.*"
    }
}
            
Is another shopping list but for higher level stuff. Commerce as an example that uses it extensibly.

Composer.json vs composer.lock

Dependencies solved. Is the key for everyone to have the same versions. Always commit your lock file.

Everything downloaded with an exact version number. Lock is a bigger file.

composer install

Reads composer.lock and downloads all your dependencies in the /vendor directory.

Everything gets a cache. Needed to develop drupal 8.1.x

composer update

Writes a new composer.lock based on what is on composer.json and downloads what is needed You can update only one package to do more atomic updates.

For last release crazies.

Execute binaries

            
vendor/bin/drush
            
            

            
composer exec drush
            
            

Examples: drush, phpunit, phpcs, behat... Is a problem not easy to solve for newbies. Drush solves it with drush launcher.

Global vs local

The power of comes from having per project dependencies. but you can also install composer packages shared by everything and everyone. Use "composer global" before commands. (neeeds $COMPOSER_HOME)

Computer vs. only my repo. Same for config.

Simulating enviroments

            
"config": {
    "platform": {
       "php": "5.6.2",
       "ext-mongodb": "1.1"
    }
}
            
            

Follow restrictions.

Semantic versioning

All packagist supports this. A new feature of drupal 8. Contrib doesn't support it.

Composer in production

            
composer install --prefer-dist --no-dev --optimize-autoloader
            
            

Component vs project

The composer.json for a project has more capabalities like:

  • Use your own repos
  • Define hook scripts

aka reusable library Don't have to wait for new releases.

Patches

              
{
  "require": {
    "cweagans/composer-patches": "~1.0",
    "drupal/drupal": "8.0.*@dev"
  },
  "config": {
    "preferred-install": "source"
  },
  "extra": {
    "patches": {
      "drupal/drupal": {
        "Add startup configuration for PHP server": "https://www.drupal.org/files/issues/add_a_startup-1543858-30.patch"
      }
    }
  }
}
              
            

Install or update

  • Make sure you have the last updates from other developers.
  • Deploying a new release of your application to production.
  • Checked out a new project and want to start coding.
  • Grab new versions for the dependencies of your project.

But... How do I use composer in my drupal project?

Composer template for drupal projects comes to the rescue. https://github.com/drupal-composer/drupal-project

Is conceived as a replacement for drush make. People from pantheon, acquia are contributing.

What does the template do?

  • Drupal will be installed with correct permissions ready for install.
  • Modules, themes, and profiles (packages of type drupal-[module|theme|profile]) will be placed in web/[module|theme|profile]/contrib/
  • Creates default writable versions of settings.php and services.yml.
  • Latest version of drush is installed locally for use at vendor/bin/drush.
  • Latest version of DrupalConsole is installed locally for use at vendor/bin/drupal.

  • Autoloader is implemented to use the generated composer autoloader instead of Drupal.
  • separates scaffold files from the real drupal core
  • Has a skeleton to put your drush aliases

Profit :D

  • Now everyone can have not only the same modules but also drush and console
  • Updating all your modules to the stable versions is just a couple of words away
  • Your continous integration system can become much more simple

Questions

THE END

Thank you. - https://getcomposer.org/

Making projects is boring, let's start composing them. Die drush make die A talk by Rodrigo Aguilera / @marinero Because I knew there was something better.