Example #1
0
 public function createJigExecutable($templateName, InjectionParams $injectionParams = null)
 {
     if ($injectionParams === null) {
         $injectionParams = new InjectionParams();
     }
     $className = $this->jig->compile($templateName);
     $injectionParams->alias('Jig\\JigBase', $className);
     return new Executable(['Tier\\Bridge\\TierJig', 'createHtmlBody'], $injectionParams);
 }
Example #2
0
 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;
 }
Example #3
0
 /**
  * @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;
 }
Example #4
0
 /**
  * @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']);
 }
Example #5
0
 /**
  * @param $templateName
  * @param InjectionParams $injectionParams
  * @return Executable
  */
 public static function create($templateName, InjectionParams $injectionParams = null)
 {
     if ($injectionParams === null) {
         $injectionParams = new InjectionParams();
     }
     // This uses double-dispatch so that the first Executable can have it's
     // dependency injected, and then the second Exectuable that actually renders the
     // template has its dependencies injected separately.
     $fn = function (Jig $jigRender) use($templateName, $injectionParams) {
         $className = $jigRender->compile($templateName);
         $injectionParams->alias('Jig\\JigBase', $className);
         $fn = function (Injector $injector) use($className) {
             return $injector->make($className);
         };
         $injectionParams->delegate('Jig\\JigBase', $fn);
         return new Executable(['Tier\\Bridge\\JigExecutable', 'createHtmlBody'], $injectionParams);
     };
     return new Executable($fn);
 }
Example #6
0
 public function testReturnInjectionParams()
 {
     $tierApp = new TierApp(new Injector(), new NullCallback());
     $addInjectionParamsFn = function () {
         $injectionParams = new InjectionParams();
         $injectionParams->alias('Fixtures\\FooInterface', 'Fixtures\\FooImplementation');
         return $injectionParams;
     };
     // When tier tries to instantiate this, it will fail if the alias
     // hasn't been added.
     $requiresInterfaceFn = function (\Fixtures\FooInterface $foo) {
         return TierApp::PROCESS_END;
     };
     $tierApp->addExecutable(10, $addInjectionParamsFn);
     $tierApp->addExecutable(20, $requiresInterfaceFn);
     $tierApp->executeInternal();
 }