/** * @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]; $injectionParams = InjectionParams::fromParams($vars); $injectionParams->share(new \Tier\JigBridge\RouteInfo($vars)); $executable = new Executable($handler, $injectionParams, null); $executable->setTierNumber(TierHTTPApp::TIER_GENERATE_BODY); return $executable; } else { if ($dispatcherResult === \FastRoute\Dispatcher::METHOD_NOT_ALLOWED) { //TODO - need to embed allowedMethods....theoretically. $executable = new Executable([$this, 'serve405ErrorPage']); $executable->setTierNumber(TierHTTPApp::TIER_GENERATE_BODY); return $executable; } } $executable = new Executable([$this, 'serve404ErrorPage']); $executable->setTierNumber(TierHTTPApp::TIER_GENERATE_BODY); 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; }
public function testPath() { $fn1 = function () { return "actual"; }; $fn2 = function () { return "setup"; }; $injectionParams = new InjectionParams(); $exec = new Executable($fn1, $injectionParams, $fn2); $exec->getInjectionParams(); $callable = $exec->getCallable(); $setupCallable = $exec->getSetupCallable(); $this->assertEquals("setup", $setupCallable()); $this->assertEquals("actual", $callable()); }
/** * @param $result * @return TierException */ public static function getWrongTypeException($result, Executable $executable) { $messageStart = self::RETURN_VALUE; $messageStart .= " Instead %s returned, when calling executable %s"; if ($result === null) { $detail = 'null'; } else { if (is_object($result) === true) { $detail = "object of type '" . get_class($result) . "'"; } else { $detail = "variable of type '" . gettype($result) . "'"; } } $callableInfo = var_export($executable->getCallable(), true); $message = sprintf($messageStart, $detail, $callableInfo); return new InvalidReturnException($message, $result); }
public function testExecutableAddedPreviousTier() { $executableListByTier = new ExecutableListByTier(); $fn2 = function () { $this->fail("This should never be reached."); }; $fn1 = function () use(&$executableListByTier, $fn2) { $executable = new Executable($fn2); $executable->setTierNumber(4); $executableListByTier->addExecutable($executable); }; $executableListByTier->addExecutableToTier(5, $fn1); $this->setExpectedException('Tier\\TierException', TierException::INCORRECT_VALUE); foreach ($executableListByTier as $tier => $executable) { $order[] = $tier; $callable = $executable->getCallable(); call_user_func($callable); } }
/** * */ public function run() { /** @noinspection PhpUndefinedMethodInspection */ \ImagickDemo\Imagick\functions::load(); \ImagickDemo\ImagickDraw\functions::load(); \ImagickDemo\ImagickPixel\functions::load(); \ImagickDemo\ImagickKernel\functions::load(); \ImagickDemo\ImagickPixelIterator\functions::load(); \ImagickDemo\Tutorial\functions::load(); $executableList = []; $imageGenerateExecutable = new Executable([$this, 'actuallyRun']); $imageGenerateExecutable->setTierNumber(\Tier\TierCLIApp::TIER_LOOP); $imageGenerateExecutable->setAllowedToReturnNull(true); $executableList[] = $imageGenerateExecutable; $timeoutExecutable = new Executable([$this, 'timeoutCheck']); $timeoutExecutable->setTierNumber(\Tier\TierCLIApp::TIER_LOOP); $executableList[] = $timeoutExecutable; return $executableList; }
/** * @param $result mixed The result produced by running the previous executable. * @param Executable $executable The executable that was just run to produce * the result. * @return int * @throws TierException * @throws \Auryn\ConfigException */ protected function processResult($result, Executable $executable) { // If it's a new Tier to run, setup the next loop. if ($result instanceof InjectionParams) { $result->addToInjector($this->injector); return self::PROCESS_CONTINUE; } // If it's a new Tier to run, setup the next loop. if ($result instanceof Executable) { $this->executableListByTier->addExecutable($result); return self::PROCESS_CONTINUE; } if ($result === null && $executable->isAllowedToReturnNull() === true) { return self::PROCESS_CONTINUE; } // if (is_array($result) === true && // count($result) !== 0) { // //It's an array of tiers to run. // foreach ($result as $executableOrCallable) { // if (($executableOrCallable instanceof Executable) === true) { // $this->executableListByTier->addExecutable($executableOrCallable); // continue; // } // else if (is_callable($executableOrCallable) === true) { // $newExecutable = new Executable($executableOrCallable); // $this->executableListByTier->addExecutable($newExecutable); // continue; // } // // throw InvalidReturnException::getWrongTypeException($result, $executable); // } // return self::PROCESS_CONTINUE; // } if ($result === false) { // The executed tier wasn't able to handle it e.g. a caching layer // There should be another tier to execute in this stage. return self::PROCESS_CONTINUE; } // If the $result is an expected product share it for further stages foreach ($this->expectedProducts as $expectedProduct => $created) { if ($result instanceof $expectedProduct) { if (is_subclass_of($result, $expectedProduct) === true) { //product is a sub-class of the expected product. Setup an //alias for it $this->injector->alias($expectedProduct, get_class($result)); } $this->expectedProducts[$expectedProduct] = true; $this->injector->share($result); return self::PROCESS_CONTINUE; } } if ($result === self::PROCESS_CONTINUE || $result === self::PROCESS_END) { return $result; } // Otherwise it's an error throw InvalidReturnException::getWrongTypeException($result, $executable); }