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.
- *
- * Note: Do not use this class directly. Instead use its child class {@link PhpBakery}
- * where you can customize all methods.
- *
- * @package system
- * @author Michael Beers <[email protected]>
- * @copyright (c) 2012, Michael Beers | Online Media en Design
- * @todo logger methods
- */
- class Bootstrap
- {
- /**
- * Class map used to load all framework functionalities very fast.
- *
- * Format: Class name => File path
- *
- * @var array
- */
- public static $coreClassMap = array(
- 'PhpBakery\Exception' => 'Exception.php',
- 'PhpBakery\Web' => 'Web.php',
- );
- /**
- * Class map used to load other functionalities very fast.
- *
- * Format: Class name => File path
- *
- * @var array
- */
- public static $classMap = array();
- /**
- * Alias paths used to shorten all paths.
- *
- * Format: Alias => Path
- *
- * @var array
- */
- public static $aliases = array();
- /**
- * Class map used to check what resources are already loaded.
- *
- * @var array
- */
- public static $resources = array();
- /**
- * Include paths to search in for files
- *
- * @var array
- */
- public static $paths = array();
- /**
- * Whether to rely on the PHP include paths at the autoloader.
- *
- * Note: You may set this to false if your hosting environment does not support this.
- *
- * @var boolean
- */
- private static $_enableIncludePaths = TRUE;
- /**
- * Instance of the registered application.
- *
- * @var object
- */
- private static $_app;
- /**
- * Instance of the registered logger.
- *
- * @var object
- */
- private static $_logger;
- /* Constructor and class helpers.
- ===================================================== */
- /**
- * Constructor.
- */
- public function __construct()
- {
- //Set default aliases
- self::$aliases['system'] = SYS_PATH;
- //Register the autoloader
- spl_autoload_register( array($this, 'autoloader') );
- }
- /**
- * Creates a web-based application instance.
- *
- * Note: This is a function shortner for registerApplication().
- *
- * @param mixed $params Application configurations.
- * @return object
- */
- public static function runWebApplication($params = NULL)
- {
- return self::registerApplication('PhpBakery\Web', $params);
- }
- /**
- * Creates a console-based application instance.
- *
- * Note: This is a function shortner for registerApplication().
- *
- * @param mixed $params Application configurations.
- * @return object
- */
- public static function runConsoleApplication($params = NULL)
- {
- return self::registerApplication('PhpBakery\Console', $params);
- }
- /**
- * Returns the registered application instance.
- *
- * Note: This is function shortner for getApplication().
- *
- * @return object
- */
- public static function app()
- {
- return self::getApplication();
- }
- /* Application methods
- ===================================================== */
- /**
- * Creates a new application instance.
- *
- * @param string $className Class name of the application.
- * @param mixed $params Application configurations.
- * @return object
- */
- public static function registerApplication($className, $params = NULL)
- {
- return new $className($params);
- }
- /**
- * Returns the registered application instance.
- *
- * @return object
- */
- public static function getApplication()
- {
- return self::$_app;
- }
- /**
- * Set the application instance.
- *
- * Note: If the parameter is null, the existing application
- * will be removed.
- *
- * @param object $obj Application instance.
- */
- public static function setApplication($obj)
- {
- if (self::$_app === NULL || $obj === NULL)
- {
- self::$_app = $obj;
- }
- else
- {
- //Throw exception
- Throw new PhpBakery\Exception('Application instance can only be created once.');
- }
- }
- /* Autoloader methods
- ===================================================== */
- /**
- * Register a new class autoloader.
- *
- * @param mixed $callback Valid php callback (function name or array)
- * @param boolean $append Whether to append the new autoloader after the default autoloader.
- */
- public static function registerAutoloader($callback, $append = FALSE)
- {
- if ($append)
- {
- self::$_enableIncludePaths = FALSE;
- spl_autoload_register($callback);
- }
- else
- {
- spl_autoload_unregister( array($this, 'autoloader') );
- spl_autoload_register($callback);
- spl_autoload_register( array($this, 'autoloader') );
- }
- }
- /**
- * Class autoloader.
- *
- * Note: This method provided to be invoked within an __autoload() magic method.
- *
- * @param string $className Class name to be loaded.
- * @return boolean
- */
- public static function autoloader($className)
- {
- if (isset(self::$coreClassMap[$className]))
- {
- //Get class from the core class map.
- include(SYS_PATH . self::$coreClassMap[$className]);
- }
- else if (isset(self::$classMap[$className]))
- {
- //Get class from the normal class map.
- include(self::$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
- ===================================================== */
- /**
- * Creates an new object an initialize it based on the given configurations.
- *
- * @param mixed $params Resource configurations. (String or Array)
- * @return object
- */
- public static 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
- Throw new PhpBakery\Exception('Resource configurations must contain a string or array with the key "class".');
- }
- //Check whether a class exists else import
- if (!class_exists($class, TRUE))
- {
- self::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;
- }
- /**
- * Import a resource class or directory.
- *
- * @param string $alias Alias path to the file
- * @param boolean $force Whether to include the file immediately.
- * @return string
- */
- public static 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.
- //
- }
- /**
- * Returns a path by the given alias.
- *
- * @param string $alias Alias from the path.
- * @return mixed
- */
- public static function getAlias($alias)
- {
- if (isset(self::$aliases[$alias]))
- {
- //Get alias from alias cache
- return self::$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(self::$aliases[$root_alias]))
- {
- //Get root alias from alias cache
- return self::$aliases[$root_alias] = rtrim(self::$aliases[$root_alias] . DS . str_replace('.', DS, substr($alias, $pos+1)), '*', DS);
- }
- //
- // Other options can be placed here like finding modules etc.
- //
- }
- return FALSE;
- }
- /**
- * Set a new alias path.
- *
- * Note: This method checks the existing path and normalizes it.
- *
- * @param string $alias Alias from the path.
- * @param string $path Path corresponding to the alias.
- */
- public static function setAlias($alias, $path = NULL)
- {
- if (empty($path))
- {
- unset(self::$aliases[$alias]);
- }
- else
- {
- self::$aliases[$alias] = ltrim($path, '\\/');
- }
- }
- /* Logging methods
- ===================================================== */
- }
Advertisement
Add Comment
Please, Sign In to add comment