2017 © Pedro Peláez
 

composer-plugin autoload-drupal

Autoload Drupal modules into composer autoloader

image

fenetikm/autoload-drupal

Autoload Drupal modules into composer autoloader

  • Monday, April 2, 2018
  • by michaelwelford
  • Repository
  • 1 Watchers
  • 0 Stars
  • 464 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Drupal 8 Composer Autoloader

A Composer plugin to add Drupal 8 autoloading of modules to the composer autoloader., (*1)

Why?

Why would you want this? Doesn't Drupal 8 have its own autoloading mechanism?, (*2)

There are quite a few handy types of tools (e.g. static analysis, intellisense) that rely on being able to load all classes via the composer autoloader. Unfortunately, as Drupal 8 does its own autoloading at boot time, using these tools at best becomes slow (if you try to boot Drupal on the fly) or at worst becomes impossible (because you are running Drupal inside a VM). Some IDEs (e.g. PHPStorm) get around this by implementing their own discovery mechanism but if you are using an IDE or editor that doesn't do this you are out of luck., (*3)

This plugin plugs this gap., (*4)

How

This plugin is heavily based off of the Composer Merge Plugin. Essentially this plugin creates a composer.json file in memory including in all the specified modules to an autoload section and merges it into the root composer.json file, also in memory., (*5)

Installation

Require the plugin in your composer.json

Standard stuff:, (*6)

{
    "require": {
        "fenetikm/autoload-drupal": "0.2"
    }
}

Configure modules to autoload

This plugin is configured via the extra section in your composer.json. Usually you would want the app/modules/contrib/, app/core/modules/ and the app/modules/custom/ directories included. As Drupal can be configured in many ways, none of this is assumed and so all must be added in., (*7)

You can also constrain which modules are added in from a directory by specifying an array that follows the pattern of [ "directory_to_include", [ "module1", "module2" ] ]., (*8)

For example:, (*9)

    "extra": {
        "autoload-drupal": {
            "modules": [
              "app/modules/contrib/",
              "app/core/modules/",
              [
                  "app/modules/custom/", [ "my_module" ]
              ]
            ]
        }
    }

Here, all modules in app/modules/contrib/ are added in, all modules in app/core/modules/ are added in and only the my_module module is added in from the app/modules/custom/ directory., (*10)

NOTE: only modules with src directories will be added in (since they conform to PSR for class autoloading)., (*11)

Configure classmap

Additionally you may also add in classes that are outside of modules. A typical example is adding in the Drupal testing classes. e.g., (*12)

    "extra": {
        "autoload-drupal": {
            "modules": [
                "app/modules/contrib/",
                "app/core/modules/",
                "app/modules/custom/",
            ],
            "classmap": [
                "app/core/tests/Drupal/Tests"
            ]
        }
    }

This will add in all the classes in that directory such as UnitTestCase, AssertHelperTraitTest etc., (*13)

Rebuild the autoloader

The composer autoloader with the Drupal modules will get generated at install and update. To manually force the refresh of it, do:, (*14)

composer autoload-dump

Result

If you have set it up successfully you will see something like the following in your vendor/composer/autoload_psr4.php file:, (*15)

return array(
  'Drupal\\user\\' => array($baseDir . '/app/core/modules/user/src'),
  'Drupal\\my_module\\' => array($baseDir . '/app/modules/custom/my_module/src'),
  'Drupal\\rad-module\\' => array($baseDir . '/app/modules/contrib/rad-module/src'),
  'Drupal\\entity\\' => array($baseDir . '/app/core/modules/entity/src'),
  );

The Versions

02/04 2018

dev-master

9999999-dev

Autoload Drupal modules into composer autoloader

  Sources   Download

MIT

The Requires

 

by Michael Welford

02/04 2018

0.1

0.1.0.0

Autoload Drupal modules into composer autoloader

  Sources   Download

MIT

The Requires

 

by Michael Welford