Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Bootstrap for serving all common framework functionality.
- *
- * @package system
- * @author Michael Beers <[email protected]>
- * @copyright (c) 2012, Michael Beers | Online Media en Design
- */
- //Set the namespace
- namespace PhpBakery;
- /**
- * Bootstrap for serving all common framework functionality.
- *
- * @package system
- * @author Michael Beers <[email protected]>
- * @copyright (c) 2012, Michael Beers | Online Media en Design
- * @todo docblocks
- * @todo throw exceptions
- * @todo create functions
- * @todo logger methods
- */
- class Bootstrap
- {
- public $coreClassMap = array();
- public $classMap = array();
- public $aliases = array();
- public $resources = array();
- public $paths = array();
- private $_enableIncludePaths = TRUE;
- private $_app;
- private $_logger;
- /* Constructor and class helpers.
- ===================================================== */
- public function __construct()
- {
- //Set default aliases
- $this->aliases['system'] = SYS_PATH;
- //Register the autoloader
- spl_autoload_register( array($this, 'autoloader') );
- }
- public function runWebApplication($params = NULL)
- {
- return $this->registerApplication('PhpBakery\Web', $params);
- }
- public function runConsoleApplication($params = NULL)
- {
- return $this->registerApplication('PhpBakery\Console', $params);
- }
- public function app()
- {
- return $this->getApplication();
- }
- /* Application methods
- ===================================================== */
- public function registerApplication($className, $params = NULL)
- {
- return new $className($params);
- }
- public function getApplication()
- {
- return $this->_app;
- }
- public function setApplication($obj)
- {
- if ($this->_app === NULL || $obj === NULL)
- {
- $this->_app = $obj;
- }
- else
- {
- //Throw exception
- }
- }
- /* Autoloader methods
- ===================================================== */
- public function registerAutoloader($callback, $append = FALSE)
- {
- if ($append)
- {
- $this->_enableIncludePaths = FALSE;
- spl_autoload_register($callback);
- }
- else
- {
- spl_autoload_unregister( array($this, 'autoloader') );
- spl_autoload_register($callback);
- spl_autoload_register( array($this, 'autoloader') );
- }
- }
- public function autoloader($className)
- {
- if (isset($this->coreClassMap[$className]))
- {
- //Get class from the core class map.
- include(SYS_PATH . $this->coreClassMap[$className]);
- }
- else if (isset($this->classMap[$className]))
- {
- //Get class from the normal class map.
- include($this->classMap[$className]);
- }
- else
- {
- //
- // Dont know how to do this for now, first make the core classes etc.
- // This function is not needed then.
- //
- }
- return TRUE;
- }
- /* Resource methods
- ===================================================== */
- public function registerResource($params)
- {
- //Format parameters
- if (is_string($params))
- {
- $class = $params;
- $params = array();
- }
- else if (isset($params['class']))
- {
- $class = $params['class'];
- unset($params['class']);
- }
- else
- {
- //Throw exception
- }
- //Check whether a class exists else import
- if (!class_exists($class, TRUE))
- {
- $this->importResource($class, TRUE);
- }
- //Register the component self
- if ( func_num_args() > 1 )
- {
- $args = func_get_args();
- unset($args[0]);
- $resource_reflection = new ReflectionClass($class);
- $resource = call_user_func_array(array($resource_reflection, 'newInstance'), $args);
- }
- else
- {
- $resource = new $class();
- }
- return $resource;
- }
- public function importResource($alias, $force = FALSE)
- {
- //
- // Dont know how to do this for now, first make the core classes etc.
- // This function is not needed then.
- //
- }
- public function getAlias($alias)
- {
- if (isset($this->aliases[$alias]))
- {
- //Get alias from alias cache
- return $this->aliases[$alias];
- }
- else if ( ($pos = strpos($alias, '.')) !== FALSE )
- {
- //Check whether a alias is having a root to return
- $root_alias = substr($alias, 0, $pos);
- if (isset($this->aliases[$root_alias]))
- {
- //Get root alias from alias cache
- return $this->aliases[$root_alias] = rtrim($this->aliases[$root_alias] . DS . str_replace('.', DS, substr($alias, $pos+1)), '*', DS);
- }
- //
- // Other options can be placed here like finding modules etc.
- //
- }
- return FALSE;
- }
- public function setAlias($alias, $path = NULL)
- {
- if (empty($path))
- {
- unset($this->aliases[$alias]);
- }
- else
- {
- $this->aliases[$alias] = ltrim($path, '\\/');
- }
- }
- /* Logging methods
- ===================================================== */
- }
Advertisement
Add Comment
Please, Sign In to add comment