Scripts::script()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 6
nop 2
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

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.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The variable $config does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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
}