Skip to content

Commit 9dec3ce

Browse files
committed
Merge pull request #165 from ppi/feature/chain-router-caching
Symfony Kernel Injection For Bundle Loading
2 parents 5ea837a + 9acf1e7 commit 9dec3ce

File tree

8 files changed

+71
-7
lines changed

8 files changed

+71
-7
lines changed

src/App.php

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
use PPI\Framework\Router\ChainRouter;
1717
use PPI\Framework\ServiceManager\ServiceManager;
1818
use PPI\Framework\ServiceManager\ServiceManagerBuilder;
19+
use PPI\Framework\Debug\ExceptionHandler;
1920
use Symfony\Component\Debug\Debug;
2021
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
2122
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23+
use Symfony\Component\HttpKernel\KernelInterface;
2224
use 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);

src/Autoload.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Autoload
4141
*
4242
* @static
4343
*/
44-
protected static $_options = array();
44+
public static $_options = array();
4545

4646
/**
4747
* @todo Add inline documentation.
@@ -77,7 +77,7 @@ public static function config(array $config)
7777
public static function add($key, $path)
7878
{
7979
self::$_registeredNamespaces[$key] = true;
80-
self::$_options['loader']->add($key, $path);
80+
self::$_options['loader']->addPsr4($key, $path);
8181
}
8282

8383
/**

src/Config/ConfigLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PPI\Framework\Config\Loader\IniFileLoader;
1616
use PPI\Framework\Config\Loader\PhpFileLoader;
1717
use PPI\Framework\Config\Loader\YamlFileLoader;
18+
use Symfony\Component\Config\Loader\LoaderInterface;
1819
use Symfony\Component\Config\Loader\LoaderResolver;
1920

2021
/**
@@ -62,7 +63,7 @@ public function load($resource, $type = null)
6263
*
6364
* @return DelegatingLoader The loader
6465
*/
65-
protected function getLoader()
66+
public function getLoader()
6667
{
6768
if (null === $this->loader) {
6869
$locator = new FileLocator($this->paths);

src/Config/Loader/YamlFileLoader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\Config\Loader\FileLoader;
1414
use Symfony\Component\Yaml\Parser as YamlParser;
1515
use Zend\Stdlib\ArrayUtils;
16+
use \InvalidArgumentException;
1617

1718
/**
1819
* YamlFileLoader loads app configuration from a YAML file.

src/Module/Controller/ControllerResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace PPI\Framework\Module\Controller;
1212

1313
use Psr\Log\LoggerInterface;
14+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1415
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
1516
use Zend\ServiceManager\ServiceLocatorAwareInterface;
1617
use Zend\ServiceManager\ServiceLocatorInterface;
@@ -89,6 +90,10 @@ protected function instantiateController($class)
8990
$controller->setServiceLocator($this->serviceManager);
9091
}
9192

93+
if($controller instanceof ContainerAwareInterface) {
94+
$controller->setContainer($this->serviceManager->get('SymfonyContainer'));
95+
}
96+
9297
return $controller;
9398
}
9499
}

src/Router/ChainRouter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class ChainRouter extends BaseChainRouter
2626
*/
2727
protected $matchedRouteRequest;
2828

29+
private $routingCache;
30+
2931
/**
3032
* @param array $parameters
3133
*
@@ -71,10 +73,12 @@ public function matchRequest(Request $request)
7173
try {
7274
$parameters = parent::matchRequest($request);
7375
$this->matchedRouteRequest = $request;
74-
7576
return $parameters;
7677
} catch (\Exception $e) {
7778
throw $e;
7879
}
80+
81+
7982
}
83+
8084
}

src/Router/Wrapper/AuraRouterWrapper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
*/
2626
class AuraRouterWrapper implements RouterInterface, RequestMatcherInterface
2727
{
28+
public function getRouter()
29+
{
30+
return self::class;
31+
}
32+
2833
/**
2934
* @var AuraRouter
3035
*/

src/ServiceManager/Factory/RouterFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public function createService(ServiceLocatorInterface $serviceLocator)
5050
$logger = $serviceLocator->has('Logger') ? $serviceLocator->get('Logger') : null;
5151

5252
$chainRouter = new ChainRouter($logger);
53+
if($serviceLocator->has('RoutingCache')) {
54+
$chainRouter->setCache($serviceLocator->get('RoutingCache'));
55+
}
56+
5357
$chainRouter->setContext($requestContext);
5458

5559
$allModuleRoutes = $serviceLocator->get('ModuleDefaultListener')->getRoutes();

0 commit comments

Comments
 (0)