terminate() public method

public terminate ( Request $request, Response $response )
$request Symfony\Component\HttpFoundation\Request
$response Symfony\Component\HttpFoundation\Response
示例#1
0
 public function run()
 {
     $this->response = $this->kernel->handle($this->request);
     $this->response->send();
     $this->kernel->terminate($this->request, $this->response);
     return $this;
 }
示例#2
0
 /**
  * run Application
  */
 public function run()
 {
     $response = $this->container->get(TwigResponse::class);
     $response->setTemplate('error/404');
     try {
         $this->eventDispatcher->addSubscriber($this->routerListener);
         $response = $this->httpKernel->handle($this->request);
     } catch (ResourceNotFoundException $e) {
         $response->setContent(['message' => $e->getMessage()]);
     } catch (\Exception $e) {
         //            $response->setContent(['message' => $e->getMessage()]);
         throw $e;
     } finally {
         $response->send();
         $this->httpKernel->terminate($this->request, $response);
     }
 }
示例#3
0
 /**
  * Triggers kernel.response and sends a form response.
  *
  * @param \Symfony\Component\HttpFoundation\Response $response
  *   A response object.
  */
 protected function sendResponse(Response $response)
 {
     $request = $this->requestStack->getCurrentRequest();
     $event = new FilterResponseEvent($this->httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);
     $this->eventDispatcher->dispatch(KernelEvents::RESPONSE, $event);
     // Prepare and send the response.
     $event->getResponse()->prepare($request)->send();
     $this->httpKernel->terminate($request, $response);
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function __invoke(Request $request)
 {
     $context = new RequestContext();
     $context->fromRequest($request);
     $this->eventDispatcher->addSubscriber(new RouterListener(new UrlMatcher($this->routeCollection, $context)));
     $kernel = new HttpKernel($this->eventDispatcher, new ControllerResolverActionResolverAdapter($this->actionResolver));
     $response = $kernel->handle($request);
     $response->send();
     $kernel->terminate($request, $response);
 }
示例#5
0
 public function __construct()
 {
     $container = new ContainerBuilder();
     $loader = new YamlFileLoader($container, new FileLocator(ROOT_PATH . '/app/config'));
     $loader->load('services.yml');
     $loader = new YamlFileLoader($container, new FileLocator(ROOT_PATH . '/src/config'));
     $loader->load('services.yml');
     $container->compile();
     $routes = $container->get('router')->getRoutes();
     $request = Request::createFromGlobals();
     $matcher = new UrlMatcher($routes, new RequestContext());
     $dispatcher = new EventDispatcher();
     $dispatcher->addSubscriber(new AuthenticationListener($container));
     $dispatcher->addSubscriber(new FirewallListener($container, $matcher));
     $dispatcher->addSubscriber(new RouterListener($matcher));
     $resolver = new MyControllerResolver($container);
     $kernel = new HttpKernel($dispatcher, $resolver);
     $response = $kernel->handle($request);
     $response->send();
     $kernel->terminate($request, $response);
     if (DEV_MODE) {
         $container->get('profiler')->showProfiler();
     }
 }
 public function testTerminate()
 {
     $dispatcher = new EventDispatcher();
     $kernel = new HttpKernel($dispatcher, $this->getResolver());
     $dispatcher->addListener(KernelEvents::TERMINATE, function ($event) use(&$called, &$capturedKernel, &$capturedRequest, &$capturedResponse) {
         $called = true;
         $capturedKernel = $event->getKernel();
         $capturedRequest = $event->getRequest();
         $capturedResponse = $event->getResponse();
     });
     $kernel->terminate($request = Request::create('/'), $response = new Response());
     $this->assertTrue($called);
     $this->assertEquals($kernel, $capturedKernel);
     $this->assertEquals($request, $capturedRequest);
     $this->assertEquals($response, $capturedResponse);
 }
示例#7
0
文件: index.php 项目: itav/httpkernel
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
$routes = new RouteCollection();
$routes->add('hello', new Route('/add/{param1}', array('_controller' => function (Request $request) {
    return new Response("asdfasdf" . $request->get("param1"));
})));
$routes->add('hello', new Route('/', array('_controller' => function (Request $request) {
    return new Response("Asdf");
})));
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
ErrorHandler::register();
ExceptionHandler::register();
$request = Request::createFromGlobals();
$matcher = new UrlMatcher($routes, new RequestContext());
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new RouterListener($matcher));
$resolver = new ControllerResolver();
$kernel = new HttpKernel($dispatcher, $resolver);
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
示例#8
0
 public function run($group = null, $controller = null, $function = null)
 {
     // 注册框架运行的服务.
     $this->handle();
     // 请求参数
     $request = Request::capture();
     // 设置请求
     $this->app['request'] = $request;
     // 入口文件
     $baseUrl = $request->getBaseUrl();
     $this->filename = empty($baseUrl) ? $request->getSchemeAndHttpHost() . $request->server('SCRIPT_FILENAME') : $request->getSchemeAndHttpHost() . $baseUrl;
     if ($path_info = trim($request->getPathInfo(), '/')) {
         $args = explode('/', $path_info);
         define('BY_PATH_INFO', true);
         if (count($args) == 1) {
             $controller = $controller ?: $args[0];
         } elseif (count($args) >= 2) {
             $controller = $controller ?: $args[0];
             $function = $function ?: $args[1];
         }
     }
     // ======== 开始调度,设置调度那个控制器===============
     $group = ucfirst($group) ?: 'Web';
     $controller = $controller ?: $request->get('c', 'DefaultController');
     $controller = strrpos($controller, 'Controller') !== false ? $controller : $controller . 'Controller';
     $function = $function ?: $request->get('m', 'index');
     $class = '\\App\\Controllers\\' . $group . '\\' . ucfirst($controller);
     $load_class = $class . "::" . $function;
     // ======== 基础控制器设置结束 ========================
     // 检查控制器类存在不
     if (!class_exists($class)) {
         $class = '\\App\\Controllers\\' . $group . '\\NoFoundController';
         $function = 'index';
         $load_class = $class . '::' . $function;
     }
     // 如果没有找到类方法,设置为404
     if (!is_callable([$class, $function])) {
         $class = NoFoundController::class;
         $function = 'index';
         $load_class = NoFoundController::class . '::' . $function;
     }
     // 事件调度器
     $dispatcher = new EventDispatcher();
     // 调度控制器参数事件
     $dispatcher->addListener(KernelEvents::CONTROLLER_ARGUMENTS, function ($event) {
         // 获取调度的参数
         $arguments = $event->getArguments();
         // 获取调度的控制器
         $controller = $event->getController();
         // 获取类
         $class = $controller[0];
         // 获取方法
         $function = $controller[1];
         // 检查调度方法在不在,在的话转发到调度方法去
         if (method_exists($class, '_do_action')) {
             $controller = [$class, '_do_action'];
             // 将方法设置为第一个参数
             array_unshift($arguments, $function);
             // 重新调度
             $event->setController($controller);
             $event->setArguments($arguments);
         }
     });
     // 设置视图事件回调
     $dispatcher->addListener(KernelEvents::VIEW, function ($event) {
         $result = $event->getControllerResult();
         // 重置视图的错误报告,忽略掉NOTICE的,视图太多NOTIC了
         $error_report = error_reporting();
         error_reporting(E_ALL ^ E_NOTICE);
         $event->setResponse(new Response($result instanceof View ? $result->render() : $result));
         // 恢复原来的错误报告
         error_reporting($error_report);
     });
     // 设置异常回调
     $dispatcher->addListener(KernelEvents::EXCEPTION, function ($event) {
         // 获取异常
         $e = $event->getException();
         // 如果是自定义异常
         if ($e instanceof HttpResponseException) {
             // 设置成正常响应
             $event->setResponse($e->getResponse());
         }
     });
     // 以下是Symfony默认HTTP调度,无需修改
     $controllerResolver = new ControllerResolver();
     $argumentResolver = new ArgumentResolver();
     $kernel = new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver);
     $request->attributes->set('_controller', $load_class);
     $response = $kernel->handle($request);
     $response->send();
     $kernel->terminate($request, $response);
 }
示例#9
0
 public function handle()
 {
     $resolver = new Resolver();
     $kernel = new HttpKernel($this->getDispatcher(), $resolver);
     $response = $kernel->handle($this->getRequest());
     $response->send();
     $kernel->terminate($this->getRequest(), $response);
 }