Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'feature/dispatch' into develop

  • Loading branch information...
commit 2bfb1cc346939025b242e6121de701144d545ec2 2 parents bcd6b0d + 9d9e692
@trq trq authored
View
52 lib/Proem/Dispatch/Dispatcher.php
@@ -31,6 +31,7 @@
use \Symfony\Component\HttpFoundation\Request;
use Proem\Service\AssetManagerInterface;
+use Proem\Routing\RouteManagerInterface;
/**
* The default dispatcher.
@@ -38,6 +39,11 @@
class Dispatcher implements DispatcherInterface
{
/**
+ * Store the class mapping string
+ */
+ protected $mapping = "\\Module\\{:module}\\Controller\\{:controller}";
+
+ /**
* Store the asset manager.
*
* @var Proem\Service\AssetManagerInterface
@@ -45,36 +51,36 @@ class Dispatcher implements DispatcherInterface
protected $assetManager;
/**
- * Store the current payload.
+ * Store the router.
*
- * @var array $payload
+ * @var Proem\Routing\RouteManagerInterface
*/
- protected $payload;
+ protected $routeManager;
/**
- * Setup the dispatcher
+ * Store any failures
+ *
+ * @var array
*/
- public function __construct(AssetManagerInterface $assetManager)
- {
- $this->assetManager = $assetManager;
- }
+ protected $failures;
/**
- * Set the current payload data.
+ * Setup the dispatcher
*/
- public function setPayload(array $payload = [])
+ public function __construct(AssetManagerInterface $assetManager, RouteManagerInterface $routeManager)
{
- $this->payload = $payload;
+ $this->assetManager = $assetManager;
+ $this->routeManager = $routeManager;
}
/**
- * Test to see if the current payload is dispatchable.
+ * Return any failed routes.
*
- * @return bool
+ * @return array
*/
- public function isDispatchable()
+ public function getFailures()
{
-
+ return $this->failures;
}
/**
@@ -84,6 +90,20 @@ public function isDispatchable()
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
-
+ while ($route = $this->router->route($request)) {
+ $module = $route->getPayload()['module'];
+ $controller = $route->getPayload()['controller'];
+ $action = $route->getPayload()['action'];
+ $class = str_replace(
+ ['{:module}', '{:controller}'],
+ [$module, $controller],
+ $this->mapping
+ );
+ try {
+ return $this->assetManager->resolve($class, ['invoke' => $action]);
+ } catch (\LogicException $e) {
+ $this->failures[] = ['route' => $route, 'exception' => $e];
+ }
+ }
}
}
View
14 lib/Proem/Dispatch/DispatcherInterface.php
@@ -32,6 +32,7 @@
use \Symfony\Component\HttpKernel\HttpKernelInterface;
use \Symfony\Component\HttpFoundation\Request;
use Proem\Service\AssetManagerInterface;
+use Proem\Routing\RouteManagerInterface;
/**
* Interface all dispatchers must implement.
@@ -41,19 +42,14 @@
/**
* Setup the dispatcher
*/
- public function __construct(AssetManagerInterface $assetManager);
+ public function __construct(AssetManagerInterface $assetManager, RouteManagerInterface $routeManager);
/**
- * Set the current payload data.
- */
- public function setPayload(array $payload = []);
-
- /**
- * Test to see if the current payload is dispatchable.
+ * Return any failed routes.
*
- * @return bool
+ * @return array
*/
- public function isDispatchable();
+ public function getFailures();
/**
* Handles a Request, converting it to a Response.
View
4 lib/Proem/Dispatch/Tests/DispatcherTest.php
@@ -32,14 +32,16 @@
class DispatcherTest extends \PHPUnit_Framework_TestCase
{
protected $asssetManager;
+ protected $routeManager;
public function setUp()
{
$this->assetManager = m::mock('\Proem\Service\AssetManagerInterface');
+ $this->routeManager = m::mock('\Proem\Routing\RouteManagerInterface');
}
public function testCanInstantiate()
{
- $this->assertInstanceOf('Proem\Dispatch\DispatcherInterface', new Dispatcher($this->assetManager));
+ $this->assertInstanceOf('Proem\Dispatch\DispatcherInterface', new Dispatcher($this->assetManager, $this->routeManager));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.