1616use PPI \Framework \Router \ChainRouter ;
1717use PPI \Framework \ServiceManager \ServiceManager ;
1818use PPI \Framework \ServiceManager \ServiceManagerBuilder ;
19+ use PPI \Framework \Debug \ExceptionHandler ;
1920use Symfony \Component \Debug \Debug ;
2021use Symfony \Component \HttpFoundation \Response as SymfonyResponse ;
2122use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
23+ use Symfony \Component \HttpKernel \KernelInterface ;
2224use Symfony \Component \Routing \Exception \ResourceNotFoundException ;
2325
2426/**
@@ -120,6 +122,11 @@ class App implements AppInterface
120122 */
121123 private $ router ;
122124
125+ /**
126+ * @var KernelInterface
127+ */
128+ private $ symfonyKernel ;
129+
123130 /**
124131 * App constructor.
125132 *
@@ -165,6 +172,10 @@ public function boot()
165172 return $ this ;
166173 }
167174
175+ if ($ this ->isDebug ()) {
176+ ExceptionHandler::register (true , 'UTF-8 ' , 'PPI Framework ' , self ::VERSION , true );
177+ }
178+
168179 $ this ->serviceManager = $ this ->buildServiceManager ();
169180 $ this ->log ('debug ' , sprintf ('Booting %s ... ' , $ this ->name ));
170181
@@ -213,7 +224,19 @@ public function run(HttpRequest $request = null, HttpResponse $response = null)
213224 $ response = new HttpResponse ();
214225 }
215226
216- $ response = $ this ->dispatch ($ request , $ response );
227+ // Create a copy of request, as it's by-ref passed into $this->dispatch() and gets modified.
228+ $ cleanRequest = clone $ request ;
229+ try {
230+ $ response = $ this ->dispatch ($ request , $ response );
231+ } catch (ResourceNotFoundException $ e ) {
232+
233+ if ($ this ->symfonyKernel === null ) {
234+ throw $ e ;
235+ }
236+ $ response = $ this ->symfonyKernel ->handle ($ cleanRequest );
237+ }
238+
239+
217240 $ response ->send ();
218241
219242 return $ response ;
@@ -235,8 +258,18 @@ public function dispatch(HttpRequest $request, HttpResponse $response)
235258 $ this ->boot ();
236259 }
237260
238- // Routing
239- $ routeParams = $ this ->handleRouting ($ request );
261+
262+ // cache like a mother fucker
263+ // if(!$this->hasRouteInCache($request)) {
264+ $ routeParams = $ this ->handleRouting ($ request );
265+ // $this->setRouteInCache($request, $routeParams);
266+ // @todo - move these 2 lines to setRouteInCache()
267+ // $routingCache = $this->serviceManager->get('RoutingCache');
268+ // $routingCache->set($request->getPathInfo(), $routeParams);
269+ // } else {
270+ // $routeParams = $this->getRouteFromCache($request);
271+ // }
272+
240273 $ request ->attributes ->add ($ routeParams );
241274
242275 // Resolve our Controller
@@ -505,11 +538,22 @@ public function getConfig()
505538 return $ this ->serviceManager ->get ('Config ' );
506539 }
507540
541+ /**
542+ * @return string
543+ */
508544 public function serialize ()
509545 {
510546 return serialize (array ($ this ->environment , $ this ->debug ));
511547 }
512548
549+ /**
550+ * @param KernelInterface $kernel
551+ */
552+ public function setSymfonyKernel (KernelInterface $ kernel )
553+ {
554+ $ this ->symfonyKernel = $ kernel ;
555+ }
556+
513557 public function unserialize ($ data )
514558 {
515559 list ($ environment , $ debug ) = unserialize ($ data );
0 commit comments