Projet

Général

Profil

Paste
Télécharger (10,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / includes / faces.inc @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Extendable Object Faces API. Provided by the faces module.
6
 */
7

    
8
if (!interface_exists('FacesExtenderInterface', FALSE)) {
9

    
10
  /**
11
   * Interface for extenders.
12
   */
13
  interface FacesExtenderInterface {
14

    
15
    /**
16
     * Constructs an instance of the extender.
17
     */
18
    public function __construct(FacesExtendable $object);
19

    
20
    /**
21
     * Returns the extended object.
22
     */
23
    public function getExtendable();
24

    
25
  }
26

    
27
  /**
28
   * The Exception thrown by the FacesExtendable.
29
   */
30
  class FacesExtendableException extends ErrorException {}
31

    
32
}
33

    
34
if (!class_exists('FacesExtender', FALSE)) {
35
  /**
36
   * A common base class for FacesExtenders.
37
   *
38
   * Extenders may access protected methods and properties of the extendable
39
   * using the property() and call() methods.
40
   */
41
  abstract class FacesExtender implements FacesExtenderInterface {
42

    
43
    /**
44
     * @var FacesExtendable
45
     */
46
    protected $object;
47

    
48
    public function __construct(FacesExtendable $object) {
49
      $this->object = $object;
50
    }
51

    
52
    /**
53
     * Returns the extended object.
54
     */
55
    public function getExtendable() {
56
      return $this->object;
57
    }
58

    
59
    /**
60
     * Makes protected properties of the extendable accessible.
61
     */
62
    protected function &property($name) {
63
      $var =& $this->object->property($name);
64
      return $var;
65
    }
66

    
67
    /**
68
     * Invokes any method on the extended object, including protected methods.
69
     *
70
     * @param string $name
71
     *   The method name.
72
     * @param array $args
73
     *   An array of arguments to pass to the method.
74
     */
75
    protected function call($name, array $args = array()) {
76
      return $this->object->call($name, $args);
77
    }
78

    
79
  }
80
}
81

    
82

    
83
if (!class_exists('FacesExtendable', FALSE)) {
84

    
85
  /**
86
   * An extendable base class.
87
   */
88
  abstract class FacesExtendable {
89

    
90
    protected $facesMethods = array();
91
    protected $faces = array();
92
    protected $facesIncludes = array();
93
    protected $facesClassInstances = array();
94
    static protected $facesIncluded = array();
95

    
96
    /**
97
     * Wraps calls to module_load_include() to prevent multiple inclusions.
98
     *
99
     * @see module_load_include()
100
     */
101
    protected static function load_include($args) {
102
      $args += array('type' => 'inc', 'module' => '', 'name' => NULL);
103
      $key = implode(':', $args);
104
      if (!isset(self::$facesIncluded[$key])) {
105
        self::$facesIncluded[$key] = TRUE;
106
        module_load_include($args['type'], $args['module'], $args['name']);
107
      }
108
    }
109

    
110
    /**
111
     * Magic method: Invoke the dynamically implemented methods.
112
     */
113
    public function __call($name, $arguments = array()) {
114
      if (isset($this->facesMethods[$name])) {
115
        $method = $this->facesMethods[$name];
116
        // Include code, if necessary.
117
        if (isset($this->facesIncludes[$name])) {
118
          self::load_include($this->facesIncludes[$name]);
119
          $this->facesIncludes[$name] = NULL;
120
        }
121
        if (isset($method[0])) {
122
          // We always pass the object reference and the name of the method.
123
          $arguments[] = $this;
124
          $arguments[] = $name;
125
          return call_user_func_array($method[0], $arguments);
126
        }
127
        // Call the method on the extender object, but don't use extender()
128
        // for performance reasons.
129
        if (!isset($this->facesClassInstances[$method[1]])) {
130
          $this->facesClassInstances[$method[1]] = new $method[1]($this);
131
        }
132
        return call_user_func_array(array($this->facesClassInstances[$method[1]], $name), $arguments);
133
      }
134
      $class = check_plain(get_class($this));
135
      throw new FacesExtendableException("There is no method $name for this instance of the class $class.");
136
    }
137

    
138
    /**
139
     * Returns the extender object for the given class.
140
     *
141
     * May be used to explicitly invoke a specific extender, e.g. a function
142
     * overriding a method may use that to explicitly invoke the original
143
     * extender.
144
     */
145
    public function extender($class) {
146
      if (!isset($this->facesClassInstances[$class])) {
147
        $this->facesClassInstances[$class] = new $class($this);
148
      }
149
      return $this->facesClassInstances[$class];
150
    }
151

    
152
    /**
153
     * Returns whether the object can face as the given interface.
154
     *
155
     * Returns whether the object can face as the given interface, thus it
156
     * returns TRUE if this object has been extended by an appropriate
157
     * implementation.
158
     *
159
     * @param $interface
160
     *   Optional. An interface to test for. If it's omitted, all interfaces
161
     *   that the object can be faced as are returned.
162
     *
163
     * @return bool
164
     *   Whether the object can face as the interface or an array of interface
165
     *   names.
166
     */
167
    public function facesAs($interface = NULL) {
168
      if (!isset($interface)) {
169
        return array_values($this->faces);
170
      }
171
      return in_array($interface, $this->faces) || $this instanceof $interface;
172
    }
173

    
174
    /**
175
     * Extend the object by a class to implement the given interfaces.
176
     *
177
     * @param $interface
178
     *   The interface name or an array of interface names.
179
     * @param $className
180
     *   The extender class, which has to implement the FacesExtenderInterface.
181
     * @param array $includes
182
     *   An optional array describing the file to include before invoking the
183
     *   class. The array entries known are 'type', 'module', and 'name'
184
     *   matching the parameters of module_load_include(). Only 'module' is
185
     *   required as 'type' defaults to 'inc' and 'name' to NULL.
186
     */
187
    public function extendByClass($interface, $className, array $includes = array()) {
188
      $parents = class_implements($className);
189
      if (!in_array('FacesExtenderInterface', $parents)) {
190
        throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the FacesExtenderInterface.");
191
      }
192
      $interfaces = is_array($interface) ? $interface : array($interface);
193

    
194
      foreach ($interfaces as $interface) {
195
        if (!in_array($interface, $parents)) {
196
          throw new FacesExtendableException("The class " . check_plain($className) . " doesn't implement the interface " . check_plain($interface) . ".");
197
        }
198
        $this->faces[$interface] = $interface;
199
        $this->faces += class_implements($interface);
200
        $face_methods = get_class_methods($interface);
201
        $this->addIncludes($face_methods, $includes);
202
        foreach ($face_methods as $method) {
203
          $this->facesMethods[$method] = array(1 => $className);
204
        }
205
      }
206
    }
207

    
208
    /**
209
     * Extend the object by the given functions to implement the given
210
     * interface. There has to be an implementation function for each method of
211
     * the interface.
212
     *
213
     * @param $interface
214
     *   The interface name or FALSE to extend the object without a given
215
     *   interface.
216
     * @param array $callbacks
217
     *   An array, where the keys are methods of the given interface and the
218
     *   values the callback functions to use.
219
     * @param array $includes
220
     *   An optional array to describe files to include before invoking the
221
     *   callbacks. You may pass a single array describing one include for all
222
     *   callbacks or an array of arrays, keyed by the method names. Look at the
223
     *   extendByClass() $include parameter for more details about how to
224
     *   describe a single file.
225
     */
226
    public function extend($interface, array $callbacks = array(), array $includes = array()) {
227
      $face_methods = $interface ? get_class_methods($interface) : array_keys($callbacks);
228
      if ($interface) {
229
        if (array_diff($face_methods, array_keys($callbacks))) {
230
          throw new FacesExtendableException("Missing methods for implementing the interface " . check_plain($interface) . ".");
231
        }
232
        $this->faces[$interface] = $interface;
233
        $this->faces += class_implements($interface);
234
      }
235
      $this->addIncludes($face_methods, $includes);
236
      foreach ($face_methods as $method) {
237
        $this->facesMethods[$method] = array(0 => $callbacks[$method]);
238
      }
239
    }
240

    
241
    /**
242
     * Override the implementation of an extended method.
243
     *
244
     * @param array $callbacks
245
     *   An array of methods of the interface, that should be overridden, where
246
     *   the keys are methods to override and the values the callback functions
247
     *   to use.
248
     * @param array $includes
249
     *   An optional array to describe files to include before invoking the
250
     *   callbacks. You may pass a single array describing one include for all
251
     *   callbacks or an array of arrays, keyed by the method names. Look at the
252
     *   extendByClass() $include parameter for more details about how to
253
     *   describe a single file.
254
     */
255
    public function override(array $callbacks = array(), array $includes = array()) {
256
      if (array_diff_key($callbacks, $this->facesMethods)) {
257
        throw new FacesExtendableException("A not implemented method is to be overridden.");
258
      }
259
      $this->addIncludes(array_keys($callbacks), $includes);
260
      foreach ($callbacks as $method => $callback) {
261
        $this->facesMethods[$method] = array(0 => $callback);
262
      }
263
    }
264

    
265
    /**
266
     * Adds in include files for the given methods while removing any old files.
267
     *
268
     * If a single include file is described, it's added for all methods.
269
     */
270
    protected function addIncludes($methods, $includes) {
271
      $includes = isset($includes['module']) && is_string($includes['module']) ? array_fill_keys($methods, $includes) : $includes;
272
      $this->facesIncludes = $includes + array_diff_key($this->facesIncludes, array_flip($methods));
273
    }
274

    
275
    /**
276
     * Only serialize what is really necessary.
277
     */
278
    public function __sleep() {
279
      return array('facesMethods', 'faces', 'facesIncludes');
280
    }
281

    
282
    /**
283
     * Destroys all references to created instances.
284
     *
285
     * Destroys all references to created instances so that PHP's garbage
286
     * collection can do its work. This is needed as PHP's gc has troubles with
287
     * circular references until PHP < 5.3.
288
     */
289
    public function destroy() {
290
      // Avoid circular references.
291
      $this->facesClassInstances = array();
292
    }
293

    
294
    /**
295
     * Makes protected properties accessible.
296
     */
297
    public function &property($name) {
298
      if (property_exists($this, $name)) {
299
        return $this->$name;
300
      }
301
    }
302

    
303
    /**
304
     * Invokes any method.
305
     *
306
     * This also allows to pass arguments by reference, so it may be used to
307
     * pass arguments by reference to dynamically extended methods.
308
     *
309
     * @param string $name
310
     *   The method name.
311
     * @param array $args
312
     *   An array of arguments to pass to the method.
313
     */
314
    public function call($name, array $args = array()) {
315
      if (method_exists($this, $name)) {
316
        return call_user_func_array(array($this, $name), $args);
317
      }
318
      return $this->__call($name, $args);
319
    }
320

    
321
  }
322

    
323
}