1: <?php
2: /*************************************************************************************/
3: /* */
4: /* Thelia */
5: /* */
6: /* Copyright (c) OpenStudio */
7: /* email : info@thelia.net */
8: /* web : http://www.thelia.net */
9: /* */
10: /* This program is free software; you can redistribute it and/or modify */
11: /* it under the terms of the GNU General Public License as published by */
12: /* the Free Software Foundation; either version 3 of the License */
13: /* */
14: /* This program is distributed in the hope that it will be useful, */
15: /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
16: /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
17: /* GNU General Public License for more details. */
18: /* */
19: /* You should have received a copy of the GNU General Public License */
20: /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
21: /* */
22: /*************************************************************************************/
23:
24: namespace Thelia\Routing\Matcher;
25:
26: use Symfony\Component\HttpFoundation\Request;
27: use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
28: use Symfony\Component\Routing\Exception\ResourceNotFoundException;
29: use Symfony\Component\EventDispatcher\EventDispatcherInterface;
30:
31: use Thelia\Core\Event\ActionEvent;
32: use Thelia\Core\Event\TheliaEvents;
33: use Thelia\Action\Cart;
34:
35: /**
36: * 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.
37: *
38: * @author Manuel Raynaud <mraynaud@openstudio.fr>
39: */
40:
41: class ActionMatcher implements RequestMatcherInterface
42: {
43: /**
44: *
45: * @var Symfony\Component\EventDispatcher\EventDispatcherInterface
46: */
47: protected $dispatcher;
48:
49: public function setDispatcher(EventDispatcherInterface $dispatcher)
50: {
51: $this->dispatcher = $dispatcher;
52: }
53:
54: public function matchRequest(Request $request)
55: {
56: if (false !== $action = $request->get("action")) {
57: //search corresponding action
58: return $this->dispatchAction($request, $action);
59: }
60:
61: throw new ResourceNotFoundException("No action parameter found");
62: }
63:
64: protected function dispatchAction(Request $request, $action)
65: {
66: $controller = null;
67: switch ($action) {
68: case 'addProduct':
69: $controller = array(
70: new Cart($this->dispatcher),
71: "addCart"
72: );
73: break;
74: default :
75: $event = new ActionEvent($request, $action);
76: $this->dispatcher->dispatch(TheliaEvents::ACTION, $event);
77: if ($event->hasController()) {
78: $controller = $event->getController();
79: }
80: break;
81: }
82:
83: if ($controller) {
84: return array(
85: '_controller' => $controller
86: );
87: }
88:
89: throw new ResourceNotFoundException("No action parameter found");
90:
91: }
92: }
93: