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

@@ -7,6 +7,8 @@
"wiki" : "http://thelia.net/wiki"
},
"require":{
"php": ">=5.4.0",
"ircmaxell/password-compat": "dev-master",
"propel/propel1" : "1.6.*",
"psr/log" : "dev-master",
"symfony/class-loader": "2.1.*",
@@ -15,8 +17,7 @@
"symfony/dependency-injection" : "2.1.*",
"symfony/event-dispatcher" : "2.1.*",
"symfony/http-kernel" : "2.1.*",
"symfony/routing" : "2.1.*",
"symfony-cmf/routing" : "1.0.*@dev"
"symfony/routing" : "2.1.*"
},
"minimum-stability": "stable",
"config" : {

View File

@@ -0,0 +1,58 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class BaseAction
{
/**
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;
/**
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
*/
public function __construct(EventDispatcherInterface $dispatcher) {
$this->dispatcher = $dispatcher;
}
/**
*
* @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
public function getDispatcher()
{
return $this->dispatcher;
}
}

View File

@@ -0,0 +1,64 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Action;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Action\BaseAction;
class Cart extends BaseAction
{
/**
*
* add an article to cart
*
* @param \Symfony\Component\HttpFoundation\Request $request
*/
public function addCart(Request $request)
{
var_dump($this->getDispatcher()); exit;
}
/**
*
* Delete specify article present into cart
*
* @param \Symfony\Component\HttpFoundation\Request $request
*/
public function deleteArticle(Request $request)
{
}
/**
*
* Modify article's quantity
*
* @param \Symfony\Component\HttpFoundation\Request $request
*/
public function modifyArticle(Request $request)
{
}
}

View File

@@ -0,0 +1,45 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Action;
use Symfony\Component\HttpFoundation\Request;
class Customer
{
public function create(Request $request)
{
}
public function modify(Request $request)
{
}
public function modifyPassword(Request $request)
{
}
}

View File

@@ -1,5 +1,25 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Controller;
use Thelia\Controller\NullControllerInterface;

View File

@@ -101,6 +101,11 @@ class TheliaBundle extends Bundle
->addMethodCall('addSubscriber', array(new Reference('listener.router')))
->addMethodCall('addSubscriber', array(new Reference('thelia.listener.view')))
;
// TODO : save listener from plugins
$container->getDefinition('matcher.action')->addMethodCall("setDispatcher", array(new Reference('dispatcher')));
$container->register('http_kernel','Thelia\Core\TheliaHttpKernel')
->addArgument(new Reference('dispatcher'))

View File

@@ -0,0 +1,104 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;
/**
*
* Class thrown on Thelia.action event
*
* call setAction if action match yours
*
*/
class ActionEvent extends Event
{
/**
*
* @var Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
*
* @var string
*/
protected $action;
/**
*
* @var string
*/
protected $controller;
/**
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $action
*/
public function __construct(Request $request, $action) {
$this->request = $request;
$this->action = $action;
}
/**
*
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
*
* @return \Symfony\Component\HttpFoundation\Request
*/
public function getRequest()
{
return $this->request;
}
/**
*
* @param string $controller
*/
public function setController($controller)
{
$this->controller = $controller;
}
public function getController()
{
return $this->controller;
}
public function hasController()
{
return null !== $this->controller;
}
}

View File

@@ -0,0 +1,43 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\Event;
/**
*
* Class containing all Thelia events name using in Thelia Core
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
final class TheliaEvents
{
/**
* ACTION event
*
* Send if no action are already present in Thelia action process ( see Thelia\Routing\Matcher\ActionMatcher)
*/
const ACTION = "thelia.action";
}

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");
}
}