summaryrefslogtreecommitdiffstats
path: root/lib/InjectedAPI/hookXautoload.php
blob: 22738537c9684075c17d61f4025714167a0a420a (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php


/**
 * An instance of this class is passed around to implementations of
 * hook_xautoload().
 */
class xautoload_InjectedAPI_hookXautoload {

  protected $finder;
  protected $extensionDir;

  /**
   * @param xautoload_ClassFinder $finder
   *   The class finder object.
   */
  function __construct($finder) {
    $this->finder = $finder;
  }

  /**
   * Register an additional namespace for this module.
   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
   *
   * @param string $namespace
   *   The namespace
   * @param string $psr_0_root_dir
   *   PSR-0 root dir.
   *   If $relative is TRUE, this is relative to the current module dir.
   *   If $relative is FALSE, this is an absolute path.
   * @param boolean $relative
   *   Whether or not the path is relative to the current extension dir.
   */
  function namespaceRoot($namespace, $psr_0_root_dir = NULL, $relative = TRUE) {
    $psr_0_root_dir = $this->processDir($psr_0_root_dir, $relative);
    $this->finder->registerNamespaceRoot($namespace, $psr_0_root_dir);
  }

  /**
   * Register an additional namespace for this module.
   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
   *
   * @param string $namespace
   *   The namespace
   * @param string $prefix_root_dir
   *   Prefix root dir.
   *   If $relative is TRUE, this is relative to the extension module dir.
   *   If $relative is FALSE, this is an absolute path.
   * @param boolean $relative
   *   Whether or not the path is relative to the current extension dir.
   */
  function prefixRoot($prefix, $prefix_root_dir = NULL, $relative = TRUE) {
    $prefix_root_dir = $this->processDir($prefix_root_dir, $relative);
    $this->finder->registerPrefixRoot($prefix, $prefix_root_dir);
  }

  /**
   * Register an additional namespace for this module.
   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
   *
   * @param string $namespace
   *   The namespace
   * @param string $psr_0_root_dir
   *   PSR-0 root dir.
   *   If $relative is TRUE, this is relative to the current extension dir.
   *   If $relative is FALSE, this is an absolute path.
   * @param boolean $relative
   *   Whether or not the path is relative to the current extension dir.
   */
  function namespaceDeep($namespace, $namespace_deep_dir = NULL, $relative = TRUE) {
    $namespace_deep_dir = $this->processDir($namespace_deep_dir, $relative);
    $this->finder->registerNamespaceDeep($namespace, $namespace_deep_dir);
  }

  /**
   * Register an additional namespace for this module.
   * Note: Drupal\<module name>\ is already registered for <module dir>/lib.
   *
   * @param string $namespace
   *   The namespace
   * @param string $prefix_deep_dir
   *   PSR-0 root dir.
   *   If $relative is TRUE, this is relative to the current extension dir.
   *   If $relative is FALSE, this is an absolute path.
   * @param boolean $relative
   *   Whether or not the path is relative to the current extension dir.
   */
  function prefixDeep($prefix, $prefix_deep_dir = NULL, $relative = TRUE) {
    $prefix_root_dir = $this->processDir($prefix_deep_dir, $relative);
    $this->finder->registerPrefixDeep($prefix, $prefix_deep_dir);
  }

  /**
   * Legacy: Plugins were called "Handler" before.
   */
  function namespaceHandler($namespace, $plugin) {
    $this->finder->registerNamespacePlugin($namespace, $plugin);
  }

  /**
   * Legacy: Plugins were called "Handler" before.
   */
  function prefixHandler($prefix, $plugin) {
    $this->finder->registerPrefixPlugin($prefix, $plugin);
  }

  /**
   * Register a namespace plugin object
   */
  function namespacePlugin($namespace, $plugin) {
    $this->finder->registerNamespacePlugin($namespace, $plugin);
  }

  /**
   * Register a prefix plugin object
   */
  function prefixPlugin($prefix, $plugin) {
    $this->finder->registerPrefixPlugin($prefix, $plugin);
  }

  /**
   * Process a given directory to make it relative to Drupal root,
   * instead of relative to the current extension dir.
   *
   * @param string $dir
   *   The directory path that we want to make absolute.
   * @param boolean $relative
   *   If TRUE, the $dir will be transformed from relative to absolute.
   *   If FALSE, the $dir is assumed to already be absolute, and remain unchanged.
   *
   * @return string
   *   The modified (absolute) directory path.
   */
  protected function processDir($dir, $relative) {
    if (!isset($dir)) {
      $dir = $this->extensionDir . '/lib';
    }
    elseif ($relative) {
      // Root dir is relative to module root.
      if (empty($dir)) {
        $dir = $this->extensionDir;
      }
      else {
        $dir = $this->extensionDir . '/' . $dir;
      }
    }
    else {
      // Leave the $dir as it is.
    }
    return $dir;
  }

  /**
   * Set a module to use as base for relative paths.
   * This is typically called before each invocation of hook_xautoload() on a
   * module.
   * It can also be called by a module or theme that implements hook_xautoload(),
   * to register class loading information on behalf of another module.
   *
   * @param string $module
   *   Machine name of the module.
   */
  function setModule($module) {
    $this->extensionDir = drupal_get_path('module', $module);
  }

  /**
   * Set a theme to use as base for relative paths.
   * This is typically called before each invocation of hook_xautoload() on a
   * theme.
   * It can also be called by a module or theme that implements hook_xautoload(),
   * to register class loading information on behalf of another theme.
   *
   * @param string $theme
   *   Machine name of the theme.
   */
  function setTheme($theme) {
    $this->extensionDir = drupal_get_path('theme', $theme);
  }

  /**
   * Set a library to use as base for relative paths.
   * This is called e.g. in libraries_xautoload(), before passing the $api
   * object to library xautoload callbacks.
   *
   * @param string $library
   *   Machine name of the library.
   */
  function setLibrary($library) {
    if (!module_exists('libraries')) {
      throw new Exception('Libraries module not installed.');
    }
    $this->extensionDir = libraries_get_path($library);
  }

  /**
   * Explicitly set the base for relative paths.
   *
   * @param string $dir
   *   New relative base path.
   */
  function setExtensionDir($dir) {
    $this->extensionDir = $dir;
  }
}