/** * Run. * @return void */ public final function run() { // security & performans checks if ($halt = $this->haltCheck()) { $this->halt($halt); } // re-set app as global set_global('app', $this); $this->request->init(['uriRoot' => $this->root]); $this->response->init(); // set defaults $this->setDefaults(); // start output buffer $this->startOutputBuffer(); $this->service = (new ServiceAdapter($this))->getService(); // create session if service uses session if ($this->service->usesSession()) { $this->session = Session::init($this->config['app.session.cookie']); } $output = $this->service->run(); // end output buffer $this->endOutputBuffer($output); }
/** * Constructor. * @param Froq\App $app */ public final function __construct(App $app) { $this->app = $app; // aliases $serviceAliases = $this->app->config->get('app.service.aliases', []); // detect service name $serviceNameAlias = ''; $serviceName = strtolower($this->app->request->uri->segment(0, '')); // check alias if (array_key_exists($serviceName, $serviceAliases) && isset($serviceAliases[$serviceName][0])) { $serviceNameAlias = $serviceName; $serviceName = $serviceAliases[$serviceName][0]; } else { $serviceName = $serviceName ?: ServiceInterface::SERVICE_MAIN; } $this->serviceName = $this->toServiceName($serviceName); $this->serviceFile = $this->toServiceFile($this->serviceName); $this->serviceClass = $this->toServiceClass($this->serviceName); // set service as FailService if (!$this->isServiceExists()) { set_global('app.service.fail', ['code' => 404, 'text' => sprintf('Service not found [%s]', $this->serviceName)]); $this->serviceName = ServiceInterface::SERVICE_FAIL . ServiceInterface::SERVICE_NAME_SUFFIX; $this->serviceMethod = ServiceInterface::METHOD_MAIN; $this->serviceFile = $this->toServiceFile($this->serviceName); $this->serviceClass = $this->toServiceClass($this->serviceName); } $this->service = $this->createService(); if (!$this->service->isFailService()) { // detect service method if ($this->service->useMainOnly) { // main only $this->serviceMethod = ServiceInterface::METHOD_MAIN; } elseif ($this->service->protocol == ServiceInterface::PROTOCOL_SITE) { // from segment $serviceMethod = strtolower($this->app->request->uri->segment(1, '')); // check alias if (isset($serviceAliases[$serviceNameAlias]['methods']) && array_key_exists($serviceMethod, $serviceAliases[$serviceNameAlias]['methods'])) { $this->serviceMethod = $this->toServiceMethod($serviceAliases[$serviceNameAlias]['methods'][$serviceMethod]); } elseif ($serviceMethod == '' || $serviceMethod == ServiceInterface::METHOD_MAIN) { $this->serviceMethod = ServiceInterface::METHOD_MAIN; } else { $this->serviceMethod = $this->toServiceMethod($serviceMethod); } } elseif ($this->service->protocol == ServiceInterface::PROTOCOL_REST) { // from request method $this->serviceMethod = strtolower($this->app->request->method); } // check method if (!$this->isServiceMethodExists()) { // check fallback method if ($this->isServiceMethodFallExists()) { $this->serviceMethod = ServiceInterface::METHOD_FALL; } else { set_global('app.service.fail', ['code' => 404, 'text' => sprintf('Service method not found [%s::%s()]', $this->serviceName, $this->serviceMethod)]); // overwrite $this->serviceName = ServiceInterface::SERVICE_FAIL . ServiceInterface::SERVICE_NAME_SUFFIX; $this->serviceMethod = ServiceInterface::METHOD_MAIN; $this->serviceFile = $this->toServiceFile($this->serviceName); $this->serviceClass = $this->toServiceClass($this->serviceName); // re-create service as FailService $this->service = $this->createService(); } } // set service method $this->service->setMethod($this->serviceMethod); // set service method args if ($this->isServiceMethodExists()) { $methodArgs = array_slice($this->app->request->uri->segments(), 2); $ref = new \ReflectionMethod($this->serviceClass, $this->serviceMethod); foreach ($ref->getParameters() as $i => $param) { if (!isset($methodArgs[$i])) { $methodArgs[$i] = $param->isDefaultValueAvailable() ? $param->getDefaultValue() : null; } } $this->service->setMethodArgs($methodArgs); } } }