Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 7.75 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * @package         Chattra.Site
  4.  * @copyright       Copyright (c) 2010, The Chattra Team and Johan Dahlberg
  5.  * @link            http://www.chattra.com
  6.  * @version         Bootstrap.php 00010 2010-07-30 23.39
  7.  * @author          Johan Dahlberg <johan.dahlberg@live.se>
  8.  *
  9.  * Status:          Needs review
  10.  * Assigned:        Johan Dahlberg <johandahlberg1991@gmail.com>
  11.  */
  12.  
  13. class Core_Bootstrap {
  14.   private $configPath;
  15.   private $cmods = array();
  16.  
  17.   /**
  18.    * Never used
  19.    */
  20.   public function __construct() {
  21.    
  22.   }
  23.    
  24.   /**
  25.    * Sets the enviroment for the project
  26.    *
  27.    * @param unknown_type $enviroment
  28.    */
  29.   public function setEnviroment($enviroment) {
  30.     $this->_enviroment = $enviroment;
  31.   }
  32.  
  33.   /**
  34.    * Returning the enviroment
  35.    *
  36.    * @return $_enviroment Returns the enviroment which is currently set
  37.    */
  38.   public function getEnviroment() {
  39.     return $this->_enviroment;
  40.   }
  41.  
  42.   /**
  43.    * The bootstrap method: Initialize the system
  44.    *
  45.    * @param unknown_type $configPath The path to the config file
  46.    */
  47.   public function bootstrap($configPath) {
  48.     if(!$this->_enviroment) {
  49.       throw new Exception('Please set the enviroment using ::setEnviroment');
  50.     }
  51.    
  52.     $this->configPath = $configPath;
  53.     if($configPath == '' || !file_exists($configPath)) {
  54.       throw new Exception('Please supply the configuration file path');
  55.     }
  56.    
  57.     $frontController = $this->initialize();
  58.      
  59.     $this->setupRoutes($frontController);
  60.     $response = $this->dispatch($frontController);
  61.    
  62.     $this->render($response);
  63.   }
  64.    
  65.  
  66.   /**
  67.    * Initialize neccesary stuff
  68.    */
  69.   public function initialize() {
  70.     // Zend_Loader_Autoloader - Eliminates the need for include/require/require_once
  71.     require_once('Zend/Loader/Autoloader.php');
  72.     $autoloader = Zend_Loader_Autoloader::getInstance();
  73.     $autoloader->registerNamespace('Core_');
  74.     $autoloader->setFallbackAutoloader(true);
  75.    
  76.     // Load the configuration file
  77.     $config = new Zend_Config_Xml($this->configPath, $this->getEnviroment());
  78.     Zend_Registry::set('config', $config);
  79.    
  80.     // If Benchmark is active, run the Benchmark class
  81.     if( $config->system->benchmark === true ) {
  82.       $benchmark = new Core_Benchmark();
  83.     }
  84.    
  85.     // Setting up the frontController
  86.     $frontController = Zend_Controller_Front::getInstance();
  87.     $frontController->throwExceptions((bool) $config->mvc->exceptions);
  88.    
  89.     // Setting upp modules path
  90.     $moduleFolder = CHATTRA_ROOTPATH . DIRECTORY_SEPARATOR . 'modules';
  91.     $this->loadModules($frontController, $moduleFolder);
  92.    
  93.     $router = $frontController->getRouter();
  94.     $router->addDefaultRoutes();
  95.    
  96.     // Add database to the registry
  97.     $params = array('host'    => $config->database->hostname,
  98.             'username'  => $config->database->username,
  99.             'password'  => $config->database->password,
  100.             'dbname'  => $config->database->dbname);
  101.     try {
  102.       /**
  103.        * @todo fix so that the adapter setting is loaded from the config file
  104.        */
  105.       $db = Zend_Db::factory($config->database->adapter, $params);
  106.       $db->getConnection();
  107.       Zend_Db_Table_Abstract::setDefaultAdapter($db);
  108.       Zend_Registry::set('db', $db);
  109.     } catch (Zend_Db_Adapter_Exception $e) {
  110.       // Perhaps a failed login credential, or perhaps the RDMS is nott running
  111.       echo "Caught exception: " . get_class($e) . "\n";
  112.       echo "Message: " . $e->getMessage() . "\n";
  113.     }
  114.    
  115.     // Set Base URL
  116.     Zend_Registry::set('baseUrl', $config->system->url);
  117.     //date_default_timezone_set($config->date_default_timezone);
  118.    
  119.     // Check if system is in maintenace mode
  120.     if($config->system->status == 'offline') {
  121.       $layoutOptions = array( 'layoutPath'  => CHATTRA_ROOTPATH . '/templates/' . $config->general->template . '/',
  122.                   'layout'    => 'maintenance');
  123.     } else {
  124.       $layoutOptions = array( 'layoutPath'  => CHATTRA_ROOTPATH . '/templates/' . $config->general->template . '/',
  125.                   'layout'    => 'default');
  126.     }
  127.     $layout = new Zend_Layout();
  128.     $layout->startMvc($layoutOptions);
  129.     $view = new Zend_View();
  130.    
  131.     // Setup ACL
  132.     $acl = $this->setACL();
  133.     Zend_Registry::set('ACL', $acl);
  134.     ZendX_JQuery::enableView($view);
  135.      
  136.     return $frontController;
  137.   }
  138.    
  139.   /**
  140.    * @param Zend_Controller_Front $frontController
  141.    * @return $router
  142.    */
  143.   public function setupRoutes(Zend_Controller_Front $frontController) {
  144.     // Retrieve the router from the frontController
  145.     $router = $frontController->getRouter();
  146.    
  147.     return $router;
  148.   }
  149.  
  150.   /**
  151.    * @param Zend_Controller_Front $frontController
  152.    * @return The reponse
  153.    */
  154.   public function dispatch(Zend_Controller_Front $frontController) {
  155.     $frontController->returnResponse(true);
  156.     return $frontController->dispatch();
  157.   }
  158.  
  159.   /**
  160.    * @param Zend_Controller_Response_Abstract $response
  161.    */
  162.   public function render(Zend_Controller_Response_Abstract $response) {
  163.     $response->sendHeaders();
  164.     $response->outputBody();
  165.   }
  166.  
  167.   /**
  168.    * @return ACL
  169.    */
  170.   public function setAcl() {
  171.     $_model = new Acl();
  172.     $roles = $_model->getRoles();
  173.     foreach($roles as $role) {
  174.       $acl_named[$role['id']] = $role['role'];
  175.     }
  176.                
  177.     $acl = new Zend_Acl();
  178.     foreach($roles as $role) {
  179.       if($role['parent_id'] > 0) {
  180.           $acl->addRole(new Zend_Acl_Role($role['role'], $acl_named[$role['parent_id']]));
  181.         } else {
  182.           $acl->addRole(new Zend_Acl_Role($role['role']));
  183.         }
  184.         $acl->add(new Zend_Acl_Resource($role['role']));
  185.         $acl->allow($role['role'], $role['role']);
  186.     }
  187.                
  188.     // Ok lets load our settings
  189.     $access = $_model->getAccess();
  190.                
  191.     foreach($access as $acc) {
  192.       if(!$acl->has($acc['controller'])) {
  193.           $acl->add(new Zend_Acl_Resource($acc['controller']));
  194.             //echo "New ACL {$acc['controller']}<br />";
  195.         }/*
  196.         if($acc['action'] == NULL || $acc['action'] == 'NULL') {
  197.           echo $acc['controller'];
  198.             $acl->allow($acc['role'], $acc['controller']);
  199.         } else {*/
  200.           if($acc['role'] != 'Guests') {
  201.               $acl->deny('Guests', $acc['controller'], $acc['action']);
  202.             }
  203.             $acl->allow($acc['role'], $acc['controller'], $acc['action']);
  204.             //echo "{$acc['role']}, {$acc['controller']}, {$acc['action']}<br />";
  205.         //}
  206.     }
  207.  
  208.     $acl->allow('Superadmins');
  209.     return $acl;
  210.   }
  211.    
  212.   /**
  213.    * @param unknown_type $frontController
  214.    * @param unknown_type $folder
  215.    */
  216.   public function loadModules($frontController, $folder) {
  217.      $frontController->addModuleDirectory( $folder );
  218.          
  219.     if(is_dir($folder)) {
  220.       $listmodules = scandir($folder);
  221.     }
  222.    
  223.     if(is_array($listmodules)) {
  224.       foreach ($listmodules as $module) {
  225.         if($module[0] == '.') continue;
  226.         if(!in_array($module, $this->cmods)) {
  227.           //$frontController->addControllerDirectory($folder . '/' . $module . '/controllers', $module);
  228.           $this->cmods[] = $module;
  229.         }
  230.        
  231.         set_include_path($folder
  232.                   . DIRECTORY_SEPARATOR . $module
  233.                   . PATH_SEPARATOR . get_include_path());        
  234.         set_include_path($folder
  235.                   . DIRECTORY_SEPARATOR . $module
  236.                   . DIRECTORY_SEPARATOR . 'views'
  237.                   . PATH_SEPARATOR . get_include_path());
  238.         set_include_path($folder
  239.                   . DIRECTORY_SEPARATOR . $module
  240.                   . DIRECTORY_SEPARATOR . 'models'
  241.                   . PATH_SEPARATOR . get_include_path());
  242.       }
  243.     }
  244.   }
  245. }