Exemple #1
0
 /**
  * Starts Atomik
  * 
  * If dispatch is false, you will have to manually dispatch the request and exit.
  * 
  * @param string $env A configuration key which will be merged at the root of the store
  * @param string $uri
  * @param bool $dispatch Whether to dispatch
  */
 public static function run($rootDirectory = '.', $env = null, $uri = null, $dispatch = true)
 {
     self::$rootDirectory = $rootDirectory;
     // wrap the whole app inside a try/catch block to catch all errors
     try {
         // config & environment
         try {
             self::set(self::loadConfig(self::path(self::get('atomik.files.config'), null, false), false));
         } catch (AtomikException $e) {
         }
         $env = $env ?: (defined('ATOMIK_ENV') ? ATOMIK_ENV : null);
         if ($env !== null && self::has($env)) {
             self::set(self::get($env));
         }
         self::fireEvent('Atomik::Config');
         // sets the error reporting to all errors if debug mode is on
         if (self::get('atomik.debug', false) == true) {
             error_reporting(E_ALL | E_STRICT);
         }
         // makes relative include dirs relative to app root
         self::set('atomik.dirs.includes', self::path(self::get('atomik.dirs.includes', array())));
         // registers the class autoload handler
         if (self::get('atomik.class_autoload', true) == true) {
             if (!function_exists('spl_autoload_register')) {
                 throw new AtomikException("Missing 'spl_autoload_register()' function");
             }
             spl_autoload_register('Atomik::needed');
         }
         // cleans the plugins array
         $plugins = array();
         foreach (self::get('plugins', array()) as $key => $value) {
             if (!is_string($key)) {
                 $key = $value;
                 $value = array();
             }
             $plugins[ucfirst($key)] = (array) $value;
         }
         self::set('plugins', $plugins, false);
         // loads plugins
         // this method allows plugins that are being loaded to modify the plugins array
         $loadedPlugins = array();
         while (count($pluginsToLoad = array_diff(array_keys(self::get('plugins')), self::getLoadedPlugins(), $loadedPlugins)) > 0) {
             foreach ($pluginsToLoad as $plugin) {
                 if (self::loadPlugin($plugin) === false) {
                     $loadedPlugins[] = $plugin;
                 }
             }
         }
         self::fireEvent('Atomik::Bootstrap');
         if ($filename = self::path(self::get('atomik.files.bootstrap'))) {
             include $filename;
         }
         $cancel = false;
         self::fireEvent('Atomik::Start', array(&$cancel));
         if ($cancel) {
             self::end(true);
         }
         if (!self::has('atomik.url_rewriting')) {
             self::set('atomik.url_rewriting', isset($_SERVER['REDIRECT_URL']) || isset($_SERVER['REDIRECT_URI']));
         }
         if ($dispatch) {
             self::dispatch($uri);
             self::end(true);
         }
     } catch (Exception $e) {
         $cancel = false;
         self::fireEvent('Atomik::Error', array($e, &$cancel));
         if (!$cancel) {
             throw $e;
         }
     }
 }