Guest User

Untitled

a guest
Dec 17th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.  * Note: Do not use this class directly. Instead use its child class {@link PhpBakery}
  17.  * where you can customize all methods.
  18.  *
  19.  * @package system
  20.  * @author Michael Beers <[email protected]>
  21.  * @copyright (c) 2012, Michael Beers | Online Media en Design
  22.  * @todo logger methods
  23.  */
  24. class Bootstrap
  25. {
  26.     /**
  27.      * Class map used to load all framework functionalities very fast.
  28.      *
  29.      * Format: Class name => File path
  30.      *
  31.      * @var array
  32.      */
  33.     public static $coreClassMap = array(
  34.         'PhpBakery\Exception' => 'Exception.php',
  35.         'PhpBakery\Web' => 'Web.php',
  36.     );
  37.    
  38.     /**
  39.      * Class map used to load other functionalities very fast.
  40.      *
  41.      * Format: Class name => File path
  42.      *
  43.      * @var array
  44.      */
  45.     public static $classMap = array();
  46.    
  47.     /**
  48.      * Alias paths used to shorten all paths.
  49.      *
  50.      * Format: Alias => Path
  51.      *
  52.      * @var array
  53.      */
  54.     public static $aliases = array();
  55.    
  56.     /**
  57.      * Class map used to check what resources are already loaded.
  58.      *
  59.      * @var array
  60.      */
  61.     public static $resources = array();
  62.    
  63.     /**
  64.      * Include paths to search in for files
  65.      *
  66.      * @var array
  67.      */
  68.     public static $paths = array();
  69.    
  70.     /**
  71.      * Whether to rely on the PHP include paths at the autoloader.
  72.      *
  73.      * Note: You may set this to false if your hosting environment does not support this.
  74.      *
  75.      * @var boolean
  76.      */
  77.     private static $_enableIncludePaths = TRUE;
  78.    
  79.     /**
  80.      * Instance of the registered application.
  81.      *
  82.      * @var object
  83.      */
  84.     private static $_app;
  85.    
  86.     /**
  87.      * Instance of the registered logger.
  88.      *
  89.      * @var object
  90.      */
  91.     private static $_logger;
  92.    
  93.     /* Constructor and class helpers.
  94.     ===================================================== */
  95.    
  96.     /**
  97.      * Constructor.
  98.      */
  99.     public function __construct()
  100.     {
  101.         //Set default aliases
  102.         self::$aliases['system'] = SYS_PATH;
  103.        
  104.         //Register the autoloader
  105.         spl_autoload_register( array($this, 'autoloader') );
  106.     }
  107.    
  108.     /**
  109.      * Creates a web-based application instance.
  110.      *
  111.      * Note: This is a function shortner for registerApplication().
  112.      *
  113.      * @param mixed $params Application configurations.
  114.      * @return object
  115.      */
  116.     public static function runWebApplication($params = NULL)
  117.     {
  118.         return self::registerApplication('PhpBakery\Web', $params);
  119.     }
  120.    
  121.     /**
  122.      * Creates a console-based application instance.
  123.      *
  124.      * Note: This is a function shortner for registerApplication().
  125.      *
  126.      * @param mixed $params Application configurations.
  127.      * @return object
  128.      */
  129.     public static function runConsoleApplication($params = NULL)
  130.     {
  131.         return self::registerApplication('PhpBakery\Console', $params);
  132.     }
  133.    
  134.     /**
  135.      * Returns the registered application instance.
  136.      *
  137.      * Note: This is function shortner for getApplication().
  138.      *
  139.      * @return object
  140.      */
  141.     public static function app()
  142.     {
  143.         return self::getApplication();
  144.     }
  145.    
  146.     /* Application methods
  147.     ===================================================== */   
  148.    
  149.     /**
  150.      * Creates a new application instance.
  151.      *
  152.      * @param string $className Class name of the application.
  153.      * @param mixed $params Application configurations.
  154.      * @return object
  155.      */
  156.     public static function registerApplication($className, $params = NULL)
  157.     {
  158.         return new $className($params);
  159.     }
  160.    
  161.     /**
  162.      * Returns the registered application instance.
  163.      *
  164.      * @return object
  165.      */
  166.     public static function getApplication()
  167.     {
  168.         return self::$_app;
  169.     }
  170.    
  171.     /**
  172.      * Set the application instance.
  173.      *
  174.      * Note: If the parameter is null, the existing application
  175.      * will be removed.
  176.      *
  177.      * @param object $obj Application instance.
  178.      */
  179.     public static function setApplication($obj)
  180.     {
  181.         if (self::$_app === NULL || $obj === NULL)
  182.         {
  183.             self::$_app = $obj;
  184.         }
  185.         else
  186.         {
  187.             //Throw exception
  188.             Throw new PhpBakery\Exception('Application instance can only be created once.');
  189.         }
  190.     }
  191.            
  192.     /* Autoloader methods
  193.     ===================================================== */
  194.    
  195.     /**
  196.      * Register a new class autoloader.
  197.      *
  198.      * @param mixed $callback Valid php callback (function name or array)
  199.      * @param boolean $append Whether to append the new autoloader after the default autoloader.
  200.      */
  201.     public static function registerAutoloader($callback, $append = FALSE)
  202.     {
  203.         if ($append)
  204.         {
  205.             self::$_enableIncludePaths = FALSE;
  206.             spl_autoload_register($callback);
  207.         }
  208.         else
  209.         {
  210.             spl_autoload_unregister( array($this, 'autoloader') );
  211.             spl_autoload_register($callback);
  212.             spl_autoload_register( array($this, 'autoloader') );
  213.         }
  214.     }
  215.    
  216.     /**
  217.      * Class autoloader.
  218.      *
  219.      * Note: This method provided to be invoked within an __autoload() magic method.
  220.      *
  221.      * @param string $className Class name to be loaded.
  222.      * @return boolean
  223.      */
  224.     public static function autoloader($className)
  225.     {
  226.         if (isset(self::$coreClassMap[$className]))
  227.         {
  228.             //Get class from the core class map.
  229.             include(SYS_PATH . self::$coreClassMap[$className]);
  230.         }
  231.         else if (isset(self::$classMap[$className]))
  232.         {
  233.             //Get class from the normal class map.
  234.             include(self::$classMap[$className]);
  235.         }
  236.         else
  237.         {
  238.             //
  239.             // Dont know how to do this for now, first make the core classes etc.
  240.             // This function is not needed then.
  241.             //             
  242.         }
  243.        
  244.         return TRUE;
  245.     }
  246.    
  247.     /* Resource methods
  248.     ===================================================== */
  249.    
  250.     /**
  251.      * Creates an new object an initialize it based on the given configurations.
  252.      *
  253.      * @param mixed $params Resource configurations. (String or Array)
  254.      * @return object
  255.      */
  256.     public static function registerResource($params)
  257.     {
  258.         //Format parameters
  259.         if (is_string($params))
  260.         {
  261.             $class = $params;
  262.             $params = array();
  263.         }
  264.         else if (isset($params['class']))
  265.         {
  266.             $class = $params['class'];
  267.             unset($params['class']);
  268.         }
  269.         else
  270.         {
  271.             //Throw exception
  272.             Throw new PhpBakery\Exception('Resource configurations must contain a string or array with the key "class".');
  273.         }
  274.        
  275.         //Check whether a class exists else import
  276.         if (!class_exists($class, TRUE))
  277.         {
  278.             self::importResource($class, TRUE);
  279.         }
  280.        
  281.         //Register the component self
  282.         if ( func_num_args() > 1 )
  283.         {
  284.             $args = func_get_args();
  285.             unset($args[0]);
  286.            
  287.             $resource_reflection = new ReflectionClass($class);
  288.             $resource = call_user_func_array(array($resource_reflection, 'newInstance'), $args);
  289.         }
  290.         else
  291.         {
  292.             $resource = new $class();
  293.         }
  294.        
  295.         return $resource;
  296.     }
  297.    
  298.     /**
  299.      * Import a resource class or directory.
  300.      *
  301.      * @param string $alias Alias path to the file
  302.      * @param boolean $force Whether to include the file immediately.
  303.      * @return string
  304.      */
  305.     public static function importResource($alias, $force = FALSE)
  306.     {
  307.         //
  308.         // Dont know how to do this for now, first make the core classes etc.
  309.         // This function is not needed then.
  310.         //
  311.     }
  312.    
  313.     /**
  314.      * Returns a path by the given alias.
  315.      *
  316.      * @param string $alias Alias from the path.
  317.      * @return mixed
  318.      */
  319.     public static function getAlias($alias)
  320.     {
  321.         if (isset(self::$aliases[$alias]))
  322.         {
  323.             //Get alias from alias cache
  324.             return self::$aliases[$alias];
  325.         }
  326.         else if ( ($pos = strpos($alias, '.')) !== FALSE )
  327.         {
  328.             //Check whether a alias is having a root to return
  329.             $root_alias = substr($alias, 0, $pos);
  330.            
  331.             if (isset(self::$aliases[$root_alias]))
  332.             {
  333.                 //Get root alias from alias cache
  334.                 return self::$aliases[$root_alias] = rtrim(self::$aliases[$root_alias] . DS . str_replace('.', DS, substr($alias, $pos+1)), '*', DS);
  335.             }
  336.            
  337.             //
  338.             // Other options can be placed here like finding modules etc.
  339.             //
  340.            
  341.         }
  342.        
  343.         return FALSE;
  344.     }
  345.    
  346.     /**
  347.      * Set a new alias path.
  348.      *
  349.      * Note: This method checks the existing path and normalizes it.
  350.      *
  351.      * @param string $alias Alias from the path.
  352.      * @param string $path Path corresponding to the alias.
  353.      */
  354.     public static function setAlias($alias, $path = NULL)
  355.     {
  356.         if (empty($path))
  357.         {
  358.             unset(self::$aliases[$alias]);
  359.         }
  360.         else
  361.         {
  362.             self::$aliases[$alias] = ltrim($path, '\\/');
  363.         }
  364.     }
  365.    
  366.     /* Logging methods
  367.     ===================================================== */
  368.    
  369. }
Advertisement
Add Comment
Please, Sign In to add comment