Esempio n. 1
0
File: Hook.php Progetto: hjr3/titon
 /**
  * Cycles through the hooks for the specified event, and executes the related method.
  * If a scope is defined, and the hook doesn't match the scope, it will be bypassed.
  *
  * @access public
  * @param string $event
  * @param object $Object    - Object passed by reference: Controller, View, etc
  * @return void
  * @static
  */
 public static function execute($event, $Object = null)
 {
     if (!empty(self::$__hooks[$event])) {
         $route = Router::current();
         foreach (self::$__hooks[$event] as $slug => &$hook) {
             if ($hook['executed'] === true) {
                 continue;
             }
             // Check to see if the hook is restricted to a certain scope
             if (isset(self::$__scopes[$event][$slug])) {
                 $scope = self::$__scopes[$event][$slug];
                 foreach (array('container', 'controller', 'action') as $action) {
                     if ($scope[$action] != $route[$action] || $scope[$action] != '*') {
                         continue;
                     }
                 }
             }
             if (isset(self::$__objectMap[$slug])) {
                 $obj = self::$__objectMap[$slug];
                 if ($obj instanceof Closure) {
                     $obj($Object);
                 } else {
                     if (method_exists($obj, $event)) {
                         $obj->{$event}($Object);
                     }
                 }
             }
             $hook['executed'] = true;
         }
     }
 }
Esempio n. 2
0
 /**
  * Loads the $_POST, $_FILES data, configures the query params and populates the accepted headers fields.
  *
  * @access public
  * @return void
  */
 public function initialize()
 {
     if ($this->_initialized) {
         return;
     }
     parent::initialize();
     // Store data
     $this->data =& App::$data;
     $this->params = Router::current();
     // Store accept HTTP headers
     foreach (array('Accept', 'Accept-Language', 'Accept-Charset') as $acception) {
         if ($accept = $this->env($acception)) {
             $accept = explode(',', $accept);
             foreach ($accept as $type) {
                 if (strpos($type, ';') !== false) {
                     list($type, $quality) = explode(';', $type);
                 } else {
                     $quality = 1;
                 }
                 $data = array('type' => $type, 'quality' => str_replace('q=', '', $quality));
                 if ($acception == 'Accept-Language') {
                     $this->__acceptLangs[] = $data;
                 } else {
                     if ($acception == 'Accept-Charset') {
                         $this->__acceptCharsets[] = $data;
                     } else {
                         $this->__acceptTypes[] = $data;
                     }
                 }
             }
         }
     }
 }
Esempio n. 3
0
 /**
  * Initialize dispatch and detects if a custom dispatcher should be used within the current scope.
  * If no scope is defined, the default dispatcher will be instantiated.
  *
  * @access public
  * @return void
  * @static
  */
 public static function initialize()
 {
     $params = Router::current();
     $dispatch = null;
     if (!empty(self::$__mapping)) {
         // Specific controller and container
         if (isset(self::$__mapping[$params['container'] . '.' . $params['controller']])) {
             $dispatch = self::$__mapping[$params['container'] . '.' . $params['controller']];
             // All controllers within a specific container
         } else {
             if (isset(self::$__mapping[$params['container'] . '.*'])) {
                 $dispatch = self::$__mapping[$params['container'] . '.*'];
                 // Specific controller within any container
             } else {
                 if (isset(self::$__mapping['*.' . $params['controller']])) {
                     $dispatch = self::$__mapping['*.' . $params['controller']];
                     // Apply to all controllers and containers
                 } else {
                     if (isset(self::$__mapping['*.*'])) {
                         $dispatch = self::$__mapping['*.*'];
                     }
                 }
             }
         }
     }
     if ($dispatch) {
         $Dispatcher = $dispatch($params);
     } else {
         switch (Environment::detect()) {
             case 'development':
                 $Dispatcher = new \titon\modules\dispatchers\front\FrontDev($params);
                 break;
             default:
                 $Dispatcher = new \titon\modules\dispatchers\front\Front($params);
                 break;
         }
     }
     if ($Dispatcher instanceof \titon\modules\dispatchers\DispatcherInterface) {
         $Dispatcher->run();
         exit;
     }
     throw new Exception(sprintf('%s Dispatcher must implement the \\titon\\modules\\dispatchers\\DispatcherInterface.', $dispatch));
 }