psr0 autoloader

Categories

Component ID

2078739

Component name

psr0 autoloader

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

6664

Component created

Component changed

Component body

This is a minimalistic, Drupal 8 compatible autoloader for PSR-4.

Features

  • Support for PSR-4 in all enabled modules and profiles.
  • Minimal module size. Ever had to find out why the hell your class is not autoloading? With psr0 you'll find out. It's ~80 lines of code, including comments.
  • Early bootstrap. Autoloading works even in hook_install() and hook_boot().
  • Also works during test-runs.

Usage

Simply add a dependency on this module if you want to use autoloading in your module. Use PSR-4 namespaces and you're good to go. No further configuration needed.

Examples for classes and paths

Drupalyour_moduleYourClass -> …/your_module/src/YourClass.php
Drupalyour_moduleSomeInterface -> …/your_module/src/SomeInterface.php
Drupalyour_moduleSubNamespaceClass -> …/your_module/src/SubNamespace/Class.php
Drupalyour_moduleTestsSomeTest -> …/your_module/src/Tests/SomeTest.php (since 1.4)
Drupalyour_moduleTestsSomeTest -> …/your_module/tests/src/SomeTest.php (since 1.4)

The same works also for profiles:

Drupalyour_profileYourClass -> profiles/your_profile/src/YourClass.php
Drupalyour_profileSomeInterface -> profiles/your_profile/src/SomeInterface.php
Drupalyour_profileSubNamespaceClass -> profiles/your_profile/src/SubNamespace/Class.php
Drupalyour_profileTestsSomeTest -> profies/your_profile/src/Tests/SomeTest.php (since 1.4)
Drupalyour_profileTestsSomeTest -> profiles/your_profile/tests/src/SomeTest.php (since 1.4)

The lib/-prefix is still supported in 7.x-1.x although src/ was chosen as prefix for PSR-4 in D8.

Related modules / alternatives

  • X Autoload: Implements advanced caching, registration of custom prefixes, …
  • Classloader is a direct backport of the D8 autoloader.

None of these modules allows reliable autoloading during hook_install() - at least without further configuration. Both have a bigger footprint though in some cases their caching mechanisms may make them perform better.

Roadmap

The module is feature complete. No additional changes are planned.