function prepareMenu(array &$menu_list) { $is_active = FALSE; $rights = \Biome\Biome::getService('rights'); foreach ($menu_list as $index => &$menu) { $url = isset($menu['url']) ? $menu['url'] : ''; $menu['class'] = isset($menu['class']) ? $menu['class'] : ''; $menu['subclass'] = isset($menu['subclass']) ? $menu['subclass'] : ''; /** * Check if the menu is allowed. */ if (!empty($url) && !$rights->isUrlAllowed('GET', $url)) { unset($menu_list[$index]); continue; } if (URL::matchRequest($url)) { $menu['class'] .= 'active'; $is_active = TRUE; } if (!empty($menu['submenu'])) { if (prepareMenu($menu['submenu'])) { $menu['subclass'] .= 'in'; $is_active = TRUE; } /* Remove the menu if no other menu is inside. */ if (empty($menu['submenu'])) { unset($menu_list[$index]); } } } unset($menu); return $is_active; }
public function load($controller, $action) { /** * Opening template file */ $dirs = \Biome\Biome::getDirs('views'); $template_file = ''; foreach ($dirs as $dir) { $path = $dir . '/' . $controller . '.xml'; if (!file_exists($path)) { continue; } $template_file = $path; } if (!file_exists($template_file)) { throw new \Biome\Core\View\Exception\TemplateNotFoundException('Missing template file for ' . $controller . '->' . $action); } $tree = TemplateReader::loadFilename($template_file); View\Component::$view = $this; if ($tree['value'] instanceof \Biome\Component\ViewsComponent) { $node = $tree['value']; $node->load($action); $this->_tree = $node; } }
private static function classloader($class_name, $type) { $dirs = \Biome\Biome::getDirs($type); if (empty($dirs)) { return FALSE; } $filename = ''; foreach ($dirs as $d) { if (!file_exists($d)) { continue; } $files = scandir($d); foreach ($files as $f) { if ($f[0] == '.') { continue; } if (strtolower($f) == strtolower($class_name) . '.php') { $filename = $d . '/' . $f; } } } if (file_exists($filename)) { Logger::debug('Load class in ' . $filename); include_once $filename; return TRUE; } return FALSE; }
public function isAllowed() { $rights = \Biome\Biome::getService('rights'); $controller = $this->getAttribute('controller', 'index'); $action = $this->getAttribute('action', 'index'); return $rights->isRouteAllowed('GET', $controller, $action); }
public static function getObjects() { $modelsDirs = \Biome\Biome::getDirs('models'); /** * List existings models. */ $objects_list = array(); foreach ($modelsDirs as $dir) { if (!file_exists($dir)) { continue; } $filenames = scandir($dir); foreach ($filenames as $file) { if ($file[0] == '.') { continue; } $object_name = substr($file, 0, -4); $objects_list[$object_name] = $object_name; } } $objects = array(); foreach ($objects_list as $object_name) { $objects[$object_name] = self::get($object_name); } return $objects; }
/** * @description List all the routes. */ public function listRoutes() { $router = \Biome\Biome::getService('router'); $routes = $router->routes_list; foreach ($routes as $route) { echo $route['method'], ' ', $route['path'], PHP_EOL; } }
public static function __get($type) { switch ($type) { case 'string': $lang = \Biome\Biome::getService('lang'); return $lang; default: throw new Exception('Unrecognized type of resources!'); } }
public function __destruct() { $class_name = get_class($this); $view_state = Biome::getService('view')->getViewState(); // Store to session. $data = $this->serialize(); if (!isset($_SESSION['collections_req'])) { $_SESSION['collections_req'] = array(); } $_SESSION['collections_req'][$class_name][$view_state] = $data; }
public function postAuthorizations(RolesCollection $c) { $r = \Biome\Biome::getService('request'); $rights = $r->get('rights'); $c->role->role_rights = json_encode($rights); if ($c->role->save()) { $this->flash()->success('@string/role_update_success'); } else { $this->flash()->error('@string/role_update_failure', join(', ', $c->role->getErrors())); } return $this->response()->redirect(); }
public function redirect($controller = NULL, $action = NULL, $item = NULL, $module = NULL) { if ($controller === NULL) { $url = \Biome\Biome::getService('request')->headers->get('referer'); } else { $url = \URL::fromRoute($controller, $action, $item, $module); } Logger::info('Redirect to ' . $url); $this->headers->set('Location', $url); $this->setStatusCode(302); return $this; }
protected function load() { $lang_dirs = \Biome\Biome::getDirs('resources'); $reverse_locales = array_reverse($this->locales); foreach ($lang_dirs as $dir) { /** * Read default language file. */ $locale_dir = $dir . '/string/'; if (!file_exists($locale_dir)) { continue; } $files = scandir($locale_dir); foreach ($files as $file) { if ($file[0] == '.') { continue; } if (substr($file, -4) != '.xml') { continue; } $xmlFile = $locale_dir . '/' . $file; $this->read($xmlFile); } /** * Read locale language file. */ foreach ($reverse_locales as $locale) { $raw = explode('_', $locale); $locale = $raw[0]; $locale_dir = $dir . '/string/' . $locale . '/'; if (!file_exists($locale_dir)) { continue; } $files = scandir($locale_dir); foreach ($files as $file) { if ($file[0] == '.') { continue; } if (substr($file, -4) != '.xml') { continue; } $xmlFile = $locale_dir . '/' . $file; $this->read($xmlFile); } } } }
public static function loadFilename($filename) { $xml_contents = file_get_contents($filename); $reader = new Reader(); /** * Loading components */ $components_list = array(); $components = scandir(__DIR__ . '/../../Component/'); foreach ($components as $file) { if ($file[0] == '.') { continue; } if (substr($file, -4) != '.php') { continue; } $componentName = substr($file, 0, -strlen('Component.php')); $components_list['{http://github.com/mermetbt/Biome/}' . strtolower($componentName)] = 'Biome\\Component\\' . $componentName . 'Component'; } $components_dirs = \Biome\Biome::getDirs('components'); $components_dirs = array_reverse($components_dirs); foreach ($components_dirs as $dir) { if (!file_exists($dir)) { continue; } $components = scandir($dir); foreach ($components as $file) { if ($file[0] == '.') { continue; } if (substr($file, -4) != '.php') { continue; } $componentName = substr($file, 0, -strlen('Component.php')); $components_list['{http://github.com/mermetbt/Biome/}' . strtolower($componentName)] = $componentName . 'Component'; } } $reader->elementMap = $components_list; /** * Parsing XML template */ $reader->xml($xml_contents); $tree = $reader->parse(); return $tree; }
public static function init() { error_reporting(E_ALL); if (Config\Config::get('WHOOPS_ERROR', FALSE) || Config\Config::get('DEBUG', FALSE)) { $whoops = new \Whoops\Run(); $request = \Biome\Biome::getService('request'); if ($request->acceptHtml()) { $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler()); } else { if (\Whoops\Util\Misc::isCommandLine()) { $whoops->pushHandler(new \Whoops\Handler\PlainTextHandler()); } else { $whoops->pushHandler(new \Whoops\Handler\JsonResponseHandler()); } } $whoops->register(); } }
public function process($type, $controller_name, $action_name, $method_name, $method_params) { Logger::info('Processing method ' . $method_name); $this->_call_params['http_method_type'] = $type; $this->_call_params['controller_name'] = $controller_name; $this->_call_params['action_name'] = $action_name; $this->_call_params['method_name'] = $method_name; $this->_call_params['method_params'] = $method_params; /** * preRoute */ if (!$this->beforeRoute()) { return $this->response(); } $rendering = $type == 'GET' ? TRUE : FALSE; $this->view = \Biome\Biome::getService('view'); try { $this->view->load($controller_name, $action_name); } catch (\Biome\Core\View\Exception\TemplateNotFoundException $e) { $rendering = FALSE; } $result = call_user_func_array(array($this, $method_name), $method_params); if ($result instanceof Response) { $this->response = $result; } // Ajax Request if ($this->request->isXmlHttpRequest()) { $rendering = FALSE; $partial_rendering = $this->request->get('partial'); if ($partial_rendering) { $this->response->setContentType('application/json'); $this->view->ajaxHandle($partial_rendering); } } if ($rendering && !$this->response->isRedirection()) { // Render view $content = $this->view->render(); if (!empty($content)) { $this->response->setContent($content); } } $this->response = $this->afterRoute($this->response); return $this->response; }
public function renderComponent() { $components_dirs = \Biome\Biome::getDirs('components'); $components_dirs[] = __DIR__ . '/../../Component/'; $components_dirs = array_reverse($components_dirs); foreach ($components_dirs as $dir) { if (file_exists($dir . '/templates/' . $this->_nodename . '.php')) { $component_template_file = $dir . '/templates/' . $this->_nodename . '.php'; } } if (file_exists($component_template_file)) { ob_start(); $view = self::$view; include $component_template_file; $content = ob_get_contents(); ob_end_clean(); } else { throw new \Exception('Template file not found: ' . $component_template_file . ' for component ' . get_called_class() . ' - ' . $this->_fullname); } return $content; }
/** * @description Create the Administrator role with all the access rights. */ public function createAdminRole() { try { $admin = Role::get(1); $this->output->writeln('The administrator role already exists!'); } catch (\Exception $e) { $admin = new Role(); $admin->role_id = 1; } $rights = \Biome\Biome::getService('rights'); /** * Routes allowing. */ $router = \Biome\Biome::getService('router'); foreach ($router->getRoutes() as $route) { $method = $route['method']; $controller = $route['controller']; $action = $route['action']; $rights->setRoute($method, $controller, $action); } /** * Objects allowing. */ $objects = \Biome\Core\ORM\ObjectLoader::getObjects(); foreach ($objects as $object_name => $object) { $rights->setObject($object_name); foreach ($object->getFieldsName() as $attribute_name) { $rights->setAttribute($object_name, $attribute_name); } } $admin->role_name = 'Administrator'; $admin->role_rights = $rights->exportToJSON(); if (!$admin->save()) { $this->output->writeln('Unable to create the administrator role!'); $this->output->writeln(print_r($admin->getErrors(), TRUE)); return FALSE; } $this->output->writeln('Administrator role created!'); return TRUE; }
public function building() { $filename = $this->getAttribute('src'); $dirs = \Biome\Biome::getDirs('views'); $template_file = ''; foreach ($dirs as $dir) { $path = $dir . '/' . $filename; if (!file_exists($path)) { continue; } $template_file = $path; } if (!file_exists($template_file)) { throw new \Exception('Unable to load template file: ' . $filename); } $nodes = TemplateReader::loadFilename($template_file); // print_r($nodes); // // echo '<br/>'; // print_r($this->value); // die(); $this->_value = $nodes['value']->_value; return TRUE; }
<?php $objects = \Biome\Core\ORM\ObjectLoader::getObjects(); $router = \Biome\Biome::getService('router'); $routes = $router->getRoutes(); $rights = \Biome\Core\Rights\AccessRights::loadFromJSON($this->getValue()); ?> <div id="<?php echo $this->getId(); ?> "> <ul class="nav nav-tabs" role="tablist"> <li role="presentation" class="active"><a href="#role_routes" aria-controls="role_routes" role="tab" data-toggle="tab">Routes</a></li> <li role="presentation"><a href="#role_objects" aria-controls="role_objects" role="tab" data-toggle="tab">Objects</a></li> </ul> <div class="tab-content"> <div role="tabpanel" class="tab-pane fade in active" id="role_routes"> <table id="table_routes" class="table table-striped table-hover table-condensed"> <thead> <tr> <th>Method</th> <th>Route</th> <th>Description</th> <th><input type="checkbox" onClick="toggleCheckboxColumn(this, 'table_routes', 'rights-routes');"/> <span> Access</span></th> </tr> </thead> <tbody> <?php foreach ($routes as $r) { $method = $r['method'];
<?php $field = $this->getField(); $value = $this->getValue(); $rights = \Biome\Biome::getService('rights'); $viewable = $field === NULL || $rights->isAttributeView($field); if ($viewable) { if ($field !== NULL && $this->getType() == 'enum') { $enumeration = $field->getEnumeration(); if (isset($enumeration[$value])) { echo $enumeration[$value]; } } else { echo $value; } } else { ?> <i class="fa fa-ban"></i><?php }
protected function parameterInjection($type, $name, $required, $args) { $request = \Biome\Biome::getService('request'); $value = NULL; if (empty($type)) { return $args[$name]; } switch ($type) { // Default PHP type case 'string': case 'int': return $value; break; default: // Class injection } /** * Collection injection */ if (substr($type, -strlen('Collection')) == 'Collection') { // Instanciate the collection $collection_name = strtolower(substr($type, 0, -strlen('Collection'))); $value = Collection::get($collection_name); // Check if data are sent foreach ($request->request->keys() as $key) { if (strncmp($collection_name . '/', $key, strlen($collection_name . '/')) == 0) { $raw = explode('/', $key); $total = count($raw); $iter = $value; for ($i = 1; $i < $total - 1; $i++) { $iter = $iter->{$raw[$i]}; } $v = $request->request->get($key); $iter->{$raw[$i]} = $v; } } } else { $object_name = strtolower($type); $value = ObjectLoader::get($object_name); // Check if data are sent foreach ($request->request->keys() as $key) { if (strncmp($object_name . '/', $key, strlen($object_name . '/')) == 0) { $raw = explode('/', $key); $total = count($raw); $iter = $value; for ($i = 1; $i < $total - 1; $i++) { $iter = $iter->{$raw[$i]}; } $v = $request->request->get($key); $iter->{$raw[$i]} = $v; } } } return $value; }
public static function getRequest() { return \Biome\Biome::getService('request'); }
<?php /** * Activate the autoload. */ require_once __DIR__ . '/../vendor/autoload.php'; Biome\Biome::registerDirs(array('commands' => __DIR__ . '/../src/app/commands/', 'controllers' => __DIR__ . '/../src/app/controllers/', 'models' => [__DIR__ . '/../src/app/models/', __DIR__ . '/models/'], 'views' => __DIR__ . '/../src/app/views/', 'components' => __DIR__ . '/../src/app/components/', 'collections' => __DIR__ . '/../src/app/collections/', 'resources' => __DIR__ . '/../src/resources/'), TRUE); Biome\Biome::registerAlias(array('URL' => 'Biome\\Core\\URL')); Biome\Biome::registerService('logger', function () { return new \Biome\Core\Logger\Handler\FileLogger(__DIR__ . '/../biome-tests.log'); }); Biome\Biome::registerService('mysql', function () { $DB = Biome\Core\ORM\Connector\MySQLConnector::getInstance(); $DB->setConnectionParameters(array('hostname' => 'localhost', 'username' => 'test', 'password' => 'test', 'database' => 'biome')); return $DB; }); \Biome\Biome::tests();
public static function get($attribute, $default_value = NULL) { return Biome::getService('config')->get($attribute, $default_value); }
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!'); }
/** * Detailed debug information. * * @param string $message * @param array $context * * @return null */ public static function debug($message, array $context = array()) { Biome::getService('logger')->debug($message, $context); }
/** * From the longest to the smallest. */ protected function rec_fetchValue($var, &$inner_context = 'global') { $ctx = NULL; $result = $this->getContext($var, $ctx); if ($result !== NULL) { return $result; } /* Remove one item, save the name of the last one. */ $raw = explode('.', $var); if (count($raw) > 1) { $end = array_pop($raw); $result = $this->rec_fetchValue(join('.', $raw), $ctx); /* We find the preceding item, fetch the next. */ if (method_exists($result, 'get' . $end)) { $end = 'get' . $end; $result = $result->{$end}(); } else { if (method_exists($result, $end)) { $result = $result->{$end}(); } else { if (is_array($result) && isset($result[$end])) { $result = $result[$end]; } else { $result = $result->{$end}; } } } $this->setContext($var, $result, $ctx); return $result; } /* No item found, check view. */ $view = \Biome\Biome::getService('view'); $result = $view->{$raw[0]}; if ($result !== NULL) { return $result; } /* No item found, check collections. */ $result = Collection::get($raw[0]); if ($result !== NULL) { return $result; } /* No item found, check objects. */ $result = ObjectLoader::get($raw[0]); if ($result !== NULL) { return $result; } throw new \Exception('Unable to find the variable ' . $var . ' in the context!'); }
/** * Query generation. */ protected function db() { return Biome::getService('mysql'); }
<?php $enctype = $this->getEnctype(); $type = ''; if (!empty($enctype)) { $type = ' enctype="' . $enctype . '"'; } ?> <form class="<?php echo $this->getClasses(); ?> " method="POST" action="<?php echo $this->getAction(); ?> "<?php echo $type; ?> > <input type="hidden" name="_token" value="<?php echo \Biome\Biome::getService('view')->getViewState(); ?> "/> <?php echo $this->getContent(); ?> </form>