Skip to content

Commit

Permalink
Initial (untested) work on dispatching module/controller/actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
trq committed Jan 31, 2013
1 parent bcd6b0d commit 9d9e692
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
52 changes: 36 additions & 16 deletions lib/Proem/Dispatch/Dispatcher.php
Expand Up @@ -31,12 +31,18 @@

use \Symfony\Component\HttpFoundation\Request;
use Proem\Service\AssetManagerInterface;
use Proem\Routing\RouteManagerInterface;

/**
* The default dispatcher.
*/
class Dispatcher implements DispatcherInterface
{
/**
* Store the class mapping string
*/
protected $mapping = "\\Module\\{:module}\\Controller\\{:controller}";

/**
* Store the asset manager.
*
Expand All @@ -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;
}

/**
Expand All @@ -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];
}
}
}
}
14 changes: 5 additions & 9 deletions lib/Proem/Dispatch/DispatcherInterface.php
Expand Up @@ -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.
Expand All @@ -41,19 +42,14 @@ interface DispatcherInterface extends HttpKernelInterface
/**
* 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.
Expand Down
4 changes: 3 additions & 1 deletion lib/Proem/Dispatch/Tests/DispatcherTest.php
Expand Up @@ -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));
}
}

0 comments on commit 9d9e692

Please sign in to comment.