From Fedora Project Wiki

< SIGs‎ | PHP

No edit summary
No edit summary
Line 17: Line 17:
There are various way to create an autoloader
There are various way to create an autoloader


'''classmap''' : when "composer.json" describe a library to autoload a classmap, e.g.
'''classmap''': when "composer.json" describe a library to autoload a classmap, e.g.
<pre>
<pre>
"autoload": {
"autoload": {
Line 74: Line 74:


=== More autoloader tips ===
=== More autoloader tips ===
'''Test''' your autoloader : usually by using it to run the test suite, e.g.
'''Test''' your autoloader: usually by using it to run the test suite, and better to run the installed copy, e.g.
<pre>
<pre>
%check
%check

Revision as of 12:16, 26 June 2015

PHP Packaging Tips

Autoloader

Explanation

Composer: most library and lot of applications are now "composer" aware. Which means composer is used to install dependencies and create a suitable autoloader. Composer is mostly a "Bundled every library in every project", not something we want in Fedora.

Consumer Autoloader: in some case, we can add an autoloader for an application and all its dependencies. It seems this is not the best solution, if the dependency tree change, the autoloader need to be fixed.

Provider Autoloader: if each library provides an autoloader for its classes and its dependencies, a consumer just have to include this autoloader. This seems the best solution, so recommended here.

Providing an autoloader is not mandatory (so not in the Guidelines), but seems a good practice.

Implementation

There are various way to create an autoloader

classmap: when "composer.json" describe a library to autoload a classmap, e.g.

"autoload": {
      "classmap": ["src/"]
}

the simple solution is to generate an simple classmap autoloader, using the phpab command, e.g.

BuildRequires: %{_bindir}/phpab

%build
%{_bindir}/phpab --output src/autoload.php src

symfony: recommended when your package already pull some symfony components and is PSR-0/PSR-4 compliant, e.g.

"require": {
   "php": ">=5.3.2",
   "symfony/console": "~2.5",
   "foo/baz": "~1.0"
},
"autoload": {
    "psr-0": { "Foo\\Bar\\": "src/" }
},

In your spec:

BuildRequires: php-composer(symfony/class-loader)
Requires:      php-composer(symfony/class-loader)

(cat <<'AUTOLOAD'
<?php
if (!isset($fedoraClassLoader) || !($fedoraClassLoader instanceof \Symfony\Component\ClassLoader\ClassLoader)) {
    if (!class_exists('Symfony\\Component\\ClassLoader\\ClassLoader', false)) {
        require_once '%{_datadir}/php/Symfony/Component/ClassLoader/ClassLoader.php';
    }
    $fedoraClassLoader = new \Symfony\Component\ClassLoader\ClassLoader();
    $fedoraClassLoader->register();
}
// This library
$fedoraClassLoader->addPrefix('Foo\\Bar', dirname(dirname(__DIR__)));
// Another library (dependency)
require_once '/usr/share/php/Foo/Baz/autoload.php';
AUTOLOAD
) | tee src/autoload.php

Notice: in this implementation proposal, $fedoraClassLoader can be shared between libraries when various autoloader are stacked.

ZendFramework: recommended when your package already pull some ZF components.

TODO and example

More autoloader tips

Test your autoloader: usually by using it to run the test suite, and better to run the installed copy, e.g.

%check
phpunit --bootstrap=%{buildroot}/%{_datadir}/php/Foo/Bar/autoload.php

Use full path to dependencies autoloader, to ensure you use the expected one (don't rely on include_path to avoid /usr/share/pear which can provide an old version, or SCL which doesn't includes /usr/share/php, or any user altered env.)

Use relative path for the library itself, so the autoloader can be included from /usr/share/php or %{buildroot}