Skip to content

WIP: autoload.php template generator

Robin Gustafsson requested to merge wip/phpabtpl into master

Hi,

This is a suggestion/first draft of a tool to automate the creation of autoload.php templates for the phpab tool.

It's a generalized version of the ad-hoc autoload template generator I wrote and use for the php-laravel-framework package, following the brief discussion in #976799 about incorporating it here.

Background

The PHP PEAR team customarily uses phpab to generate a class autoloader for each package. While phpab will load the package's classes, it will not load its dependencies nor any statically loaded (non-PSR-4) files. To load these, a custom template must be used where these files are explicitly loaded. These templates are often trivial but must be maintained manually.

Solution

The new script "phpabtpl" (as in "phpab template") generates such a template from a composer.json file. The template contains the additions needed to load the dependencies (both required and suggested dependencies) as well as the package's own statically loaded files.

Details

The tool reads the following parts of composer.json:

  • require: Autoloaders for these packages will be loaded with require_once.
  • suggest: Autoloaders for these packages will be loaded with include_once, if they are present on the system.
  • autoload files: These files will be loaded with require_once.

Each autoloader path is either read from a list of known autoloader paths or guessed based on the Composer package name.

By default, the autoloader path is guessed based on the package name. For example, vendor/package1 gives Vendor/Package1/autoload.php.

Known autoloaders are read from (in order of precedence):

  1. debian/pkg-php-tools-autoloaders
  2. /usr/share/pkg-php-tools/autoloaders/*
  3. /usr/share/pkg-php-tools/autoloaders/common

This is meant to work in a way similar to the pkg-php-tools-overrides.

These files contain, for example:

vendor/package1 Vendor/PackageOne/autoload.php
symfony/polyfill-php54 none

where the first field is the Composer package name and the second field is the autoloader's path (or none/empty if nothing should be loaded).


The change includes automated tests that could be referenced for an idea of what is generated.

I have also successfully tried using this script in a real build of the php-laravel-framework package.

All feedback is appreciated.

Merge request reports

Loading