protected static function declareServices() { /* Registering default services. */ /** * Autoload */ Autoload::register(); /** * Biome default logger service. */ if (!Biome::hasService('logger')) { Biome::registerService('logger', function () { return new \Psr\Log\NullLogger(); }); } /** * Biome default request. */ if (!Biome::hasService('request')) { Biome::registerService('request', function () { return Request::createFromGlobals(); }); } /** * Biome default lang service. */ if (!Biome::hasService('lang')) { Biome::registerService('lang', function () { $languages = Biome::getService('request')->getLanguages(); $lang = new \Biome\Core\Lang\XMLLang($languages); return $lang; }); } /** * Biome default view service. */ if (!Biome::hasService('view')) { Biome::registerService('view', function () { $view = new \Biome\Core\View(); $app_name = Biome::getService('lang')->get('app_name'); $view->setTitle($app_name); return $view; }); } /** * Biome default rights service. */ if (!Biome::hasService('rights')) { Biome::registerService('rights', function () { $auth = \Biome\Core\Collection::get('auth'); if ($auth->isAuthenticated()) { $admin_id = 1; // Default value of the Admin role id. if (Biome::hasService('config')) { $admin_id = Biome::getService('config')->get('ADMIN_ROLE_ID', 1); } $roles = $auth->user->roles; foreach ($roles as $role) { /* If Admin. */ if ($role->role_id == $admin_id) { return new \Biome\Core\Rights\FreeRights(); } $rights = AccessRights::loadFromJSON($role->role_rights); } return $rights; } $rights = AccessRights::loadFromArray(array()); $rights->setAttribute('User', 'firstname', TRUE, TRUE)->setAttribute('User', 'lastname', TRUE, TRUE)->setAttribute('User', 'mail', TRUE, TRUE)->setAttribute('User', 'password', TRUE, TRUE)->setRoute('GET', 'index', 'index')->setRoute('GET', 'auth', 'login')->setRoute('POST', 'auth', 'signin')->setRoute('POST', 'auth', 'signup'); return $rights; }); } /** * Biome default route service. */ if (!Biome::hasService('router')) { Biome::registerService('router', function () { $router = new Route(); $router->autoroute(); return $router; }); } /** * Biome default dispatch service. */ if (!Biome::hasService('dispatcher')) { Biome::registerService('dispatcher', function () { return Biome::getService('router')->getDispatcher(); }); } Logger::info('Services registered!'); }
/** * This method generate all the route available in the controllers. */ public function autoroute() { /** * Generating all routes. */ $cache = NULL; $classname_routes = NULL; if (\Biome\Biome::hasService('staticcache')) { $cache = \Biome\Biome::getService('staticcache'); $classname_routes = $cache->get('classname_routes'); } if (empty($classname_routes)) { $controllers_dirs = \Biome\Biome::getDirs('controllers'); $controllers_dirs = array_reverse($controllers_dirs); foreach ($controllers_dirs as $dir) { $files = scandir($dir); foreach ($files as $file) { if ($file[0] == '.') { continue; } if (substr($file, -14) != 'Controller.php') { continue; } $controller_name = substr($file, 0, -14); $controller_name = strtolower($controller_name); // Skip if already defined! if (isset($this->classname_routes[$controller_name])) { continue; } $this->classname_routes[$controller_name] = $this->getRoutesFromControllerName($controller_name); } } if (!empty($cache)) { $cache->store('classname_routes', $this->classname_routes); } } else { $this->classname_routes = $classname_routes; } foreach ($this->classname_routes as $controller_name => $actions) { foreach ($actions as $type => $action) { foreach ($action as $name => $meta) { $method = function (Request $request, Response $response, array $args) use($type, $controller_name, $name, $meta) { /* Initialize the controller. */ $ctrl = new $meta['controller']($request, $response); $method_params = array(); foreach ($meta['parameters'] as $param) { switch (strtolower($param['type'])) { case 'biome\\core\\orm\\models': $type_param = $ctrl->objectName(); break; case 'biome\\core\\collection': $type_param = $ctrl->collectionName() . 'Collection'; break; default: $type_param = $param['type']; } $method_params[] = $this->parameterInjection($type_param, $param['name'], $param['required'], $args); } /* Execute the action. */ return $ctrl->process($type, $controller_name, $name, $meta['action'], $method_params); }; $route_path = $meta['path']; $this->addRoute($type, $route_path, $method); $this->routes_list[] = array('method' => $type, 'path' => $route_path); if ($name == 'index') { $route_path = '/' . $controller_name; $this->addRoute($type, $route_path, $method); $this->routes_list[] = array('method' => $type, 'path' => $route_path); } if ($controller_name == 'index' && $name == 'index') { $route_path = '/' . $controller_name . '/' . $name; $this->addRoute($type, $route_path, $method); $this->routes_list[] = array('method' => $type, 'path' => $route_path); } } } } }