summaryrefslogtreecommitdiffstats
path: root/xautoload.api.php
blob: 52377e60ca429be12cf3e71caae5ee014b06c2d4 (plain)
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
<?php

/**
 * @file
 * Hooks provided by X Autoload.
 */


/**
 * Implements hook_xautoload()
 *
 * Register additional classes, namespaces, autoload patterns, that are not
 * already registered by default.
 *
 * @param xautoload_InjectedAPI_hookXautoload $api
 *
 *   Object with a number of methods, which are documented at the class
 *   definition of xautoload_InjectedAPI_hookXautoload.
 *
 *   The object already knows which module we are at, so we don't need
 *   drupal_get_path().
 *
 *   TODO: The $api object should be specified by an interface.
 */
function hook_xautoload($api) {

  // Declare a foreign namespace in (module dir)/lib/ForeignNamespace/
  $api->namespaceRoot('ForeignNamespace');

  // Declare a foreign namespace in (module dir)/vendor/ForeignNamespace/
  $api->namespaceRoot('ForeignNamespace', 'vendor');

  // Declare a foreign namespace in /home/username/lib/ForeignNamespace/,
  // setting the $relative argument to FALSE.
  $api->namespaceRoot('ForeignNamespace', '/home/username/lib', FALSE);
}


/**
 * 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($api).
 * This means, you can use the same methods on the $api object.
 *
 * TODO: The $api object should be specified by an interface.
 *
 * @return array
 *   Same as explained in libraries module, but with added key 'xautoload'.
 */
function mymodule_libraries_info() {

  return array(
    'mymodule-test-lib' => array(
      'name' => 'My test library',
      'vendor url' => 'http://www.example.com',
      'download url' => 'http://github.com/example/my-php-api',
      'version' => '1.0',
      'xautoload' => function($api) {
        // Register a namespace with PSR-0 root in <library dir>/lib/
        // Note: $api already knows the library directory.
        // Note: We could omit the 'lib', as this is the default value.
        $api->namespaceRoot('XALib\TestNamespace', 'lib');
      },
    ),
  );
}