forked from fossar/selfoss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpoutLoader.php
More file actions
101 lines (80 loc) · 2.72 KB
/
SpoutLoader.php
File metadata and controls
101 lines (80 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?PHP
namespace helpers;
/**
* Helper class for loading spouts (special spouts which
* defines an spout for this application)
*
* @package helpers
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <tobias.zeising@aditu.de>
*/
class SpoutLoader {
/**
* array of available spouts
*
* @var bool|array
*/
public $spouts = false;
/**
* returns all available spouts
*
* @return array available spouts
*/
public function all() {
$this->readSpouts();
return $this->spouts;
}
/**
* returns a given spout object
*
* @return mixed|boolean an instance of the spout, false if this spout doesn't exist
* @param string $spout a given spout type
*/
public function get($spout) {
$this->readSpouts();
if(!array_key_exists($spout, $this->spouts))
return false;
else
return $this->spouts[$spout];
}
//
// private helpers
//
/**
* reads all spouts
*
* @return void
*/
protected function readSpouts() {
if($this->spouts===false)
$this->spouts = $this->loadClass('spouts', 'spouts\spout');
}
/**
* returns all classes which extends a given class
*
* @return array with classname (key) and an instance of a class (value)
* @param string $location the path where all spouts in
* @param string $parentclass the parent class which files must extend
*/
protected function loadClass($location, $parentclass) {
$return = array();
foreach(scandir($location) as $dir) {
if(is_dir($location . '/' . $dir) && substr($dir,0,1)!=".") {
// search for spouts
foreach(scandir($location . "/" . $dir) as $file) {
// only scan visible .php files
if(is_file($location . "/" . $dir . "/" . $file) && substr($file,0,1)!="." && strpos($file,".php")!==false) {
// create reflection class
$classname = $location."\\".$dir."\\".str_replace(".php","",$file);
$class = new \ReflectionClass($classname);
// register widget
if($class->isSubclassOf(new \ReflectionClass($parentclass)))
$return[$classname] = $class->newInstance();
}
}
}
}
return $return;
}
}