/** * Create a new apllication from scratch * @param KernelInterface $application * @return KernelInterface */ public function createApplication(KernelInterface $application) { $bootstrap = $this->getBootstrap(); if ($application->getContainer() === null) { $application->setContainer($bootstrap->getContainer()); } if ($application->getConfig() === null) { $application->setConfig($bootstrap->getConfig()); } if ($application->getStrategy() === null) { $application->setStrategy($application->getContainer()->has(StrategyInterface::class) ? $application->getContainer()->get(StrategyInterface::class) : new MvcStrategy()); } if ($bootstrap instanceof Bootstrap) { $bootstrap->addService(KernelInterface::class, $application); } return $application; }
/** * @param KernelInterface $kernel * @param RequestInterface $request * @param ResponseInterface $response * @return ResponseInterface * @throws \Exception */ public function dispatch(KernelInterface $kernel, RequestInterface $request, ResponseInterface $response) { $routerId = RouteCollectionInterface::class; $router = $kernel->getContainer()->get($routerId); if (!$router instanceof RouteCollectionInterface) { throw new InvalidClassException($router, $routerId); } if (isset($config['routes'])) { foreach ($config['routes'] as $route) { $router->map($route['methods'], $route['path'], $route['handler']); } } if ($request instanceof ServerRequestInterface && ($router instanceof RouteCollection || method_exists($router, 'dispatch'))) { return $router->dispatch($request, $response); } throw new RuntimeException('Unable to dispatch router'); }