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()); }
/** * @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'); }
/** * @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); }); } }
/** * @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; }
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); }); } }