|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the Apix Project. |
|
6
|
|
|
* |
|
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
|
8
|
|
|
* |
|
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Apix\Plugin; |
|
14
|
|
|
|
|
15
|
|
|
abstract class PluginAbstract implements \SplObserver |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Holds the hook'ing array. |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
public static $hook = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Holds a plugin's adapter. |
|
25
|
|
|
* @var closure|object |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $adapter = null; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Holds an array of plugin's options. |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $options = array(); |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param mix $options Array of options |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct($options=null) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->setOptions($options); |
|
43
|
|
|
|
|
44
|
|
|
if (isset($this->options['adapter'])) { |
|
45
|
|
|
$this->setAdapter($this->options['adapter']); |
|
46
|
|
|
|
|
47
|
|
|
if ( isset(static::$hook) && isset(static::$hook['interface'])) { |
|
48
|
|
|
self::checkAdapterClass( |
|
49
|
|
|
$this->adapter, |
|
50
|
|
|
static::$hook['interface'] |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Checks the plugin's adapter comply to the provided class/interface. |
|
58
|
|
|
* |
|
59
|
|
|
* @param object $adapter |
|
60
|
|
|
* @param object $class |
|
61
|
|
|
* @throws \RuntimeException |
|
62
|
|
|
* @return true |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function checkAdapterClass($adapter, $class) |
|
65
|
|
|
{ |
|
66
|
|
|
if (!is_subclass_of($adapter, $class)) { |
|
67
|
|
|
throw new \RuntimeException( |
|
68
|
|
|
sprintf('%s not implemented.', $class) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Sets and merge the defaults options for this plugin. |
|
77
|
|
|
* |
|
78
|
|
|
* @param mix $options Array of options if it is an object set as an adapter |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setOptions($options=null) |
|
81
|
|
|
{ |
|
82
|
|
|
if (null !== $options) { |
|
83
|
|
|
if (is_object($options)) { |
|
84
|
|
|
$options = array('adapter' => $options); |
|
85
|
|
|
} |
|
86
|
|
|
$this->options = $options+$this->options; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Gets this plugin's options. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getOptions() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->options; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sets this plugin's adapter. |
|
102
|
|
|
* |
|
103
|
|
|
* @param closure|object $adapter |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setAdapter($adapter) |
|
106
|
|
|
{ |
|
107
|
|
|
if (is_string($adapter)) { |
|
108
|
|
|
$this->adapter = new $adapter(); |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->adapter = $adapter instanceof \Closure |
|
111
|
|
|
? $adapter() |
|
112
|
|
|
: $adapter; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Gets this plugin's adapter. |
|
118
|
|
|
* |
|
119
|
|
|
* @return mix |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getAdapter() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->adapter; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// /** |
|
127
|
|
|
// * Just a shortcut for now. This is TEMP and will be moved elsewhere! |
|
128
|
|
|
// * TODO: TEMP to refactor |
|
129
|
|
|
// * @codeCoverageIgnore |
|
130
|
|
|
// */ |
|
131
|
|
|
// public function log($msg, $context=null, $level='debug') |
|
|
|
|
|
|
132
|
|
|
// { |
|
133
|
|
|
// if (defined('DEBUG') && !defined('UNIT_TEST')) { |
|
|
|
|
|
|
134
|
|
|
// if(is_array($context)) $context = implode(', ', $context); |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
// $str = sprintf('%s %s (%s)', get_class($this), $msg, $context); |
|
|
|
|
|
|
137
|
|
|
// error_log( $str ); |
|
|
|
|
|
|
138
|
|
|
// } |
|
139
|
|
|
// } |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.