implement action factory

This commit is contained in:
Manuel Raynaud
2013-05-21 15:24:13 +02:00
parent 5afc54096a
commit c44f9a78a1
5 changed files with 156 additions and 4 deletions

View File

@@ -32,7 +32,7 @@ use Symfony\Component\HttpFoundation\Request;
* call setAction if action match yours
*
*/
class ActionEvent extends Event
abstract class ActionEvent extends Event
{
/**

View File

@@ -0,0 +1,53 @@
<?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;
class ActionEventClass extends Event
{
protected $className;
protected $action;
public function __construct($action)
{
$this->action = $action;
}
public function setClassName($className)
{
$this->className = $className;
}
public function hasClassName()
{
return $this->className !== null;
}
public function getClassName()
{
return $this->className;
}
}

View File

@@ -0,0 +1,16 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: manu
* Date: 21/05/13
* Time: 12:35
* To change this template use File | Settings | File Templates.
*/
namespace Thelia\Core\Event;
use Thelia\Core\Event\ActionEvent;
class DefaultActionEvent extends ActionEvent {
}

View File

@@ -25,9 +25,19 @@ namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Thelia\Core\Event\ActionEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Factory\ActionEventFactory;
/**
*
* Action are dispatch here.
*
* A factory is used for creating appropriate action object
*
* Class ControllerListener
* @package Thelia\Core\EventListener
*/
class ControllerListener implements EventSubscriberInterface
{
@@ -38,8 +48,8 @@ class ControllerListener implements EventSubscriberInterface
$request = $event->getRequest();
if (false !== $action = $request->get("action")) {
//search corresponding action
$event = new ActionEvent($request, $action);
$dispatcher->dispatch("action.".$action, $event);
$event = new ActionEventFactory($request, $action, $event->getDispatcher());
$dispatcher->dispatch("action.".$action, $event->createActionEvent());
}
}

View File

@@ -0,0 +1,73 @@
<?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\Factory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Thelia\Core\Event\ActionEventClass;
class ActionEventFactory
{
protected $request;
protected $action;
protected $dispatcher;
public function __construct(Request $request, $action, EventDispatcherInterface $dispatcher)
{
$this->request = $request;
$this->action = $action;
$this->dispatcher = $dispatcher;
}
public function createActionEvent()
{
$className = "Thelia\\Core\\Event\\".$this->action."Event";
$class = null;
if (class_exists($className)) {
$class = new \ReflectionClass($className);
// return $class->newInstance($this->request, $this->action);
} else {
$actionEventClass = new ActionEventClass($this->action);
$this->dispatcher->dispatch("action.searchClass", $actionEventClass);
if ($actionEventClass->hasClassName()) {
$class = new \ReflectionClass($className);
}
}
if( is_null($class)) {
$class = new \ReflectionClass("Thelia\Core\Event\DefaultActionEvent");
}
if ($class->isSubclassOf("Thelia\Core\Event\ActionEvent") === false) {
}
return $class->newInstance($this->request, $this->action);
}
}