- <?php
- /**
- * @package Chattra.Site
- * @copyright Copyright (c) 2010, The Chattra Team and Johan Dahlberg
- * @link http://www.chattra.com
- * @version Bootstrap.php 00010 2010-07-30 23.39
- * @author Johan Dahlberg <johan.dahlberg@live.se>
- *
- * Status: Needs review
- * Assigned: Johan Dahlberg <johandahlberg1991@gmail.com>
- */
- class Core_Bootstrap {
- private $configPath;
- private $cmods = array();
- /**
- * Never used
- */
- public function __construct() {
- }
- /**
- * Sets the enviroment for the project
- *
- * @param unknown_type $enviroment
- */
- public function setEnviroment($enviroment) {
- $this->_enviroment = $enviroment;
- }
- /**
- * Returning the enviroment
- *
- * @return $_enviroment Returns the enviroment which is currently set
- */
- public function getEnviroment() {
- return $this->_enviroment;
- }
- /**
- * The bootstrap method: Initialize the system
- *
- * @param unknown_type $configPath The path to the config file
- */
- public function bootstrap($configPath) {
- if(!$this->_enviroment) {
- throw new Exception('Please set the enviroment using ::setEnviroment');
- }
- $this->configPath = $configPath;
- if($configPath == '' || !file_exists($configPath)) {
- throw new Exception('Please supply the configuration file path');
- }
- $frontController = $this->initialize();
- $this->setupRoutes($frontController);
- $response = $this->dispatch($frontController);
- $this->render($response);
- }
- /**
- * Initialize neccesary stuff
- */
- public function initialize() {
- // Zend_Loader_Autoloader - Eliminates the need for include/require/require_once
- require_once('Zend/Loader/Autoloader.php');
- $autoloader = Zend_Loader_Autoloader::getInstance();
- $autoloader->registerNamespace('Core_');
- $autoloader->setFallbackAutoloader(true);
- // Load the configuration file
- $config = new Zend_Config_Xml($this->configPath, $this->getEnviroment());
- Zend_Registry::set('config', $config);
- // If Benchmark is active, run the Benchmark class
- if( $config->system->benchmark === true ) {
- $benchmark = new Core_Benchmark();
- }
- // Setting up the frontController
- $frontController = Zend_Controller_Front::getInstance();
- $frontController->throwExceptions((bool) $config->mvc->exceptions);
- // Setting upp modules path
- $moduleFolder = CHATTRA_ROOTPATH . DIRECTORY_SEPARATOR . 'modules';
- $this->loadModules($frontController, $moduleFolder);
- $router = $frontController->getRouter();
- $router->addDefaultRoutes();
- // Add database to the registry
- $params = array('host' => $config->database->hostname,
- 'username' => $config->database->username,
- 'password' => $config->database->password,
- 'dbname' => $config->database->dbname);
- try {
- /**
- * @todo fix so that the adapter setting is loaded from the config file
- */
- $db = Zend_Db::factory($config->database->adapter, $params);
- $db->getConnection();
- Zend_Db_Table_Abstract::setDefaultAdapter($db);
- Zend_Registry::set('db', $db);
- } catch (Zend_Db_Adapter_Exception $e) {
- // Perhaps a failed login credential, or perhaps the RDMS is nott running
- echo "Caught exception: " . get_class($e) . "\n";
- echo "Message: " . $e->getMessage() . "\n";
- }
- // Set Base URL
- Zend_Registry::set('baseUrl', $config->system->url);
- //date_default_timezone_set($config->date_default_timezone);
- // Check if system is in maintenace mode
- if($config->system->status == 'offline') {
- $layoutOptions = array( 'layoutPath' => CHATTRA_ROOTPATH . '/templates/' . $config->general->template . '/',
- 'layout' => 'maintenance');
- } else {
- $layoutOptions = array( 'layoutPath' => CHATTRA_ROOTPATH . '/templates/' . $config->general->template . '/',
- 'layout' => 'default');
- }
- $layout = new Zend_Layout();
- $layout->startMvc($layoutOptions);
- $view = new Zend_View();
- // Setup ACL
- $acl = $this->setACL();
- Zend_Registry::set('ACL', $acl);
- ZendX_JQuery::enableView($view);
- return $frontController;
- }
- /**
- * @param Zend_Controller_Front $frontController
- * @return $router
- */
- public function setupRoutes(Zend_Controller_Front $frontController) {
- // Retrieve the router from the frontController
- $router = $frontController->getRouter();
- return $router;
- }
- /**
- * @param Zend_Controller_Front $frontController
- * @return The reponse
- */
- public function dispatch(Zend_Controller_Front $frontController) {
- $frontController->returnResponse(true);
- return $frontController->dispatch();
- }
- /**
- * @param Zend_Controller_Response_Abstract $response
- */
- public function render(Zend_Controller_Response_Abstract $response) {
- $response->sendHeaders();
- $response->outputBody();
- }
- /**
- * @return ACL
- */
- public function setAcl() {
- $_model = new Acl();
- $roles = $_model->getRoles();
- foreach($roles as $role) {
- $acl_named[$role['id']] = $role['role'];
- }
- $acl = new Zend_Acl();
- foreach($roles as $role) {
- if($role['parent_id'] > 0) {
- $acl->addRole(new Zend_Acl_Role($role['role'], $acl_named[$role['parent_id']]));
- } else {
- $acl->addRole(new Zend_Acl_Role($role['role']));
- }
- $acl->add(new Zend_Acl_Resource($role['role']));
- $acl->allow($role['role'], $role['role']);
- }
- // Ok lets load our settings
- $access = $_model->getAccess();
- foreach($access as $acc) {
- if(!$acl->has($acc['controller'])) {
- $acl->add(new Zend_Acl_Resource($acc['controller']));
- //echo "New ACL {$acc['controller']}<br />";
- }/*
- if($acc['action'] == NULL || $acc['action'] == 'NULL') {
- echo $acc['controller'];
- $acl->allow($acc['role'], $acc['controller']);
- } else {*/
- if($acc['role'] != 'Guests') {
- $acl->deny('Guests', $acc['controller'], $acc['action']);
- }
- $acl->allow($acc['role'], $acc['controller'], $acc['action']);
- //echo "{$acc['role']}, {$acc['controller']}, {$acc['action']}<br />";
- //}
- }
- $acl->allow('Superadmins');
- return $acl;
- }
- /**
- * @param unknown_type $frontController
- * @param unknown_type $folder
- */
- public function loadModules($frontController, $folder) {
- $frontController->addModuleDirectory( $folder );
- if(is_dir($folder)) {
- $listmodules = scandir($folder);
- }
- if(is_array($listmodules)) {
- foreach ($listmodules as $module) {
- if($module[0] == '.') continue;
- if(!in_array($module, $this->cmods)) {
- //$frontController->addControllerDirectory($folder . '/' . $module . '/controllers', $module);
- $this->cmods[] = $module;
- }
- set_include_path($folder
- . DIRECTORY_SEPARATOR . $module
- . PATH_SEPARATOR . get_include_path());
- set_include_path($folder
- . DIRECTORY_SEPARATOR . $module
- . DIRECTORY_SEPARATOR . 'views'
- . PATH_SEPARATOR . get_include_path());
- set_include_path($folder
- . DIRECTORY_SEPARATOR . $module
- . DIRECTORY_SEPARATOR . 'models'
- . PATH_SEPARATOR . get_include_path());
- }
- }
- }
- }