|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @file |
|
4
|
|
|
* The extension registry. |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace QueryPath; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A registry for QueryPath extensions. |
|
11
|
|
|
* |
|
12
|
|
|
* QueryPath extensions should call the QueryPath::ExtensionRegistry::extend() |
|
13
|
|
|
* function to register their extension classes. The QueryPath library then |
|
14
|
|
|
* uses this information to determine what QueryPath extensions should be loaded and |
|
15
|
|
|
* executed. |
|
16
|
|
|
* |
|
17
|
|
|
* Extensions are attached to a Query object. |
|
18
|
|
|
* |
|
19
|
|
|
* To enable an extension (the easy way), use QueryPath::enable(). |
|
20
|
|
|
* |
|
21
|
|
|
* This class provides lower-level interaction with the extension |
|
22
|
|
|
* mechanism. |
|
23
|
|
|
* |
|
24
|
|
|
* @ingroup querypath_extensions |
|
25
|
|
|
*/ |
|
26
|
|
|
class ExtensionRegistry |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Internal flag indicating whether or not the registry should |
|
31
|
|
|
* be used for automatic extension loading. If this is false, then |
|
32
|
|
|
* implementations should not automatically load extensions. |
|
33
|
|
|
*/ |
|
34
|
|
|
public static $useRegistry = true; |
|
35
|
|
|
/** |
|
36
|
|
|
* The extension registry. This should consist of an array of class |
|
37
|
|
|
* names. |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $extensionRegistry = []; |
|
40
|
|
|
protected static $extensionMethodRegistry = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Extend a Query with the given extension class. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function extend($classname) |
|
46
|
|
|
{ |
|
47
|
|
|
self::$extensionRegistry[] = $classname; |
|
48
|
|
|
$class = new \ReflectionClass($classname); |
|
49
|
|
|
$methods = $class->getMethods(); |
|
50
|
|
|
foreach ($methods as $method) { |
|
51
|
|
|
self::$extensionMethodRegistry[$method->getName()] = $classname; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check to see if a method is known. |
|
57
|
|
|
* This checks to see if the given method name belongs to one of the |
|
58
|
|
|
* registered extensions. If it does, then this will return TRUE. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $name |
|
61
|
|
|
* The name of the method to search for. |
|
62
|
|
|
* @return boolean |
|
63
|
|
|
* TRUE if the method exists, false otherwise. |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function hasMethod($name) |
|
66
|
|
|
{ |
|
67
|
|
|
return isset(self::$extensionMethodRegistry[$name]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check to see if the given extension class is registered. |
|
72
|
|
|
* Given a class name for a QueryPath::Extension class, this |
|
73
|
|
|
* will check to see if that class is registered. If so, it will return |
|
74
|
|
|
* TRUE. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $name |
|
77
|
|
|
* The name of the class. |
|
78
|
|
|
* @return boolean |
|
79
|
|
|
* TRUE if the class is registered, FALSE otherwise. |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function hasExtension($name) |
|
82
|
|
|
{ |
|
83
|
|
|
return in_array($name, self::$extensionRegistry); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the class that a given method belongs to. |
|
88
|
|
|
* Given a method name, this will check all registered extension classes |
|
89
|
|
|
* to see if any of them has the named method. If so, this will return |
|
90
|
|
|
* the classname. |
|
91
|
|
|
* |
|
92
|
|
|
* Note that if two extensions are registered that contain the same |
|
93
|
|
|
* method name, the last one registred will be the only one recognized. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $name |
|
96
|
|
|
* The name of the method. |
|
97
|
|
|
* @return string |
|
98
|
|
|
* The name of the class. |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function getMethodClass($name) |
|
101
|
|
|
{ |
|
102
|
|
|
return self::$extensionMethodRegistry[$name]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Get extensions for the given Query object. |
|
107
|
|
|
* |
|
108
|
|
|
* Given a Query object, this will return |
|
109
|
|
|
* an associative array of extension names to (new) instances. |
|
110
|
|
|
* Generally, this is intended to be used internally. |
|
111
|
|
|
* |
|
112
|
|
|
* @param Query $qp |
|
113
|
|
|
* The Query into which the extensions should be registered. |
|
114
|
|
|
* @return array |
|
115
|
|
|
* An associative array of classnames to instances. |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function getExtensions(Query $qp) |
|
118
|
|
|
{ |
|
119
|
|
|
$extInstances = []; |
|
120
|
|
|
foreach (self::$extensionRegistry as $ext) { |
|
121
|
|
|
$extInstances[$ext] = new $ext($qp); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $extInstances; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public static function extensionNames() : array |
|
128
|
|
|
{ |
|
129
|
|
|
return self::$extensionRegistry; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Enable or disable automatic extension loading. |
|
134
|
|
|
* |
|
135
|
|
|
* If extension autoloading is disabled, then QueryPath will not |
|
136
|
|
|
* automatically load all registred extensions when a new Query |
|
137
|
|
|
* object is created using qp(). |
|
138
|
|
|
* @param bool $boolean |
|
139
|
|
|
*/ |
|
140
|
|
|
public static function autoloadExtensions($boolean = true) : void |
|
141
|
|
|
{ |
|
142
|
|
|
self::$useRegistry = $boolean; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|