Exemplo n.º 1
1
 public function dispatch(Route $route, Request $request, Response $response)
 {
     $controller = __NAMESPACE__ . '\\Controllers\\' . $route->getController();
     $action = $route->getAction();
     $foo = new $controller($request, $response);
     $foo->{$action}();
     $response->send();
 }
Exemplo n.º 2
0
 public function __construct()
 {
     self::$__instance = $this;
     set_exception_handler('exceptionHandler');
     // setup our loader instance
     $this->loader = new Loader();
     // load a few helpers
     $this->loader->helper('uri', FRAMEWORK_PATH . 'helpers');
     // loader the plugins
     $this->plugins = $this->loader->manager('plugins');
     $this->plugins->loadFrameworkPlugins();
     // what shall we load first?
     $this->route = $this->loader->manager('route')->find();
     // load the controller
     $this->controller = $this->loader->controller($this->route->getController());
     $this->controller->invokeAction($this->route->getAction());
 }
Exemplo n.º 3
0
 /**
  * @param Route $route The unparsed route whose properties we are copying
  */
 public function __construct(Route $route)
 {
     parent::__construct($route->getMethods(), $route->getRawPath(), $route->getController());
     $this->setName($route->getName());
     $this->setRawHost($route->getRawHost());
     $this->addMiddleware($route->getMiddleware());
     $this->setSecure($route->isSecure());
     $this->setVarRegexes($route->varRegexes);
 }
Exemplo n.º 4
0
 /**
  * __construct()
  * check if init() method is declared and runs
  *
  * @see \ngfw\Route
  * @see \ngfw\View
  */
 public function __construct()
 {
     $className = Route::getController();
     $method = Route::getAction();
     $this->view = new View($className, $method);
     if (method_exists($this, 'init')) {
         $this->init();
     }
 }
Exemplo n.º 5
0
 /**
  * @dataProvider getSetProvider
  * @covers Route::getController
  * @covers Route::setController
  * @covers Route::getAction
  * @covers Route::setAction
  * @covers Route::getParams
  * @covers Route::setParams
  *
  * @param string $controller
  * @param string $action
  * @param array $params
  */
 public function testGetSet($controller, $action, array $params)
 {
     $route = new Route('a', 'b', array('c'));
     $route->setController($controller);
     $this->assertEquals($controller, $route->getController());
     $route->setAction($action);
     $this->assertEquals($action, $route->getAction());
     $route->setParams($params);
     $this->assertEquals($params, $route->getParams());
 }
Exemplo n.º 6
0
 /**
  * The function returns current controller link.
  * If passed link starts from / returns related to root controller.
  * 
  * @access protected
  * @param string $link The part of link.
  * @param bool $restoreGet If TRUE returns link with GET parameters.
  * @return string The link.
  */
 protected function getLink($link = '', $restoreGet = false)
 {
     $Controller = $this->getController();
     if (substr($link, 0, 1) == '/') {
         $Controller = Route::getController('/');
     }
     if ($link) {
         $link = '/' . ltrim($link, '/');
     }
     if ($restoreGet && count($_GET)) {
         $link .= strpos($link, '?') === false ? '?' : '&';
         $link .= http_build_query($_GET);
     }
     return rtrim(_L($Controller), '/') . $link;
 }
Exemplo n.º 7
0
 private function invoke(Route $route)
 {
     $className = 'AlfredSlack\\Controllers\\' . ucfirst($route->getController()) . 'Controller';
     $actionName = $route->getAction() . 'Action';
     $controller = new $className();
     if (!$controller instanceof \AlfredSlack\Controllers\Controller) {
         throw new \Exception("{$className} must inherits from AlfredSlack\\Controllers\\Controller");
     }
     Utils::log('ACTION: ' . $className . '::' . $actionName . '()');
     Utils::log('SIMULATE: php -r \'$query="' . str_replace('"', '\\"', json_encode($route)) . '";include "scripts/index.php";\';');
     $interruptAction = $controller->preDispatch($actionName, $route->getParams()) === false;
     if (!$interruptAction) {
         $actionResult = $controller->dispatch($actionName, $route->getParams());
         $controller->postDispatch($actionName, $route->getParams(), $actionResult);
     }
 }
 /**
  * @param array      $routeConfig
  * @param Route|null $parentRoute
  *
  * @return Route
  */
 private function processRoute(array $routeConfig, Route $parentRoute = null)
 {
     $name = $routeConfig['name'];
     $url = isset($routeConfig['options']['route']) ? $routeConfig['options']['route'] : '';
     $controller = isset($routeConfig['options']['defaults']['controller']) ? $routeConfig['options']['defaults']['controller'] : null;
     $action = isset($routeConfig['options']['defaults']['action']) ? $routeConfig['options']['defaults']['action'] : null;
     if (null !== $parentRoute) {
         $name = $parentRoute->getName() . '/' . $name;
         $url = $parentRoute->getUrl() . $url;
         if (null === $controller) {
             $controller = $parentRoute->getController();
         }
     }
     if (null === $action) {
         $action = 'index';
     }
     $action .= 'Action';
     return new Route($name, $url, $controller, $action);
 }
Exemplo n.º 9
0
 /**
  * _loadController()
  * Loads application controller
  *
  * @see \ngfw\Route
  * @throws \ngfw\Exception
  * @return void
  */
 private function loadController()
 {
     if (!$this->_controllerLoaded) {
         $controllerTitle = Route::getController() . "Controller";
         if (class_exists($controllerTitle)) {
             $this->_controllerObject = new $controllerTitle();
         } else {
             throw new Exception(sprintf('The requested Controller "%s" does not exist.', $controllerTitle));
         }
         if ($this->_viewTemplate) {
             $this->_controllerObject->setViewObject("template", $this->_viewTemplate);
         }
         $this->_controllerLoaded = true;
         $method = Route::getAction() . "Action";
         if (is_callable(array($this->_controllerObject, $method))) {
             call_user_func(array($this->_controllerObject, $method));
             $this->_controllerObject->startRander();
         } else {
             throw new Exception(sprintf('The requested method "%s" does not exist in %s.', $method, $controllerTitle));
         }
     }
 }
Exemplo n.º 10
0
 /**
  * Return current controller name
  *
  * @static
  * @access   public
  * @return   string
  * @since    1.0.0-alpha
  * @version  1.0.0-alpha
  */
 public static function getControllerName()
 {
     return static::$currentRoute->getController();
 }
Exemplo n.º 11
0
 /**
  * The function initializes router feature.
  * 
  * @static
  * @access private
  */
 private static function initRoute()
 {
     Route::run(Request::get('REQUEST_URI', '/', 'SERVER'));
     $host = strtolower(Config::get('host'));
     if ($host) {
         $sub = trim(str_replace($host, '', preg_replace('/^www\\./', '', strtolower(Runtime::get('HTTP_HOST')))), '.');
         if (!$sub) {
             $sub = 'www';
         }
         Runtime::set('HTTP_SUBDOMAIN', $sub);
     }
     $controller = 0;
     $args = array();
     $path = Route::get();
     Runtime::set('REQUEST_URI', '/' . implode('/', $path));
     if (!is_array($path)) {
         $path = ltrim('/', explode('/', $path));
     }
     $values = $path;
     $link = '/';
     for ($i = 0; $i < count($values); $i++) {
         $arr = array_slice($values, 0, count($values) - $i);
         $link = '/' . implode('/', $arr);
         $controller = Route::getController($link);
         if ($controller) {
             $args = array_slice($values, count($values) - $i);
             break;
         }
     }
     if (!$controller) {
         $controller = Route::getController('/');
         $args = $values;
     }
     if (!$controller) {
         echo "No controller found: /" . implode('/', $path) . "\n";
         exit;
     }
     Runtime::set('ROUTING_CONTROLLER', $controller);
     Runtime::set('ROUTING_ROUTER', Route::getRouter($link));
     Runtime::set('ROUTING_ARGUMENTS', $args);
 }
Exemplo n.º 12
0
 /**
  * Run application
  *
  * @staticvar boolean $is_init
  * @staticvar array $routes
  * @param $route (optional, ex: 'my/route->action')
  * @return void
  */
 public function run($route = null)
 {
     static $is_init = false;
     static $routes = [];
     // routes stack for current request
     if (!$is_init) {
         $this->log->trace('Initializing', Logger::CATEGORY_DRONE);
         // param default values
         $default = [self::KEY_DEBUG => true, self::KEY_ERROR_BACKTRACE => true, self::KEY_ERROR_HANDLER => ['\\Drone\\Core', 'errorHandler'], self::KEY_ERROR_LOG => false, self::KEY_EXT_TEMPLATE => '.tpl', self::KEY_EXT_WEB => '.htm', self::KEY_PATH_CONTROLLER => PATH_ROOT . '_app/mod', self::KEY_PATH_TEMPLATE => PATH_ROOT . '_app/tpl', self::KEY_PATH_TEMPLATE_GLOBAL => PATH_ROOT . '_app/tpl/_global'];
         // init param default values
         foreach ($default as $k => $v) {
             if (!Registry::has($k)) {
                 Registry::set($k, $v);
             }
         }
         // set default error handler
         if (is_array(Registry::get(self::KEY_ERROR_HANDLER))) {
             set_error_handler(Registry::get(self::KEY_ERROR_HANDLER));
         }
         // init paths
         $this->__formatDir(Registry::get(self::KEY_PATH_CONTROLLER));
         $this->__formatDir(Registry::get(self::KEY_PATH_TEMPLATE));
         $this->__formatDir(Registry::get(self::KEY_PATH_TEMPLATE_GLOBAL));
         $is_init = true;
     }
     Registry::set(self::KEY_ROUTE_CONTROLLER, false);
     // init controller
     if ($route !== null) {
         $routes[] = $route;
         // cache route
         $route = new Route(null, $route);
         Registry::set([self::KEY_ROUTE_CONTROLLER => $route->getController(), self::KEY_ROUTE_CLASS => $route->getClass(), self::KEY_ROUTE_TEMPLATE => $route->getController()]);
         if ($route->isAction()) {
             Registry::set(self::KEY_ROUTE_ACTION, $route->getAction());
         }
         $this->log->trace('Route set: \'' . Registry::get(self::KEY_ROUTE_CONTROLLER) . '\'', Logger::CATEGORY_DRONE);
     } else {
         $is_index = false;
         $request = $_SERVER['REQUEST_URI'];
         $this->log->trace('Process request: \'' . $request . '\'', Logger::CATEGORY_DRONE);
         Registry::set(self::KEY_REQUEST, $request);
         if (($pos = strpos($request, '?')) !== false) {
             $request = substr($request, 0, $pos);
         }
         unset($pos);
         $routes[] = $request;
         if (substr($request, -1) != '/') {
             // ensure request has web extension
             if (substr($request, -strlen(Registry::get(self::KEY_EXT_WEB))) === Registry::get(self::KEY_EXT_WEB)) {
                 // do not allow direct access to index like '/path/index.htm'
                 if (basename($request) === 'index' . Registry::get(self::KEY_EXT_WEB)) {
                     $this->error(self::ERROR_404);
                     // kick direct index request
                     return;
                 }
                 // rm web extension
                 $request = substr($request, 0, strlen($request) - strlen(Registry::get(self::KEY_EXT_WEB)));
             } else {
                 $this->error(self::ERROR_404);
                 // kick request
                 return;
             }
         } else {
             $is_index = true;
         }
         $r = null;
         foreach ($this->__routes as $r) {
             if ($rf = $r->matchFile($request)) {
                 $this->log->trace('Route file loaded: \'' . $r->getController() . '\'', Logger::CATEGORY_DRONE);
                 foreach ($rf as $k => $v) {
                     $r = new Route($k, $v);
                     if ($r->match($request)) {
                         break 2;
                         // match
                     }
                 }
             } else {
                 if ($r->match($request)) {
                     break;
                     // match
                 }
             }
             $r = null;
             // no match
         }
         if ($r) {
             $this->log->trace('Route (mapped) detected: \'' . $r->getPath() . '\'', Logger::CATEGORY_DRONE);
             Registry::set([self::KEY_ROUTE_CONTROLLER => $r->getController(), self::KEY_ROUTE_CLASS => $r->getClass(), self::KEY_ROUTE_TEMPLATE => $r->getController()]);
             if ($r->isAction()) {
                 Registry::set(self::KEY_ROUTE_ACTION, $r->getAction());
             }
             $this->view->setRouteParams($r->getParams());
             // set route params
         }
         unset($r);
         // test static routes
         if (Registry::get(self::KEY_ROUTE_CONTROLLER) === false) {
             $request = str_replace('/', DIRECTORY_SEPARATOR, $request);
             if ($is_index) {
                 $request .= 'index';
             }
             Registry::set([self::KEY_ROUTE_CONTROLLER => $request, self::KEY_ROUTE_TEMPLATE => $request]);
             $this->log->trace('Route (static) detected: \'' . Registry::get(self::KEY_ROUTE_CONTROLLER) . '\'', Logger::CATEGORY_DRONE);
         }
         // cleanup
         unset($is_index, $request);
     }
     if (max(array_count_values($routes)) > 1) {
         $routes = [];
         // reset
         $this->error(self::ERROR_500, 'Route loop detected');
         return;
     }
     // set full paths + extensions
     Registry::set(self::KEY_ROUTE_CONTROLLER, Registry::get(self::KEY_PATH_CONTROLLER) . ltrim(Registry::get(self::KEY_ROUTE_CONTROLLER), DIRECTORY_SEPARATOR) . '.php');
     Registry::set(self::KEY_ROUTE_TEMPLATE, Registry::get(self::KEY_PATH_TEMPLATE) . ltrim(Registry::get(self::KEY_ROUTE_TEMPLATE), DIRECTORY_SEPARATOR) . Registry::get(self::KEY_EXT_TEMPLATE));
     try {
         $this->view->resetTemplate();
         // reset template (for multiple runs like errors)
         $this->view->setDefaultTemplate(Registry::get(self::KEY_ROUTE_TEMPLATE));
         // set default template
         $this->error(false);
         // reset error flag
         if (is_file(Registry::get(self::KEY_ROUTE_CONTROLLER))) {
             ob_start();
             // buffer output
             $this->__hooks(self::HOOK_BEFORE, 'before');
             if (isset($this->__hooks[self::HOOK_BEFORE])) {
                 foreach ($this->__hooks[self::HOOK_BEFORE] as $hook) {
                     require $hook;
                 }
             }
             $this->log->trace('Loading controller: \'' . Registry::get(self::KEY_ROUTE_CONTROLLER) . '\'', Logger::CATEGORY_DRONE);
             require_once Registry::get(self::KEY_ROUTE_CONTROLLER);
             $this->__headersSend();
             // send headers
             $controller_class = Registry::get(self::KEY_ROUTE_CLASS);
             // call controller action
             if (Registry::has(self::KEY_ROUTE_ACTION)) {
                 $this->log->trace('Calling action: \'' . Registry::get(self::KEY_ROUTE_ACTION) . '\' on controller class \'' . $controller_class . '\'', Logger::CATEGORY_DRONE);
                 if (!class_exists($controller_class, false)) {
                     if (count($routes) > 1) {
                         $this->log->fatal('Multiple route controllers not found loop detected', Logger::CATEGORY_DRONE);
                         $this->stop();
                     }
                     $this->error(self::ERROR_500, 'Class \'' . $controller_class . '\' not found when calling route action');
                     return;
                 }
                 if (!method_exists($controller_class, Registry::get(self::KEY_ROUTE_ACTION))) {
                     $this->error(self::ERROR_500, 'Method \'' . $controller_class . '::' . Registry::get(self::KEY_ROUTE_ACTION) . '\' not found when calling route action');
                     return;
                 }
                 // set controller instance
                 $controller = new $controller_class();
                 if (method_exists($controller, '__before')) {
                     $controller->__before();
                 }
                 // call controller action
                 $controller->{Registry::get(self::KEY_ROUTE_ACTION)}();
                 if (method_exists($controller, '__after')) {
                     $controller->__after();
                 }
             } else {
                 if ($this->deny(null)) {
                     $this->error(self::ERROR_404, 'Deny no action');
                     return;
                 }
             }
             unset($controller_class);
             // cleanup
             if (!Registry::get(self::KEY_DEBUG)) {
                 $this->__bufferClean();
             }
             $this->__hooks(self::HOOK_MIDDLE, 'middle');
             if (isset($this->__hooks[self::HOOK_MIDDLE])) {
                 foreach ($this->__hooks[self::HOOK_MIDDLE] as $hook) {
                     require $hook;
                 }
             }
             // view display template
             if (!is_null($this->view->getTemplate())) {
                 $this->log->trace('Loading view template: \'' . $this->view->getTemplate() . '\'', Logger::CATEGORY_DRONE);
                 if (!is_file($this->view->getTemplate())) {
                     $this->error(self::ERROR_500, 'View template \'' . $this->view->getTemplate() . '\' not found');
                     return;
                 }
                 if (isset($controller)) {
                     extract(get_object_vars($controller), EXTR_OVERWRITE);
                 }
                 // extract all view public properties for variable use in template
                 extract(get_object_vars($this->view), EXTR_OVERWRITE);
                 if (strlen($this->view->getTemplateHeader()) > 0) {
                     include $this->view->getTemplateHeader();
                 }
                 require $this->view->getTemplate();
                 if (strlen($this->view->getTemplateFooter()) > 0) {
                     include $this->view->getTemplateFooter();
                 }
             }
             if (!$this->error()) {
                 ob_end_flush();
                 // flush buffer
                 $this->stop();
                 // finalize application
             } else {
                 ob_end_clean();
                 // clean buffer
                 $this->error(self::ERROR_500);
                 // call 500 error handler
             }
         } else {
             $this->error(self::ERROR_404);
         }
     } catch (\Exception $ex) {
         $this->error($ex);
     }
 }
Exemplo n.º 13
0
<?php

// get the absolute path to system
$sys_path = dirname(__FILE__);
// set the include path
set_include_path($sys_path . PATH_SEPARATOR . get_include_path());
// include some required components
require_once 'components/common.php';
require_once 'components/autoload.php';
// get config instance
$config = get_config();
// store the system path in config
$config->set('path.system', $sys_path);
// get route library
$route = new Route();
// get controller, action and request params
$controller = $route->getController();
$action = $route->getAction();
$params = $route->getParams();
// store in config
$config->set('request.controller', $controller);
$config->set('request.action', $action);
$config->set('request.params', $params);
// init the application
$application = new Application($controller, $action);
// EOF
Exemplo n.º 14
0
 /**
  * Calls an action inside a controller
  * (if at least one of them is not present
  * nothing happens)
  * 
  * @param Route Route
  */
 private function runController(Route $route, array $params)
 {
     $controller = $route->getController();
     $action = $route->getAction();
     Router::$currentController = $controller . '#' . $action;
     ControllerBase::runController($controller, $action, $params);
 }
Exemplo n.º 15
0
 public function testSetController()
 {
     $this->object->setController('someController');
     $this->assertEquals('someController', $this->object->getController());
 }