     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:         $event = new ActionEvent($request, $action);
    68:         $this->dispatcher->dispatch(TheliaEvents::ACTION, $event);
    69:         if ($event->hasController()) {
    70:             $controller = $event->getController();
    71:         }
    72:
    73: /*        switch ($action) {
    74:             case 'addProduct':
    75:                 $controller = array(
    76:                     new Cart($this->dispatcher),
    77:                     "addCart"
    78:                 );
    79:                 break;
    80:             default :
    81:                 $event = new ActionEvent($request, $action);
    82:                 $this->dispatcher->dispatch(TheliaEvents::ACTION, $event);
    83:                 if ($event->hasController()) {
    84:                     $controller = $event->getController();
    85:                 }
    86:                 break;
    87:         }*/
    88:
    89:         if ($controller) {
    90:             return array(
    91:                 '_controller' => $controller
    92:             );
    93:         }
    94:
    95:         throw new ResourceNotFoundException("No action parameter found");
    96:
    97:     }
    98: }
    99: