Guest User

Untitled

a guest
Dec 17th, 2012
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Bootstrap for serving all common framework functionality.
  4.  *
  5.  * @package system
  6.  * @author Michael Beers <[email protected]>
  7.  * @copyright (c) 2012, Michael Beers | Online Media en Design
  8.  */
  9.  
  10. //Set the namespace
  11. namespace PhpBakery;
  12.  
  13. /**
  14.  * Bootstrap for serving all common framework functionality.
  15.  *
  16.  * @package system
  17.  * @author Michael Beers <[email protected]>
  18.  * @copyright (c) 2012, Michael Beers | Online Media en Design
  19.  * @todo docblocks
  20.  * @todo throw exceptions
  21.  * @todo create functions
  22.  * @todo logger methods
  23.  */
  24. class Bootstrap
  25. {
  26.     public $coreClassMap = array();
  27.    
  28.     public $classMap = array();
  29.    
  30.     public $aliases = array();
  31.    
  32.     public $resources = array();
  33.    
  34.     public $paths = array();
  35.    
  36.     private $_enableIncludePaths = TRUE;
  37.    
  38.     private $_app;
  39.    
  40.     private $_logger;
  41.    
  42.     /* Constructor and class helpers.
  43.     ===================================================== */
  44.    
  45.     public function __construct()
  46.     {
  47.         //Set default aliases
  48.         $this->aliases['system'] = SYS_PATH;
  49.        
  50.         //Register the autoloader
  51.         spl_autoload_register( array($this, 'autoloader') );
  52.     }
  53.    
  54.     public function runWebApplication($params = NULL)
  55.     {
  56.         return $this->registerApplication('PhpBakery\Web', $params);
  57.     }
  58.    
  59.     public function runConsoleApplication($params = NULL)
  60.     {
  61.         return $this->registerApplication('PhpBakery\Console', $params);
  62.     }
  63.    
  64.     public function app()
  65.     {
  66.         return $this->getApplication();
  67.     }
  68.    
  69.     /* Application methods
  70.     ===================================================== */   
  71.    
  72.     public function registerApplication($className, $params = NULL)
  73.     {
  74.         return new $className($params);
  75.     }
  76.    
  77.     public function getApplication()
  78.     {
  79.         return $this->_app;
  80.     }
  81.    
  82.     public function setApplication($obj)
  83.     {
  84.         if ($this->_app === NULL || $obj === NULL)
  85.         {
  86.             $this->_app = $obj;
  87.         }
  88.         else
  89.         {
  90.             //Throw exception
  91.         }
  92.     }
  93.            
  94.     /* Autoloader methods
  95.     ===================================================== */
  96.    
  97.     public function registerAutoloader($callback, $append = FALSE)
  98.     {
  99.         if ($append)
  100.         {
  101.             $this->_enableIncludePaths = FALSE;
  102.             spl_autoload_register($callback);
  103.         }
  104.         else
  105.         {
  106.             spl_autoload_unregister( array($this, 'autoloader') );
  107.             spl_autoload_register($callback);
  108.             spl_autoload_register( array($this, 'autoloader') );
  109.         }
  110.     }
  111.    
  112.     public function autoloader($className)
  113.     {
  114.         if (isset($this->coreClassMap[$className]))
  115.         {
  116.             //Get class from the core class map.
  117.             include(SYS_PATH . $this->coreClassMap[$className]);
  118.         }
  119.         else if (isset($this->classMap[$className]))
  120.         {
  121.             //Get class from the normal class map.
  122.             include($this->classMap[$className]);
  123.         }
  124.         else
  125.         {
  126.         //
  127.         // Dont know how to do this for now, first make the core classes etc.
  128.         // This function is not needed then.
  129.         //             
  130.         }
  131.        
  132.         return TRUE;
  133.     }
  134.    
  135.     /* Resource methods
  136.     ===================================================== */
  137.    
  138.     public function registerResource($params)
  139.     {
  140.         //Format parameters
  141.         if (is_string($params))
  142.         {
  143.             $class = $params;
  144.             $params = array();
  145.         }
  146.         else if (isset($params['class']))
  147.         {
  148.             $class = $params['class'];
  149.             unset($params['class']);
  150.         }
  151.         else
  152.         {
  153.             //Throw exception
  154.         }
  155.        
  156.         //Check whether a class exists else import
  157.         if (!class_exists($class, TRUE))
  158.         {
  159.             $this->importResource($class, TRUE);
  160.         }
  161.        
  162.         //Register the component self
  163.         if ( func_num_args() > 1 )
  164.         {
  165.             $args = func_get_args();
  166.             unset($args[0]);
  167.            
  168.             $resource_reflection = new ReflectionClass($class);
  169.             $resource = call_user_func_array(array($resource_reflection, 'newInstance'), $args);
  170.         }
  171.         else
  172.         {
  173.             $resource = new $class();
  174.         }
  175.        
  176.         return $resource;
  177.     }
  178.    
  179.     public function importResource($alias, $force = FALSE)
  180.     {
  181.         //
  182.         // Dont know how to do this for now, first make the core classes etc.
  183.         // This function is not needed then.
  184.         //
  185.     }
  186.    
  187.     public function getAlias($alias)
  188.     {
  189.         if (isset($this->aliases[$alias]))
  190.         {
  191.             //Get alias from alias cache
  192.             return $this->aliases[$alias];
  193.         }
  194.         else if ( ($pos = strpos($alias, '.')) !== FALSE )
  195.         {
  196.             //Check whether a alias is having a root to return
  197.             $root_alias = substr($alias, 0, $pos);
  198.            
  199.             if (isset($this->aliases[$root_alias]))
  200.             {
  201.                 //Get root alias from alias cache
  202.                 return $this->aliases[$root_alias] = rtrim($this->aliases[$root_alias] . DS . str_replace('.', DS, substr($alias, $pos+1)), '*', DS);
  203.             }
  204.            
  205.             //
  206.             // Other options can be placed here like finding modules etc.
  207.             //
  208.            
  209.         }
  210.        
  211.         return FALSE;
  212.     }
  213.    
  214.     public function setAlias($alias, $path = NULL)
  215.     {
  216.         if (empty($path))
  217.         {
  218.             unset($this->aliases[$alias]);
  219.         }
  220.         else
  221.         {
  222.             $this->aliases[$alias] = ltrim($path, '\\/');
  223.         }
  224.     }
  225.    
  226.     /* Logging methods
  227.     ===================================================== */
  228.    
  229. }
Advertisement
Add Comment
Please, Sign In to add comment