1 | <?php |
||
2 | |||
3 | class PluginRegister { |
||
4 | private $requestedPlugins; |
||
5 | |||
6 | public function __construct($requestedPlugins=array()) { |
||
7 | $this->requestedPlugins = $requestedPlugins; |
||
8 | $this->pluginOrder = array(); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
9 | foreach ($this->requestedPlugins as $index => $value) { |
||
10 | $this->pluginOrder[$value] = $index; |
||
11 | } |
||
12 | } |
||
13 | |||
14 | /** |
||
15 | * Returns the plugin configurations found from plugin folders inside the plugins folder |
||
16 | * @return array |
||
17 | */ |
||
18 | protected function getPlugins() |
||
19 | { |
||
20 | $plugins = array(); |
||
21 | $pluginconfs = glob('plugins/*/plugin.json'); |
||
22 | foreach ($pluginconfs as $path) { |
||
23 | $folder = explode('/', $path); |
||
24 | if (file_exists($path)) { |
||
25 | $plugins[$folder[1]] = json_decode(file_get_contents($path), true); |
||
26 | } |
||
27 | } |
||
28 | return $plugins; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * Sort plugins by order defined in class constructor |
||
33 | * @return array |
||
34 | */ |
||
35 | private function sortPlugins($plugins) { |
||
36 | $sortedPlugins = array(); |
||
0 ignored issues
–
show
|
|||
37 | $sortedPlugins = array_replace($this->pluginOrder, $plugins); |
||
38 | return $sortedPlugins; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Returns the plugin configurations found from plugin folders |
||
43 | * inside the plugins folder filtered by filetype. |
||
44 | * @param string $type filetype e.g. 'css', 'js' or 'template' |
||
45 | * @param boolean $raw interpret $type values as raw text instead of files |
||
46 | * @return array |
||
47 | */ |
||
48 | private function filterPlugins($type, $raw=false) { |
||
49 | $plugins = $this->getPlugins(); |
||
50 | $plugins = $this->sortPlugins($plugins); |
||
51 | $ret = array(); |
||
52 | if (!empty($plugins)) { |
||
53 | foreach ($plugins as $name => $files) { |
||
54 | if (isset($files[$type])) { |
||
55 | $ret[$name] = array(); |
||
56 | if ($raw) { |
||
57 | $ret[$name] = $files[$type]; |
||
58 | } |
||
59 | else { |
||
60 | foreach ($files[$type] as $file) { |
||
61 | array_push($ret[$name], 'plugins/' . $name . '/' . $file); |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | return $ret; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns the plugin configurations found from plugin folders |
||
72 | * inside the plugins folder filtered by plugin name (the folder name). |
||
73 | * @param string $type filetype e.g. 'css', 'js' or 'template' |
||
74 | * @param array $names the plugin name strings (foldernames) in an array |
||
75 | * @return array |
||
76 | */ |
||
77 | private function filterPluginsByName($type, $names) { |
||
78 | $files = $this->filterPlugins($type); |
||
79 | foreach ($files as $plugin => $filelist) { |
||
80 | if (!in_array($plugin, $names)) { |
||
81 | unset($files[$plugin]); |
||
82 | } |
||
83 | } |
||
84 | return $files; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Returns an array of javascript filepaths |
||
89 | * @param array $names the plugin name strings (foldernames) in an array |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getPluginsJS($names=null) { |
||
93 | if ($names) { |
||
94 | $names = array_merge($this->requestedPlugins, $names); |
||
95 | return $this->filterPluginsByName('js', $names); |
||
96 | } |
||
97 | return $this->filterPluginsByName('js', $this->requestedPlugins); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Returns an array of css filepaths |
||
102 | * @param array $names the plugin name strings (foldernames) in an array |
||
103 | * @return array |
||
104 | */ |
||
105 | public function getPluginsCSS($names=null) { |
||
106 | if ($names) { |
||
107 | $names = array_merge($this->requestedPlugins, $names); |
||
108 | return $this->filterPluginsByName('css', $names); |
||
109 | } |
||
110 | return $this->filterPluginsByName('css', $this->requestedPlugins); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns an array of template filepaths |
||
115 | * @param array $names the plugin name strings (foldernames) in an array |
||
116 | * @return array |
||
117 | */ |
||
118 | public function getPluginsTemplates($names=null) { |
||
119 | if ($names) { |
||
120 | $names = array_merge($this->requestedPlugins, $names); |
||
121 | return $this->filterPluginsByName('templates', $names); |
||
122 | } |
||
123 | return $this->filterPluginsByName('templates', $this->requestedPlugins); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns an array of template files contents as strings |
||
128 | * @param array $names the plugin name strings (foldernames) in an array |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getTemplates($names=null) { |
||
132 | $templateStrings = array(); |
||
133 | $plugins = $this->getPluginsTemplates($names); |
||
134 | foreach ($plugins as $folder => $templates) { |
||
135 | foreach ($templates as $path) { |
||
136 | if (file_exists($path)) { |
||
137 | $filename = explode('/', $path); |
||
138 | $filename = $filename[sizeof($filename)-1]; |
||
139 | $id = $folder . '-' . substr($filename, 0 , (strrpos($filename, "."))); |
||
140 | $templateStrings[$id] = file_get_contents($path); |
||
141 | } |
||
142 | } |
||
143 | } |
||
144 | return $templateStrings; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Returns an array of plugin callback function names |
||
149 | * @param array $names the plugin name strings (foldernames) in an array |
||
150 | * @return array |
||
151 | */ |
||
152 | public function getPluginCallbacks($names=null) { |
||
153 | if ($names) { |
||
154 | $names = array_merge($this->requestedPlugins, $names); |
||
155 | return $this->filterPluginsByName('callback', $names); |
||
156 | } |
||
157 | return $this->filterPluginsByName('callback', $this->requestedPlugins); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Returns a sorted array of javascript function names to call when loading pages |
||
162 | * in order configured with skosmos:vocabularyPlugins |
||
163 | * @return array |
||
164 | */ |
||
165 | public function getCallbacks() { |
||
166 | $ret = array(); |
||
167 | $sortedCallbacks = array(); |
||
168 | $plugins = $this->getPluginCallbacks($this->requestedPlugins); |
||
169 | foreach ($plugins as $callbacks) { |
||
170 | foreach ($callbacks as $callback) { |
||
171 | $split = explode('/', $callback); |
||
172 | $sortedCallbacks[$split[1]] = $split[2]; |
||
173 | } |
||
174 | } |
||
175 | $sortedCallbacks = array_replace($this->pluginOrder, $sortedCallbacks); |
||
176 | foreach ($sortedCallbacks as $callback) { |
||
177 | $ret[] = $callback; |
||
178 | } |
||
179 | return $ret; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Returns an array that is flattened from its possibly multidimensional form |
||
184 | * copied from https://stackoverflow.com/a/1320156 |
||
185 | * @param mixed[] $array Flattens this input array |
||
186 | * @return array Flattened input |
||
187 | */ |
||
188 | protected function flatten($array) { |
||
189 | $return = array(); |
||
190 | array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; }); |
||
191 | return $return; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Returns a flattened array containing the external properties we are interested in saving |
||
196 | * @return string[] |
||
197 | */ |
||
198 | public function getExtProperties() { |
||
199 | return array_unique($this->flatten($this->filterPlugins('ext-properties', true))); |
||
200 | } |
||
201 | } |
||
202 | |||
203 |