Repository navigation
Expand file tree
/
Copy pathModule.php
More file actions
135 lines (126 loc) · 2.76 KB
/
Copy pathModule.php
File metadata and controls
135 lines (126 loc) · 2.76 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* @author : JIHAD SINNAOUR
* @package : FloatPHP
* @subpackage : Kernel Component
* @version : 1.0.1
* @category : PHP framework
* @copyright : (c) 2017 - 2023 Jihad Sinnaour <mail@jihadsinnaour.com>
* @link : https://www.floatphp.com
* @license : MIT
*
* This file if a part of FloatPHP Framework.
*/
declare(strict_types=1);
namespace FloatPHP\Kernel;
use FloatPHP\Classes\Filesystem\{
File, Json
};
use FloatPHP\Helpers\Framework\{
Configurator, Validator, Permission
};
class Module extends BaseController
{
/**
* @param void
*/
public function __construct()
{
// Init configuration
$this->initConfig();
// Load modules
$this->loadModules();
}
/**
* @access public
* @param mixed $roles
* @return bool
*/
public function hasPermissions($roles = false) : bool
{
if ( $this->isPermissions() ) {
if ( $roles ) {
return Permission::hasRole($roles);
}
}
return true;
}
/**
* Get modules routes.
*
* @access public
* @param void
* @param array
*/
public function getModulesRoutes()
{
$wrapper = [];
if ( $this->getModules() ) {
foreach ( $this->getModules() as $key => $name ) {
$config = Json::parse("{$name}/module.json", true);
if ( $config['enable'] == true ) {
foreach ($config['router'] as $route) {
$wrapper[] = $route;
}
}
}
}
return $this->applyFilter('module-routes', $wrapper);
}
/**
* Add module js.
*
* @access protected
* @param string $js
* @param string $hook
* @return void
*/
protected function addJS($js, $hook = 'add-js')
{
$this->addAction($hook, function() use($js) {
$tpl = $this->applyFilter('module-view-js', 'system/js');
$this->render(['js' => "{$this->getModulesUrl()}/{$js}"],$tpl);
});
}
/**
* Add module css.
*
* @access protected
* @param string $css
* @param string $hook
* @return void
*/
protected function addCSS($css, $hook = 'add-css')
{
$this->addAction($hook, function() use($css){
$tpl = $this->applyFilter('module-view-css', 'system/css');
$this->render(['css' => "{$this->getModulesUrl()}/{$css}"],$tpl);
});
}
/**
* Get modules routes.
*
* @access private
* @param void
* @param array
*/
private function loadModules()
{
foreach ( $this->getModules() as $name ) {
$config = Json::parse("{$name}/module.json");
Validator::checkModuleConfig($config);
if ( $config->migrate ) {
$configurator = new Configurator();
$configurator->migrate("{$name}/migrate");
}
if ( $config->enable ) {
$basename = basename($name);
$namespace = "{$this->getModuleNamespace()}{$basename}\\{$basename}";
if ( File::exists("{$name}/{$basename}Module.php") ) {
$module = "{$namespace}Module";
new $module;
}
}
}
}
}