1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ffcms\Core\Managers; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Ffcms\Core\App; |
7
|
|
|
use Ffcms\Core\Arch\Controller; |
8
|
|
|
use Ffcms\Core\Helper\FileSystem\Directory; |
9
|
|
|
use Ffcms\Core\Helper\FileSystem\File; |
10
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
11
|
|
|
use Ffcms\Core\Helper\Type\Str; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class BootManager. Manage auto executed boot methods in widgets and applications. |
15
|
|
|
* @package Ffcms\Core\Managers |
16
|
|
|
*/ |
17
|
|
|
class BootManager |
18
|
|
|
{ |
19
|
|
|
CONST CACHE_TREE_TIME = 120; |
20
|
|
|
|
21
|
|
|
private $loader; |
22
|
|
|
private $appRoots = []; |
23
|
|
|
private $widgetRoots = []; |
24
|
|
|
|
25
|
|
|
private $objects = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* BootManager constructor. Pass composer loader inside |
29
|
|
|
* @param bool $loader |
30
|
|
|
*/ |
31
|
|
|
public function __construct($loader = false) |
32
|
|
|
{ |
33
|
|
|
// pass loader inside |
34
|
|
|
$this->loader = $loader; |
35
|
|
|
|
36
|
|
|
if ($this->loader !== false) { |
37
|
|
|
$this->parseComposerLoader(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// try to get bootable class map from cache, or initialize parsing |
41
|
|
|
if (App::$Cache->get('boot.class.map') !== null) { |
42
|
|
|
$this->objects = App::$Cache->get('boot.class.map'); |
43
|
|
|
} else { |
44
|
|
|
$this->compileBootableClasses(); |
45
|
|
|
App::$Cache->set('boot.class.map', $this->objects, static::CACHE_TREE_TIME); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Find app's and widgets root directories over composer psr loader |
51
|
|
|
*/ |
52
|
|
|
private function parseComposerLoader() |
53
|
|
|
{ |
54
|
|
|
// get composer autoload map |
55
|
|
|
$map = $this->loader->getPrefixes(); |
|
|
|
|
56
|
|
|
if (Obj::isArray($map)) { |
57
|
|
|
// get all available apps root dirs by psr loader for apps |
58
|
|
|
if (array_key_exists('Apps\\', $map)) { |
59
|
|
|
foreach ($map['Apps\\'] as $appPath) { |
60
|
|
|
$this->appRoots[] = $appPath; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// get Widgets map |
65
|
|
|
if (array_key_exists('Widgets\\', $map)) { |
66
|
|
|
// get all available root dirs by psr loader for widgets |
67
|
|
|
foreach ($map['Widgets\\'] as $widgetPath) { |
68
|
|
|
$this->widgetRoots[] = $widgetPath; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// set default root path if not found anything else |
74
|
|
|
if (count($this->appRoots) < 1) { |
75
|
|
|
$this->appRoots = [root]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (count($this->widgetRoots) < 1) { |
79
|
|
|
$this->widgetRoots = [root]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Find all bootatble instances and set it to object map |
85
|
|
|
*/ |
86
|
|
|
public function compileBootableClasses() |
87
|
|
|
{ |
88
|
|
|
// list app root's |
89
|
|
View Code Duplication |
foreach ($this->appRoots as $app) { |
|
|
|
|
90
|
|
|
$app .= '/Apps/Controller/' . env_name; |
91
|
|
|
$files = File::listFiles($app, ['.php'], true); |
92
|
|
|
foreach ($files as $file) { |
93
|
|
|
// define full class name with namespace |
94
|
|
|
$class = 'Apps\Controller\\' . env_name . '\\' . Str::cleanExtension($file); |
95
|
|
|
// check if class exists (must be loaded over autoloader), boot method exist and this is controller instanceof |
96
|
|
|
if (class_exists($class) && method_exists($class, 'boot') && is_a($class, 'Ffcms\Core\Arch\Controller', true)) { |
97
|
|
|
$this->objects[] = $class; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// list widget root's |
103
|
|
View Code Duplication |
foreach ($this->widgetRoots as $widget) { |
|
|
|
|
104
|
|
|
$widget .= '/Widgets/' . env_name; |
105
|
|
|
// widgets are packed in directory, classname should be the same with root directory name |
106
|
|
|
$dirs = Directory::scan($widget, GLOB_ONLYDIR, true); |
107
|
|
|
foreach ($dirs as $instance) { |
|
|
|
|
108
|
|
|
$class = 'Widgets\\' . env_name . '\\' . $instance . '\\' . $instance; |
109
|
|
|
if (class_exists($class) && method_exists($class, 'boot') && is_a($class, 'Ffcms\Core\Arch\Widget', true)) { |
110
|
|
|
$this->objects[] = $class; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Call bootable methods in apps and widgets |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function run() |
121
|
|
|
{ |
122
|
|
|
if (!Obj::isArray($this->objects)) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($this->objects as $class) { |
127
|
|
|
forward_static_call([$class, 'boot']); |
128
|
|
|
} |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.