コード例 #1
0
ファイル: Router.php プロジェクト: adrchw/simpletools
 public function __destruct()
 {
     if ($this->_routingEvents) {
         \Simpletools\Events\Event::fire('routerDestruct');
     }
 }
コード例 #2
0
ファイル: Controller.php プロジェクト: adrchw/simpletools
 public function forward($controller, $action = null, $params = false)
 {
     $this->_forwarded = true;
     $incontroller = $controller;
     if ($action) {
         $inaction = $action;
     } else {
         $inaction = $controller;
         $incontroller = $this->getParam('controller');
     }
     if ($action === null) {
         if (stripos($controller, '.') !== false || stripos($controller, '-') !== false || stripos($controller, ' ') !== false) {
             $controller = self::getCorrectActionName($controller);
         } else {
             $controller = lcfirst($controller);
         }
         $action = $controller;
         $controller = self::getCorrectControllerName($this->getParam('controller'));
     } else {
         if (stripos($controller, '.') !== false || stripos($controller, '-') !== false || stripos($controller, ' ') !== false) {
             $controller = self::getCorrectControllerName($controller);
         } else {
             $controller = ucfirst($controller);
         }
         if (stripos($action, '.') !== false || stripos($action, '-') !== false || stripos($action, ' ') !== false) {
             $action = self::getCorrectActionName($action);
         } else {
             $action = lcfirst($action);
         }
     }
     $this->setNewParams($incontroller, $inaction, $params);
     $this->_autoRender = true;
     if ($controller == 'error' || $action == 'error') {
         $this->_errorCode = 'custom error';
     }
     $_c = false;
     $namespace = $this->_activeRoutingNamespace;
     $orgController = $controller;
     $n = substr($controller, 0, 1);
     if ($n == '\\' or $n == '/') {
         $controller = trim(str_replace('/', '\\', $controller), '\\');
         $_path = explode('\\', $controller);
         $controller = array_pop($_path);
         $namespace = implode('\\', $_path);
     }
     $className = !$namespace ? $controller . 'Controller' : $namespace . "\\" . $controller . 'Controller';
     if ($namespace && strtolower($controller) == 'error') {
         $path = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . '/' . $controller . 'Controller.php';
         $path = $this->_appDir . '/controllers/' . $path;
         if (!isset($this->_classes[$className]) && !($_c = realpath($path))) {
             $namespace = '';
             $className = $controller . 'Controller';
         }
     }
     $path = !$namespace ? $controller . 'Controller.php' : str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . '/' . $controller . 'Controller.php';
     $path = $this->_appDir . '/controllers/' . $path;
     if (isset($this->_classes[$className]) || ($_c = realpath($path))) {
         if (!isset($this->_classes[$className]) && $_c) {
             require $_c;
         }
         if (class_exists($className)) {
             if (!isset($this->_classes[$className])) {
                 $this->_classes[$className] = new $className($this->_getEnv());
                 $this->_forwarded = false;
             }
             if ($this->_routingEvents) {
                 \Simpletools\Events\Event::fire('beforeForwardController', array('controller' => $controller, 'action' => $action));
             }
             if (is_callable(array($this->_classes[$className], 'init')) && !$this->_forwarded) {
                 if ($this->_routingEvents) {
                     \Simpletools\Events\Event::fire('beforeControllerInit', array('controller' => $controller, 'action' => $action));
                     \Simpletools\Events\Event::fire('beforeForwardControllerInit', array('controller' => $controller, 'action' => $action));
                 }
                 if ($this->_current_controller != $controller) {
                     $this->_classes[$className]->init();
                     $this->_current_controller = $controller;
                 }
                 $this->_forwarded = true;
                 if ($this->_routingEvents) {
                     \Simpletools\Events\Event::fire('afterForwardControllerInit', array('controller' => $controller, 'action' => $action));
                 }
             }
             if ($this->_routingEvents) {
                 \Simpletools\Events\Event::fire('afterForwardController', array('controller' => $controller, 'action' => $action));
             }
             if ($this->_autoRender) {
                 $actionMethod = $action . 'Action';
                 if (is_callable(array($this->_classes[$className], $actionMethod))) {
                     if ($this->_routingEvents) {
                         \Simpletools\Events\Event::fire('beforeControllerAction', array('controller' => $controller, 'action' => $action));
                     }
                     $this->_classes[$className]->{$actionMethod}();
                     if ($this->_routingEvents) {
                         \Simpletools\Events\Event::fire('afterControllerAction', array('controller' => $controller, 'action' => $action));
                     }
                 } elseif ($className != 'ErrorController') {
                     if ($this->_routingEvents) {
                         \Simpletools\Events\Event::fire('missingControllerActionError', array('controller' => $controller, 'action' => $action));
                     }
                     return $this->error('a404');
                 } elseif ($actionMethod == 'errorAction') {
                     if ($this->_routingEvents) {
                         \Simpletools\Events\Event::fire('missingControllerActionError', array('controller' => $controller, 'action' => $action));
                     }
                     throw new \Exception("Missing errorAction() under ErrorController", 1);
                 } else {
                     if ($this->_routingEvents) {
                         \Simpletools\Events\Event::fire('missingControllerError', array('controller' => $controller, 'action' => $action));
                     }
                     throw new \Exception("Missing correct error handling structure", 1);
                 }
             }
             if ($this->_autoRender) {
                 $this->render($orgController, $action);
             }
         } else {
             if ($this->_routingEvents) {
                 \Simpletools\Events\Event::fire('missingControllerError', array('controller' => $controller, 'action' => $action));
             }
             $this->error('c405');
         }
     } else {
         if ($this->_routingEvents) {
             \Simpletools\Events\Event::fire('missingControllerError', array('controller' => $controller, 'action' => $action));
         }
         $this->error('c404');
     }
 }