I spent a lot of time working and thinking about the API in xautoload-7.x-4.x.
Finally I decided there is some stuff that is really difficult to maintain, and that I would like to get rid of.
PHP Version
The new version will require PHP 5.3.
(and use namespaces?)
The PHP 5.2 compatibility mode will still be supported, e.g. for Crumbs and Menupoly.
Add PSR-4 support
There will be ways to register namespaces as PSR-4. But it will not be registered by default for modules, until Drupal 8 has done its homework, and decided whether those files should live in 'src' or in 'lib'.
For now, if you want your module to use PSR-4 with the 7.x-4.x branch in the 'src' folder, you will have to put this at the top of the module file:
xautoload()->registerModulePsr4(__FILE__, 'src');
[..] // rest of the *.module file
Familiarity with Composer
There will be new API features that look and feel more like what people know from Composer.
E.g. you no longer need to remember custom stuff like addNamespaceDeep()
, but can use methods like add()
and addPsr4()
that behave exactly like the equivalent on the Composer class loader.
There will also be ways to read autoload information for a library directly from a composer.json
file, or from a directory with Composer-generated autoload files.
Changes to hook_xautoload() and hook_libraries_info()
The biggest API-breaking change here is that
paths are no longer relative to the module or library directory.
Instead, the module dir or library dir will be passed around as a second argument in the hook.
Old (7.x-3.x)
function hook_xautoload($api) {
// 'ruebenkraut' library shipped with the module.
// 'ruebenkraut/src' being relative to the module root.
$api->addNamespaceRoot('Rueben\Kraut\\', 'ruebenkraut/src');
}
function hook_libraries_info() {
return array(
// 'ruebenkraut' library sitting in 'sites/all/libraries/ruebenkraut'.
'ruebenkraut' => array(
[..]
'xautoload' => function($api) {
// 'src' is relative to 'sites/all/libraries/ruebenkraut', so it means 'sites/all/libraries/ruebenkraut/src'.
$api->addNamespaceRoot('Rueben\Kraut\\', 'src');
}
)
);
}
New (7.x-4.x):
function hook_xautoload($api, $module_dir) {
// 'ruebenkraut' library shipped with the module.
// 'ruebenkraut/src' being relative to the module root.
$api->add('Rueben\Kraut\\', $module_dir . '/ruebenkraut/src');
}
function hook_libraries_info() {
return array(
// 'ruebenkraut' library sitting in 'sites/all/libraries/ruebenkraut'.
'ruebenkraut' => array(
[..]
// $ruebenkraut_dir will be 'sites/all/libraries/ruebenkraut'.
'xautoload' => function($api, $ruebenkraut_dir) {
// Add a PSR-0 root for ruebenkraut. The directory is absolute, or relative to Drupal root.
$api->add('Rueben\Kraut\\', $ruebenkraut_dir . '/src');
}
)
);
}
I even wonder if I should entirely ditch the callback for libraries?
And, should I try to leave more legacy methods for BC compatibility?
Comments
Comment #1
donquixote CreditAttribution: donquixote commentedComment #2
donquixote CreditAttribution: donquixote commentedCompromise:
Old-style/legacy methods like $api->namespaceDeep() will keep their arguments as relative to the module or library directory.
New Composer-style methods like $api->add() and $api->addPsr4() will take absolute paths in their arguments.
Comment #3
donquixote CreditAttribution: donquixote commentedAnother solution for the relative paths could be this:
- Keep old methods as they are, but deprecate the $relative parameter.
- Don't add a $relative parameter to new methods, but let them all treat path parameters as relative to the module dir / library dir.
- Allow
$api->absolute()->addStuff($namespace, $dir)
, for a $dir that is a full path (relative to Drupal root, not the module dir).Comment #4
donquixote CreditAttribution: donquixote commentedA new feature enters the API: Class map generation!
This is going to be very useful for 3rd party libraries that don't have PEAR or PSR-0 or PSR-4 or anything.
The nice thing: The generated class map is cached in the regular Drupal cache.
Comment #5
donquixote CreditAttribution: donquixote commentedThis is fixed.
For up to date API docs, look at the documentation pages, not the above posts.