1
|
|
|
<?php |
|
|
|
|
2
|
|
|
class ar_pinp extends arBase { |
3
|
|
|
|
4
|
|
|
private static $allowed; |
5
|
|
|
|
6
|
8 |
|
public static function allow($class, $methods = null) { |
7
|
8 |
|
if (isset($methods)) { |
8
|
8 |
|
self::$allowed[$class]['methods'] = array_combine( $methods, array_fill( 0, count($methods), true ) ); |
9
|
8 |
|
if (func_num_args()>2) { |
10
|
|
|
$args = array_slice(func_get_args(), 2); |
11
|
|
|
self::$allowed[$class]['implements'] = $args; |
12
|
|
|
} |
13
|
8 |
|
} else { |
14
|
4 |
|
self::$allowed[$class]['methods'] = true; |
15
|
|
|
} |
16
|
8 |
|
} |
17
|
|
|
|
18
|
|
|
public static function allowMatch($class, $methods) { |
19
|
|
|
self::$allowed[$class]['matches'] = array_combine( $methods, array_fill( 0, count($methods), true ) ); |
20
|
|
|
} |
21
|
|
|
|
22
|
12 |
|
public static function isAllowed($class, $method) { |
23
|
|
|
// FIXME: support interfaces? |
|
|
|
|
24
|
12 |
|
if (!is_string($class)) { |
25
|
12 |
|
$class = get_class($class); |
26
|
12 |
|
} |
27
|
12 |
|
$current = $class; |
28
|
|
|
do { |
29
|
12 |
|
if (isset(self::$allowed[$current]) ) { |
30
|
12 |
|
if (self::$allowed[$current]['methods']===true) { |
31
|
12 |
|
return true; |
32
|
4 |
|
} else if (isset(self::$allowed[$current]['methods'][$method]) |
33
|
4 |
|
&& self::$allowed[$current]['methods'][$method]) { |
34
|
4 |
|
return true; |
35
|
|
|
} else if (isset(self::$allowed[$current]['matches'])) { |
36
|
|
|
$result = preg_match( '/(' . implode ( '|', self::$allowed[$current]['matches']) . ')/is', $method ); |
37
|
|
|
if ($result) { |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} while ($current = get_parent_class($current)); |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
public static function getCallback( $method, $params = array() ) { |
47
|
4 |
|
if (is_string($method)) { |
48
|
4 |
|
return function() use ( $params, $method ) { |
49
|
|
|
$me = ar::context()->getObject(); |
|
|
|
|
50
|
|
|
$args_in = func_get_args(); |
51
|
|
|
if (count($params)) { |
52
|
|
|
$args = array(); |
53
|
|
|
foreach($params as $key => $arg) { |
54
|
|
|
$args[$arg] = $args_in[$key]; |
55
|
|
|
} |
56
|
|
|
} else { |
57
|
|
|
$args = $args_in; |
58
|
|
|
} |
59
|
|
|
return ar::call($method, $args); |
60
|
4 |
|
}; |
61
|
|
|
} else { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function load( $name, $library ) { |
67
|
|
|
$context = ar::context()->getObject(); |
68
|
|
|
return $context->loadLibrary( $name, $library ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function loaded( $name = null, $library = null ) { |
72
|
|
|
global $ARConfig; |
73
|
|
|
// return if a library with that name is loaded |
74
|
|
|
// if library is set will also check if it is the same path |
75
|
|
|
$context = ar::context()->getObject(); |
76
|
|
|
$path = $context->path; |
77
|
|
|
$config = $ARConfig->cache[$path] ?: $context->loadConfig($path); |
78
|
|
|
if ( isset($name) ) { |
79
|
|
|
$libraryPath = $config->libraries[$name] ?: null; |
80
|
|
|
if ( isset($library) ) { |
81
|
|
|
return ($libraryPath == $library ); |
82
|
|
|
} else { |
83
|
|
|
return $libraryPath; |
84
|
|
|
} |
85
|
|
|
} else { |
86
|
|
|
return $config->libraries; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function exists( $template ) { |
91
|
|
|
// will check if a template with the given name is available to call |
92
|
|
|
// template has format "library:type::function" |
93
|
|
|
// FIXME: allow search for specific nls as well |
|
|
|
|
94
|
|
|
$context = ar::context()->getObject(); |
95
|
|
|
$templateData = $context->getPinpTemplate($template); |
96
|
|
|
if ( !$templateData || !$templateData['arTemplateId'] ) { |
97
|
|
|
return false; |
98
|
|
|
} else { |
99
|
|
|
return $templateData; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
ar_pinp::allow('ar_pinp', array('isAllowed', 'getCallback', 'load', 'loaded', 'exists')); |
105
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.