-
Notifications
You must be signed in to change notification settings - Fork 1
/
xautoload.api.php
95 lines (85 loc) · 3 KB
/
xautoload.api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* @file
* Hooks provided by X Autoload.
*/
/**
* Example method showing how to register namespaces from anywhere.
*/
function EXAMPLE_foo() {
// Register stuff directly to the class finder.
xautoload()->finder->addPsr4('Aaa\Bbb\\', 'sites/all/libraries/aaa-bbb/src');
// Or use an adapter with more powerful methods.
xautoload()->adapter->composerDir('sites/all/vendor/composer');
}
/**
* Implements hook_xautoload()
*
* Register additional classes, namespaces, autoload patterns, that are not
* already registered by default.
*
* @param \Drupal\xautoload\Adapter\LocalDirectoryAdapter $adapter
* An adapter object that can register stuff into the class loader.
*/
function hook_xautoload($adapter) {
// Register a namespace with PSR-0.
$adapter->add(
// Namespace of a 3rd party package included in the module directory.
'Acme\GardenKit\\',
// Path to the 3rd party package, relative to the module directory.
'shrubbery/lib');
// Register a namespace with PSR-4.
$adapter->absolute()->addPsr4(
// The namespace.
'Acme\ShrubGardens\\',
// Absolute path to the PSR-4 base directory.
'/home/karnouffle/php/shrub-gardens/src');
// Scan sites/all/vendor/composer for Composer-generated autoload files, e.g.
// 'sites/all/vendor/composer/autoload_namespaces.php', etc.
$adapter->absolute()->composerDir('sites/all/vendor/composer');
}
/**
* Implements hook_libraries_info()
*
* Allows to register PSR-0 (or other) class folders for your libraries.
* (those things living in sites/all/libraries)
*
* The original documentation for this hook is at libraries module,
* libraries.api.php
*
* X Autoload extends the capabilities of this hook, by adding an "xautoload"
* key. This key takes a callback or closure function, which has the same
* signature as hook_xautoload($adapter).
* This means, you can use the same methods on the $api object.
*
* @return array[]
* Same as explained in libraries module, but with added key 'xautoload'.
*/
function mymodule_libraries_info() {
return array(
'ruebenkraut' => array(
'name' => 'Rübenkraut library',
'vendor url' => 'http://www.example.com',
'download url' => 'http://github.com/example/ruebenkraut',
'version' => '1.0',
'xautoload' => function($adapter) {
/**
* @var \Drupal\xautoload\Adapter\LocalDirectoryAdapter $adapter
* An adapter object that can register stuff into the class loader.
*/
// Register a namespace with PSR-0 root in
// 'sites/all/libraries/ruebenkraut/src'.
$adapter->add('Rueben\Kraut\\', 'src');
},
),
'gurkentraum' => array(
'name' => 'Gurkentraum library',
'xautoload' => function($adapter) {
/** @var \Drupal\xautoload\Adapter\LocalDirectoryAdapter $adapter */
// Scan sites/all/libraries/ruebenkraut/composer.json to look for
// autoload information.
$adapter->composerJson('composer.json');
}
)
);
}