start to implement action controller.

This commit is contained in:
Manuel Raynaud
2013-02-07 17:40:27 +01:00
parent bb86533f25
commit b952888b14
9 changed files with 392 additions and 6 deletions

View File

@@ -26,22 +26,68 @@ namespace Thelia\Routing\Matcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Action\Cart;
/**
* Matcher using action param in get or post method to perform actions. For exemple index.php?action=addCart will find the good controller that can perform this action.
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ActionMatcher implements RequestMatcherInterface
{
/**
*
* @var Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;
public function setDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function matchRequest(Request $request)
{
if (false !== $action = $request->get("action")) {
//search corresponding action
return $this->dispatchAction($request, $action);
}
throw new ResourceNotFoundException("No action parameter found");
}
protected function findActionParam(Request $request)
protected function dispatchAction(Request $request, $action)
{
$controller = null;
switch ($action) {
case 'addProduct':
$controller = array(
new Cart($this->dispatcher),
"addCart"
);
break;
default :
$event = new ActionEvent($request, $action);
$event->setDispatcher($this->dispatcher);
$this->dispatcher->dispatch(TheliaEvents::ACTION, $event);
if ($event->hasController()) {
$controller = $event->getController();
}
break;
}
if ($controller) {
return array(
'_controller' => $controller
);
}
throw new ResourceNotFoundException("No action parameter found");
}
}