1: <?php
2: /**
3: * @author Jefferson González
4: * @license MIT
5: * @link http://github.com/peg-org/peg-src Source code.
6: */
7:
8: namespace Peg\Lib\Plugins;
9:
10: /**
11: * A basic plugin loader.
12: */
13: class Loader
14: {
15:
16: /**
17: * A list of loaded plugins.
18: * @var \Peg\Lib\Plugins\Base[]
19: */
20: public $plugins;
21:
22: /**
23: * Create the plugins loader object.
24: */
25: public function __construct()
26: {
27: $this->plugins = array();
28: }
29:
30: /**
31: * Scans a given path for valid plugin files and load them if possible.
32: * This method should be called after everything else in the application
33: * has been properly initialized.
34: * @param string $path
35: */
36: public function Start($path)
37: {
38: if(!is_dir($path))
39: return;
40:
41: $path = rtrim($path, "/\\") . "/";
42:
43: $elements = scandir($path);
44:
45: foreach($elements as $file_name)
46: {
47: if(is_file($path . $file_name))
48: {
49: $file_parts = explode(".", $file_name, 2);
50:
51: if(count($file_parts) < 2)
52: continue;
53:
54: $class_name = $file_parts[0];
55: $class_name_ns = "Peg\Lib\\Plugins\\$class_name";
56: $file_extension = $file_parts[1];
57:
58: if($file_extension == "php")
59: {
60: include($path . $file_name);
61:
62: if(in_array($class_name_ns, get_declared_classes()))
63: {
64: $this->plugins[$class_name] = new $class_name_ns;
65:
66: $this->plugins[$class_name]->name = $class_name;
67:
68: $this->plugins[$class_name]->path = $path . $file_name;
69:
70: $this->plugins[$class_name]->OnInit();
71: }
72: }
73: }
74: }
75: }
76:
77: }