* @link : https://floatphp.com * @license : MIT * * This file if a part of FloatPHP Framework. */ declare(strict_types=1); namespace FloatPHP\Kernel; use FloatPHP\Helpers\Framework\{ Installer, Validator }; class Module extends BaseController { /** * @uses initConfig() */ public function __construct() { // Init configuration $this->initConfig(); // Load modules $this->loadModules(); } /** * Check permission. * * @access public * @param string $role * @return bool */ public function hasPermissions(?string $role = null) : bool { if ( $this->isPermissions() ) { if ( $role ) { return $this->hasRole($role); } } return true; } /** * Get modules routes. * * @access public * @param array */ public function getModulesRoutes() { $wrapper = []; if ( $this->getModules() ) { foreach ( $this->getModules() as $key => $name ) { $config = $this->parseJson("{$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 $path * @param string $hook * @return void */ protected function addJS(string $path, string $hook = 'add-js') { $this->addAction($hook, function() use($path) { $file = $this->applyFilter('module-view-js', 'system/js'); $this->render($file, ['js' => "{$this->getModulesUrl()}/{$path}"]); }); } /** * Add module css. * * @access protected * @param string $path * @param string $hook * @return void */ protected function addCSS(string $path, string $hook = 'add-css') { $this->addAction($hook, function() use($path){ $file = $this->applyFilter('module-view-css', 'system/css'); $this->render($file, ['css' => "{$this->getModulesUrl()}/{$path}"]); }); } /** * Get modules routes. * * @access private * @param array */ private function loadModules() { foreach ( $this->getModules() as $name ) { $config = $this->parseJson("{$name}/module.json"); Validator::checkModuleConfig($config); if ( $config->migrate ) { (new Installer())->migrate("{$name}/migrate"); } if ( $config->enable ) { $basename = $this->basename($name); $namespace = "{$this->getModuleNamespace()}{$basename}\\{$basename}"; if ( $this->hasFile("{$name}/{$basename}Module.php") ) { $module = "{$namespace}Module"; new $module; } } } } }