/** * @param Request $request * @return Executable */ public function routeRequest(Request $request, $fn404ErrorPage = null, $fn405ErrorPage = null) { $path = $request->getUri()->getPath(); $routeInfo = $this->dispatcher->dispatch($request->getMethod(), $path); $dispatcherResult = $routeInfo[0]; if ($dispatcherResult === Dispatcher::FOUND) { $handler = $routeInfo[1]; $vars = $routeInfo[2]; // Share the params once as parameters so they can // be injected by name $injectionParams = InjectionParams::fromParams($vars); // and then share them as a type $injectionParams->share(new RouteParams($vars)); $executable = new Executable($handler, $injectionParams, null); return $executable; } else { if ($dispatcherResult === Dispatcher::METHOD_NOT_ALLOWED) { if ($fn405ErrorPage === null) { $message = sprintf("Method '%s' not allowed for path '%s'", $request->getMethod(), $path); throw new MethodNotAllowedException($message); } $executable = new Executable($fn405ErrorPage); return $executable; } } if ($fn404ErrorPage === null) { throw new RouteNotMatchedException("Route not matched for path {$path}"); } $executable = new Executable($fn404ErrorPage); return $executable; }
public static function routeCommand(ConsoleApplication $console) { // TODO - currently in PHP it is impossible to make a constructor of a child class // have a constructor that has more restricted visibility than then parent class. // In an ideal world, ConsoleApplication would be a child class that does not have a // default constructor, as that would prevent 'accidental' insertion of an unitialized // object. // However that is not currently possible, and would require an RFC to change. //Figure out what Command was requested. try { $parsedCommand = $console->parseCommandLine(); } catch (\Exception $e) { //@TODO change to just catch parseException when that's implemented $output = new BufferedOutput(); $console->renderException($e, $output); echo $output->fetch(); exit(-1); } $output = $parsedCommand->getOutput(); $formatter = $output->getFormatter(); $formatter->setStyle('question', new OutputFormatterStyle('blue')); $formatter->setStyle('info', new OutputFormatterStyle('blue')); $questionHelper = new QuestionHelper(); $questionHelper->setHelperSet($console->getHelperSet()); $injectionParams = InjectionParams::fromParams($parsedCommand->getParams()); $executable = new Executable($parsedCommand->getCallable(), $injectionParams); $executable->setAllowedToReturnNull(true); return $executable; }
/** * @param Request $request * @return Executable */ public function routeRequest(Request $request) { $path = $request->getUri()->getPath(); $routeInfo = $this->dispatcher->dispatch($request->getMethod(), $path); $dispatcherResult = $routeInfo[0]; if ($dispatcherResult === \FastRoute\Dispatcher::FOUND) { $handler = $routeInfo[1]; $vars = $routeInfo[2]; // Share the params once as parameters so they can // be injected by name $injectionParams = InjectionParams::fromParams($vars); // and then share them as a type $injectionParams->share(new \Tier\Bridge\RouteParams($vars)); return new Executable($handler, $injectionParams, null); } else { if ($dispatcherResult === \FastRoute\Dispatcher::METHOD_NOT_ALLOWED) { //TODO - need to embed allowedMethods....theoretically. return new Executable([$this, 'serve405ErrorPage']); } } $templateName = $this->templateExists($path, $this->jigConfig); if ($templateName !== false) { return $this->tierJig->createJigExecutable($templateName); } return new Executable([$this, 'serve404ErrorPage']); }
/** * @throws TierException */ public function testInjectorParamsUsed() { $tierApp = new TierApp(new Injector(), new NullCallback()); //Create the second function first, so it can be use'd $fooDebug = null; $fn2 = function ($foo) use(&$fooDebug) { $fooDebug = $foo; return TierApp::PROCESS_END; }; //First executable sets up the 'foo' param and runs the 2nd fn. $fn1 = function () use($fn2) { $params = InjectionParams::fromParams(['foo' => 'bar']); return new Executable($fn2, $params); }; $tierApp->addExecutable(0, $fn1); $tierApp->addExecutable(5, $fn2); $tierApp->executeInternal(); $this->assertEquals('bar', $fooDebug); }