コード例 #1
0
 public function init()
 {
     // register packages autoloading
     $this->getAutoloader()->addPsr4('Project\\Package\\', 'packages/');
     // define middleware endpoints
     $this->addSteps('init', 'bootstrap', 'route', 'action', 'rendering', 'end');
     // initialize request and response
     $this->getStep('init')->plug(new RequestWrapper())->as('request-wrapper')->plug(new ResponseInitializer())->as('response-initializer');
     $this->importPackages();
     // route request (this is done after packages have been loaded)
     $router = new MetaRouter([new PathMapperRouter(), new FastRouteRouter()]);
     $this->getStep('route')->plug($router)->as('router');
     // load framework native middleware
     $this->getStep('bootstrap')->plug(ServiceLoader::class)->asDefault('service-loader');
     // give access to config everywhere, including views
     //
     // Note: this is used by default layouts
     $this->getStep('bootstrap')->plug(function ($app) {
         Vars::$config = $app->getConfig();
     });
     // the dispatcher will actually run the matched route
     $this->getStep('route')->plug(new Dispatcher())->as('dispatcher');
     // handle view rendering
     $this->getStep('rendering')->plug(LayoutSwitcher::class, new UrlFilter('/'))->plug(new ViewResolver())->as('view-resolver')->plug(new ViewRenderer())->as('view-renderer');
     $this->getStep('end')->plug(new ResponseSender());
 }
コード例 #2
0
ファイル: Home.php プロジェクト: objective-php/starter-kit
 /**
  * @param ApplicationInterface
  *
  * @return array
  */
 public function run(ApplicationInterface $app)
 {
     // Vars class holds view variables
     //
     // you can return an array of view variables or
     // directly call it
     Vars::set('page.title', 'Objective PHP Project Template');
 }
コード例 #3
0
 /**
  * @param ApplicationInterface $app
  * @return mixed
  * @throws \ObjectivePHP\Primitives\Exception
  * @internal param ApplicationInterface $application
  *
  */
 public function run(ApplicationInterface $app)
 {
     $result = parent::run($app);
     if ($result instanceof Response) {
         $app->setResponse($result);
     } else {
         // set default content type
         $app->setResponse((new HttpResponse())->withHeader('Content-Type', 'text/html'));
         Collection::cast($result)->each(function ($value, $var) {
             Vars::set($var, $value);
         });
     }
 }
コード例 #4
0
 /**
  * @param       $viewName
  * @param array $context
  *
  * @return string
  * @throws Exception
  */
 public function render($viewPath, $vars = [])
 {
     foreach ($vars as $reference => $value) {
         Vars::set($reference, $value);
     }
     if (!file_exists($viewPath)) {
         throw new Exception(sprintf('View script "%s" does not exist', $viewPath));
     }
     ob_start();
     include $viewPath;
     $output = ob_get_clean();
     return $output;
 }
コード例 #5
0
ファイル: Dispatcher.php プロジェクト: objective-php/router
 public function run(ApplicationInterface $app)
 {
     $matchedRoute = $app->getRequest()->getMatchedRoute();
     $action = Invokable::cast($matchedRoute->getAction());
     $app->getServicesFactory()->injectDependencies($action->getCallable());
     $app->setParam('runtime.action.middleware', $action);
     $result = $action->getCallable()($app);
     if ($result instanceof Response) {
         $app->setResponse($result);
     } else {
         // set default content type
         $app->setResponse((new HttpResponse())->withHeader('Content-Type', 'text/html'));
         Collection::cast($result)->each(function ($value, $var) {
             Vars::set($var, $value);
         });
     }
 }