Example #1
0
 public function dispatchRequest(ServerRequestInterface $request = null, ResponseInterface $response = null)
 {
     $request = $request ?: $this->request;
     $response = $response ?: $this->response;
     if (!$request || !$response) {
         throw new \RuntimeException("Failed to dispatch: Dispatcher was not handled through middleware " . "and is missing either the request or the response argument");
     }
     $nameSpace = trim($this->getOption('nameSpace', ''), '\\');
     $modules = $this->getOption('modules', []);
     $module = $request->getAttribute('module', $this->getOption('defaultModule', ''));
     $controller = $request->getAttribute('controller', $this->getOption('defaultController', 'index'));
     //Make sure the values are correctly formatted
     if (Inflector::canonicalize($module) !== $module || Inflector::canonicalize($controller) !== $controller) {
         return $response;
     }
     $controllerPattern = $this->getOption('controllerPattern', '%sController');
     $controllerInflection = $this->getOption('controllerInflection', [Inflector::class, 'camelize']);
     $controllerClassName = sprintf($controllerPattern, call_user_func($controllerInflection, $controller));
     if (!empty($module)) {
         if (!isset($modules[$module])) {
             return $response;
         }
         $controllerClassName = trim($modules[$module], '\\') . "\\{$controllerClassName}";
     }
     if (!empty($nameSpace)) {
         $controllerClassName = "{$nameSpace}\\{$controllerClassName}";
     }
     $loader = null;
     if (!class_exists($controllerClassName, false) && $this->getOption('loader.enabled', false)) {
         $loaderPath = $this->getOption('loader.path', getcwd() . '/controllers');
         $loaderPattern = $this->getOption('loader.pattern', null);
         $loader = new Loader($loaderPath, empty($nameSpace) ? null : $nameSpace, $loaderPattern);
         $loader->register();
     }
     if (!class_exists($controllerClassName) || !is_a($controllerClassName, Controller::class, true)) {
         return $response;
     }
     if ($loader) {
         $loader->unregister();
     }
     $this->app->prepend($controllerClassName);
     return $response;
 }
Example #2
0
 protected function handleRequest(callable $next)
 {
     $req = $this->request;
     $action = $req->getAttribute('action', $this->getOption('defaultAction', 'index'));
     $id = $req->getAttribute('id', $this->getOption('defaultId', null));
     $format = $req->getAttribute('format', $this->getOption('defaultFormat', 'html'));
     //Make sure the values are correctly formatted
     if (Inflector::canonicalize($action) !== $action || Inflector::canonicalize($format) !== $format || !is_null($id) && !is_numeric($id) && Inflector::canonicalize($id) !== $id) {
         return $next($this->request, $this->response);
     }
     $getActionPattern = $this->getOption('getActionPattern', 'get%sAction');
     $getActionInflection = $this->getOption('getActionInflection', [Inflector::class, 'camelize']);
     $postActionPattern = $this->getOption('postActionPattern', 'post%sAction');
     $postActionInflection = $this->getOption('postActionInflection', [Inflector::class, 'camelize']);
     $actionPattern = $this->getOption('actionPattern', '%sAction');
     $actionInflection = $this->getOption('actionInflection', [Inflector::class, 'variablize']);
     $getMethodName = sprintf($getActionPattern, call_user_func($getActionInflection, $action));
     $postMethodName = sprintf($postActionPattern, call_user_func($postActionInflection, $action));
     $methodName = sprintf($actionPattern, call_user_func($actionInflection, $action));
     $foundMethodName = null;
     if ($req->getMethod() === Method::GET && method_exists($this, $getMethodName)) {
         $foundMethodName = $getMethodName;
     } else {
         if ($req->getMethod() === Method::POST && method_exists($this, $postMethodName)) {
             $foundMethodName = $postMethodName;
         } else {
             if (method_exists($this, $methodName)) {
                 $foundMethodName = $methodName;
             }
         }
     }
     if (!$foundMethodName) {
         return $next($this->request, $this->response);
     }
     if (($result = $this->initialize()) || ($result = call_user_func([$this, $foundMethodName], $id)) || ($result = $this->finalize())) {
         if (!$result instanceof ResponseInterface) {
             throw new \RuntimeException("Failed to dispatch controller: Called action {$foundMethodName} " . "doesn't return a " . ResponseInterface::class . " instance");
         }
         return $result;
     }
     return $next($this->request, $this->response);
 }
 /**
  * @dataProvider reJoinProvider
  */
 public function testRejoin($expected, $string, $delimiter)
 {
     $this->assertEquals($expected, Inflector::rejoin($string, $delimiter), "{$string} ({$delimiter})");
 }