|
1
|
|
|
<?php |
|
2
|
|
|
namespace PhpQuery\Plugin; |
|
3
|
|
|
|
|
4
|
|
|
use PhpQuery\PhpQuery; |
|
5
|
|
|
/** |
|
6
|
|
|
* PhpQuery plugin class extending PhpQuery object. |
|
7
|
|
|
* Methods from this class are callable on every PhpQuery object. |
|
8
|
|
|
* |
|
9
|
|
|
* Class name prefix '\PhpQuery\Plugin\' must be preserved. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class Scripts { |
|
12
|
|
|
/** |
|
13
|
|
|
* Limit binded methods. |
|
14
|
|
|
* |
|
15
|
|
|
* null means all public. |
|
16
|
|
|
* array means only specified ones. |
|
17
|
|
|
* |
|
18
|
|
|
* @var array|null |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $PhpQueryMethods = null; |
|
21
|
|
|
public static $config = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Enter description here... |
|
25
|
|
|
* |
|
26
|
|
|
* @param \PhpQuery\PhpQueryObject $self |
|
27
|
|
|
* @param $arg1 |
|
28
|
|
|
* @return null|\PhpQuery\PhpQueryObject |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function script($self, $arg1) { |
|
31
|
|
|
$params = func_get_args(); |
|
32
|
|
|
$params = array_slice($params, 2); |
|
33
|
|
|
$return = null; |
|
34
|
|
|
$config = self::$config; |
|
35
|
|
|
if (\PhpQuery\Plugin\UtilScripts::$scriptMethods[$arg1]) { |
|
36
|
|
|
PhpQuery::callbackRun( |
|
37
|
|
|
\PhpQuery\Plugin\UtilScripts::$scriptMethods[$arg1], |
|
38
|
|
|
array($self, $params, &$return, $config) |
|
39
|
|
|
); |
|
40
|
|
|
} else if ($arg1 != '__config' && file_exists(dirname(__FILE__)."/Scripts/$arg1.php")) { |
|
41
|
|
|
PhpQuery::debug("Loading script '$arg1'"); |
|
42
|
|
|
require dirname(__FILE__)."/Scripts/$arg1.php"; |
|
43
|
|
|
} else { |
|
44
|
|
|
PhpQuery::debug("Requested script '$arg1' doesn't exist"); |
|
45
|
|
|
} |
|
46
|
|
|
return $return |
|
47
|
|
|
? $return |
|
48
|
|
|
: $self; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
abstract class UtilScripts { |
|
|
|
|
|
|
52
|
|
|
public static $scriptMethods = array(); |
|
53
|
|
|
public static function __initialize() { |
|
54
|
|
|
if (file_exists(dirname(__FILE__)."/Scripts/__config.php")) { |
|
55
|
|
|
include dirname(__FILE__)."/Scripts/__config.php"; |
|
56
|
|
|
\PhpQuery\Plugin\Scripts::$config = $config; |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Extend scripts' namespace with $name related with $callback. |
|
62
|
|
|
* |
|
63
|
|
|
* Callback parameter order looks like this: |
|
64
|
|
|
* - $this |
|
65
|
|
|
* - $params |
|
66
|
|
|
* - &$return |
|
67
|
|
|
* - $config |
|
68
|
|
|
* |
|
69
|
|
|
* @param $name |
|
70
|
|
|
* @param $callback |
|
71
|
|
|
* @throws \Exception |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function script($name, $callback) { |
|
75
|
|
|
if (\PhpQuery\Plugin\UtilScripts::$scriptMethods[$name]) |
|
76
|
|
|
throw new \Exception("Script name conflict - '$name'"); |
|
77
|
|
|
\PhpQuery\Plugin\UtilScripts::$scriptMethods[$name] = $callback; |
|
78
|
|
|
} |
|
79
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.