Example #1
0
File: App.php Project: pagon/core
 /**
  * App will run
  */
 public function run()
 {
     // Check if run
     if ($this->_run) {
         throw new \RuntimeException("Application already running");
     }
     // Save current app
     self::$self = $this;
     // Emit run
     $this->emit('run');
     // Set run
     $this->_run = true;
     $_error = false;
     $_path = $this->input->path();
     if ($this->injectors['error']) {
         // If config error, register error handle and set flag
         $_error = true;
         $this->registerErrorHandler();
     }
     try {
         // Emit "bundle" event
         $this->emit('bundle');
         /**
          * Loop bundles and lookup the bundle info and start the bundle
          */
         foreach ($this->injectors['bundles'] as $id => $options) {
             // Set id
             $id = isset($options['id']) ? $options['id'] : $id;
             // Set bootstrap file
             $bootstrap = isset($options['bootstrap']) ? $options['bootstrap'] : 'bootstrap.php';
             // Set dir to load
             $dir = isset($options['dir']) ? $options['dir'] : 'bundles/' . $id;
             // Path check, if not match start of path, skip
             if (isset($options['path']) && strpos($_path, $options['path']) !== 0) {
                 continue;
             }
             // Check the file path
             if (!($file = $this->path($dir . '/' . $bootstrap))) {
                 throw new \InvalidArgumentException('Bundle "' . $id . '" can not bootstrap');
             }
             // Check if bootstrap file loaded
             if (isset(self::$loads[$file])) {
                 throw new \RuntimeException('Bundle "' . $id . '" can not bootstrap twice');
             }
             // Emit "bundle.[id]" event
             $this->emit('bundle.' . $id);
             // Set variable for bootstrap file
             $app = $this;
             extract($options);
             require $file;
             // Save to loads
             self::$loads[$file] = true;
         }
         // Start buffer
         if ($this->injectors['buffer']) {
             ob_start();
         }
         if (!in_array(array('', $this->router, array()), $this->injectors['stacks'])) {
             $this->injectors['stacks'][] = $this->router;
         }
         // Emit "middleware" event
         $this->emit('middleware');
         // Process the stacks
         if (!$this->router->handle($this->injectors['stacks'], function ($stack) use($_path) {
             // Try to match the path
             if (is_array($stack) && $stack[0] && strpos($_path, $stack[0]) === false) {
                 return false;
             }
             if (!is_array($stack)) {
                 return $stack;
             } else {
                 return Middleware::build($stack[1], $stack[2]);
             }
         })) {
             $this->handleError('404');
         }
         // Write direct output to the head of buffer
         if ($this->injectors['buffer']) {
             $this->output->write(ob_get_clean());
         }
     } catch (Exception\Stop $e) {
     } catch (\Exception $e) {
         if ($this->injectors['debug']) {
             throw $e;
         } else {
             try {
                 $this->handleError('exception', $e);
             } catch (Exception\Stop $e) {
             }
         }
         $this->emit('error');
     }
     $this->_run = false;
     // Send start
     $this->emit('flush');
     // Flush
     $this->flush();
     // Send end
     $this->emit('end');
     if ($_error) {
         $this->restoreErrorHandler();
     }
 }