Permalink
Browse files

Use HttpKernel class as Framework.

  • Loading branch information...
1 parent 5760c96 commit a416e4ed5acc369899f9604f1e9a280f0c3d211d Toon Ketels committed Jun 1, 2012
Showing with 16 additions and 54 deletions.
  1. +5 −35 src/Simplex/Framework.php
  2. +11 −19 web/index.php
View
40 src/Simplex/Framework.php
@@ -15,40 +15,10 @@
use Symfony\Component\HttpKernel\HttpKernelInterface;
-class Framework implements HttpKernelInterface
+use Symfony\Component\HttpKernel\HttpKernel;
+
+
+// Subclass HttpKernel
+class Framework extends HttpKernel
{
- protected $matcher;
- protected $resolver;
- protected $dispatcher;
-
- // Type hint to interfaces for easier testing => they should implement some methods.
- public function __construct(EventDispatcher $dispatcher, UrlMatcherInterface $matcher, ControllerResolverInterface $resolver)
- {
- $this->dispatcher = $dispatcher;
- $this->matcher = $matcher;
- $this->resolver = $resolver;
- }
-
- // HttpKernelInterface...
- public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
- {
- try {
- $request->attributes->add($this->matcher->match($request->getPathInfo()));
-
- $controller = $this->resolver->getController($request);
- $arguments = $this->resolver->getArguments($request, $controller);
-
- $response = call_user_func_array($controller, $arguments);
- } catch (ResourceNotFoundException $e) {
- $response = new Response('Page not found.', 404);
- } catch (\Exception $e) {
- $response = new Response('Internal error.', 500);
- }
-
- // dispatch a response event, pass response and requests objects
- // for others to modify
- $this->dispatcher->dispatch('response', new ResponseEvent($response, $request));
-
- return $response;
- }
}
View
30 web/index.php
@@ -2,38 +2,30 @@
require_once __DIR__.'/../vendor/autoload.php';
-
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing;
use Symfony\Component\HttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcher;
-use Symfony\Component\HttpKernel\HttpCache\HttpCache;
-use Symfony\Component\HttpKernel\HttpCache\Store;
-
-// Create eventdispatcher
-$dispatcher = new EventDispatcher();
-// Add eventsubscribers
-$dispatcher->addSubscriber(new Simplex\GoogleListener());
-$dispatcher->addSubscriber(new Simplex\ContentLengthListener());
-
$request = Request::createFromGlobals();
$routes = include __DIR__.'/../src/app.php';
-// Instead of specifically "use" it, grab it via Routing\...
$context = new Routing\RequestContext();
-$context->fromRequest($request);
$matcher = new Routing\Matcher\UrlMatcher($routes, $context);
$resolver = new HttpKernel\Controller\ControllerResolver();
-// Use Framework to handle request
-$framework = new Simplex\Framework($dispatcher, $matcher, $resolver);
-// We just add HttpCaching support, implements HttpCacheInterface and
-// wraps another HttpCacheInterface implementing object (our app)
-$framework = new HttpCache($framework, new Store(__DIR__.'/../cache'));
-$response = $framework->handle($request);
+$dispatcher = new EventDispatcher();
+$dispatcher->addSubscriber(new HttpKernel\EventListener\RouterListener($matcher));
+$errorHandler = function (HttpKernel\Exception\FlattenException $exception) {
+ $msg = 'Something went wrong!('.$exception->getMessage().')';
-$response->send();
+ return new Response($msg, $exception->getStatusCode());
+};
+$dispatcher->addSubscriber(new HttpKernel\EventListener\ExceptionListener($errorHandler));
+
+$framework = new Simplex\Framework($dispatcher, $resolver);
+$response = $framework->handle($request);
+$response->send();

0 comments on commit a416e4e

Please sign in to comment.