Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use HttpKernel class as Framework.

  • Loading branch information...
commit a416e4ed5acc369899f9604f1e9a280f0c3d211d 1 parent 5760c96
Toon Ketels authored

Showing 2 changed files with 16 additions and 54 deletions. Show diff stats Hide diff stats

  1. +5 35 src/Simplex/Framework.php
  2. +11 19 web/index.php
40 src/Simplex/Framework.php
@@ -15,40 +15,10 @@
15 15
16 16 use Symfony\Component\HttpKernel\HttpKernelInterface;
17 17
18   -class Framework implements HttpKernelInterface
  18 +use Symfony\Component\HttpKernel\HttpKernel;
  19 +
  20 +
  21 +// Subclass HttpKernel
  22 +class Framework extends HttpKernel
19 23 {
20   - protected $matcher;
21   - protected $resolver;
22   - protected $dispatcher;
23   -
24   - // Type hint to interfaces for easier testing => they should implement some methods.
25   - public function __construct(EventDispatcher $dispatcher, UrlMatcherInterface $matcher, ControllerResolverInterface $resolver)
26   - {
27   - $this->dispatcher = $dispatcher;
28   - $this->matcher = $matcher;
29   - $this->resolver = $resolver;
30   - }
31   -
32   - // HttpKernelInterface...
33   - public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
34   - {
35   - try {
36   - $request->attributes->add($this->matcher->match($request->getPathInfo()));
37   -
38   - $controller = $this->resolver->getController($request);
39   - $arguments = $this->resolver->getArguments($request, $controller);
40   -
41   - $response = call_user_func_array($controller, $arguments);
42   - } catch (ResourceNotFoundException $e) {
43   - $response = new Response('Page not found.', 404);
44   - } catch (\Exception $e) {
45   - $response = new Response('Internal error.', 500);
46   - }
47   -
48   - // dispatch a response event, pass response and requests objects
49   - // for others to modify
50   - $this->dispatcher->dispatch('response', new ResponseEvent($response, $request));
51   -
52   - return $response;
53   - }
54 24 }
30 web/index.php
@@ -2,38 +2,30 @@
2 2
3 3 require_once __DIR__.'/../vendor/autoload.php';
4 4
5   -
6 5 use Symfony\Component\HttpFoundation\Request;
7 6 use Symfony\Component\HttpFoundation\Response;
8 7 use Symfony\Component\Routing;
9 8 use Symfony\Component\HttpKernel;
10 9 use Symfony\Component\EventDispatcher\EventDispatcher;
11 10
12   -use Symfony\Component\HttpKernel\HttpCache\HttpCache;
13   -use Symfony\Component\HttpKernel\HttpCache\Store;
14   -
15   -// Create eventdispatcher
16   -$dispatcher = new EventDispatcher();
17   -// Add eventsubscribers
18   -$dispatcher->addSubscriber(new Simplex\GoogleListener());
19   -$dispatcher->addSubscriber(new Simplex\ContentLengthListener());
20   -
21 11 $request = Request::createFromGlobals();
22 12 $routes = include __DIR__.'/../src/app.php';
23 13
24   -// Instead of specifically "use" it, grab it via Routing\...
25 14 $context = new Routing\RequestContext();
26   -$context->fromRequest($request);
27 15 $matcher = new Routing\Matcher\UrlMatcher($routes, $context);
28 16 $resolver = new HttpKernel\Controller\ControllerResolver();
29 17
30   -// Use Framework to handle request
31   -$framework = new Simplex\Framework($dispatcher, $matcher, $resolver);
32   -// We just add HttpCaching support, implements HttpCacheInterface and
33   -// wraps another HttpCacheInterface implementing object (our app)
34   -$framework = new HttpCache($framework, new Store(__DIR__.'/../cache'));
35   -$response = $framework->handle($request);
  18 +$dispatcher = new EventDispatcher();
  19 +$dispatcher->addSubscriber(new HttpKernel\EventListener\RouterListener($matcher));
36 20
  21 +$errorHandler = function (HttpKernel\Exception\FlattenException $exception) {
  22 + $msg = 'Something went wrong!('.$exception->getMessage().')';
37 23
38   -$response->send();
  24 + return new Response($msg, $exception->getStatusCode());
  25 +};
  26 +$dispatcher->addSubscriber(new HttpKernel\EventListener\ExceptionListener($errorHandler));
  27 +
  28 +$framework = new Simplex\Framework($dispatcher, $resolver);
39 29
  30 +$response = $framework->handle($request);
  31 +$response->send();

0 comments on commit a416e4e

Please sign in to comment.
Something went wrong with that request. Please try again.