move Loop Element from Tpex to Template namespace
This commit is contained in:
103
core/lib/Thelia/Core/Template/Element/BaseLoop.php
Normal file
103
core/lib/Thelia/Core/Template/Element/BaseLoop.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?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\Template\Element;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class BaseLoop
|
||||
* @package Thelia\Tpex\Element\Loop
|
||||
*/
|
||||
abstract class BaseLoop
|
||||
{
|
||||
/**
|
||||
* @var \Symfony\Component\HttpFoundation\Request
|
||||
*/
|
||||
public $request;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
|
||||
*/
|
||||
public $dispatcher;
|
||||
|
||||
/**
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
|
||||
*/
|
||||
public function __construct(Request $request, EventDispatcherInterface $dispatcher)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->dispatcher = $dispatcher;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* this function have to be implement in your own loop class.
|
||||
*
|
||||
* All your parameters are defined in defineArgs() and can be accessible like a class property.
|
||||
*
|
||||
* example :
|
||||
*
|
||||
* public function defineArgs()
|
||||
* {
|
||||
* return array (
|
||||
* "ref",
|
||||
* "id" => "optional",
|
||||
* "stock" => array(
|
||||
* "optional",
|
||||
* "default" => 10
|
||||
* )
|
||||
* );
|
||||
* }
|
||||
*
|
||||
* you can retrieve ref value using $this->ref
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
abstract public function exec();
|
||||
|
||||
/**
|
||||
*
|
||||
* define all args used in your loop
|
||||
*
|
||||
* array key is your arg name.
|
||||
*
|
||||
* example :
|
||||
*
|
||||
* return array (
|
||||
* "ref",
|
||||
* "id" => "optional",
|
||||
* "stock" => array(
|
||||
* "optional",
|
||||
* "default" => 10
|
||||
* )
|
||||
* );
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract public function defineArgs();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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\Template\Element\Exception;
|
||||
|
||||
class ElementNotFoundException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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\Template\Element\Exception;
|
||||
|
||||
class InvalidElementException extends \RuntimeException
|
||||
{
|
||||
|
||||
}
|
||||
108
core/lib/Thelia/Core/Template/Element/LoopResult.php
Normal file
108
core/lib/Thelia/Core/Template/Element/LoopResult.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?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\Template\Element;
|
||||
|
||||
use Thelia\Core\Template\Element\LoopResultRow;
|
||||
|
||||
class LoopResult implements \Iterator
|
||||
{
|
||||
private $position;
|
||||
protected $collection = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
|
||||
public function isEmpty()
|
||||
{
|
||||
return count($this->collection) == 0;
|
||||
}
|
||||
|
||||
public function addRow(LoopResultRow $row)
|
||||
{
|
||||
$this->collection[] = $row;
|
||||
}
|
||||
|
||||
public function getCount()
|
||||
{
|
||||
return count($this->collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Return the current element
|
||||
* @link http://php.net/manual/en/iterator.current.php
|
||||
* @return \Thelia\Core\Template\Element\LoopResultRow
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->collection[$this->position];
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Move forward to next element
|
||||
* @link http://php.net/manual/en/iterator.next.php
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
++$this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Return the key of the current element
|
||||
* @link http://php.net/manual/en/iterator.key.php
|
||||
* @return mixed scalar on success, or null on failure.
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->position;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Checks if current position is valid
|
||||
* @link http://php.net/manual/en/iterator.valid.php
|
||||
* @return boolean The return value will be casted to boolean and then evaluated.
|
||||
* Returns true on success or false on failure.
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return isset($this->collection[$this->position]);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Rewind the Iterator to the first element
|
||||
* @link http://php.net/manual/en/iterator.rewind.php
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
}
|
||||
46
core/lib/Thelia/Core/Template/Element/LoopResultRow.php
Normal file
46
core/lib/Thelia/Core/Template/Element/LoopResultRow.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?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\Template\Element;
|
||||
|
||||
|
||||
class LoopResultRow {
|
||||
|
||||
protected $substitution = array();
|
||||
|
||||
public function set($key, $value)
|
||||
{
|
||||
$this->substitution["#".$key] = $value;
|
||||
}
|
||||
|
||||
public function get($key)
|
||||
{
|
||||
return $this->substitution["#".$key];
|
||||
}
|
||||
|
||||
public function getVarVal()
|
||||
{
|
||||
return $this->substitution;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,9 +25,9 @@
|
||||
namespace Thelia\Core\Template\Loop;
|
||||
|
||||
|
||||
use Thelia\Tpex\Element\Loop\BaseLoop;
|
||||
use Thelia\Tpex\Element\Loop\LoopResult;
|
||||
use Thelia\Tpex\Element\Loop\LoopResultRow;
|
||||
use Thelia\Core\Template\Element\BaseLoop;
|
||||
use Thelia\Core\Template\Element\LoopResult;
|
||||
use Thelia\Core\Template\Element\LoopResultRow;
|
||||
use Thelia\Model\CategoryQuery;
|
||||
|
||||
/**
|
||||
@@ -94,7 +94,7 @@ class Category extends BaseLoop {
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @return \Thelia\Tpex\Element\Loop\LoopResult
|
||||
* @return \Thelia\Core\Template\Element\LoopResult
|
||||
*/
|
||||
public function exec()
|
||||
{
|
||||
|
||||
@@ -26,6 +26,9 @@ namespace Thelia\Core\Template\Smarty\Plugins;
|
||||
use Thelia\Core\Template\Smarty\SmartyPluginInterface;
|
||||
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
|
||||
|
||||
use Thelia\Core\Template\Element\Exception\ElementNotFoundException;
|
||||
use Thelia\Core\Template\Element\Exception\InvalidElementException;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
@@ -167,7 +170,7 @@ class TheliaLoop implements SmartyPluginInterface {
|
||||
* find the loop class with his name and construct an instance of this class
|
||||
*
|
||||
* @param string $name
|
||||
* @return \Thelia\Tpex\Element\Loop\BaseLoop
|
||||
* @return \Thelia\Core\Template\Element\BaseLoop
|
||||
* @throws \Thelia\Tpex\Exception\InvalidElementException
|
||||
* @throws \Thelia\Tpex\Exception\ElementNotFoundException
|
||||
*/
|
||||
@@ -180,8 +183,8 @@ class TheliaLoop implements SmartyPluginInterface {
|
||||
|
||||
$class = new \ReflectionClass($this->loopDefinition[$name]);
|
||||
|
||||
if ($class->isSubclassOf("Thelia\Tpex\Element\Loop\BaseLoop") === false) {
|
||||
throw new InvalidElementException(sprintf("%s Loop class have to extends Thelia\Tpex\Element\Loop\BaseLoop",
|
||||
if ($class->isSubclassOf("Thelia\Core\Template\Element\BaseLoop") === false) {
|
||||
throw new InvalidElementException(sprintf("%s Loop class have to extends Thelia\Core\Template\Element\BaseLoop",
|
||||
$name));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
|
||||
<config xmlns="http://thelia.net/schema/dic/config"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">
|
||||
|
||||
<testLoops>
|
||||
<testLoop name="equal" class="Test\TestLoop\Equal"/>
|
||||
</testLoops>
|
||||
|
||||
<loops>
|
||||
<loop name="foo" class="Test\Loop\Foo"/>
|
||||
<loop name="doobitch" class="Test\Loop\Doobitch"/>
|
||||
</loops>
|
||||
|
||||
</config>
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* User: manu
|
||||
* Date: 15/03/13
|
||||
* Time: 09:23
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
|
||||
namespace Test\Loop;
|
||||
|
||||
use Thelia\Tpex\Element\Loop\BaseLoop;
|
||||
use Thelia\Tpex\Tools;
|
||||
use Thelia\Model\ProductQuery;
|
||||
|
||||
class Doobitch extends BaseLoop {
|
||||
|
||||
public function defineArgs()
|
||||
{
|
||||
return array(
|
||||
"param1",
|
||||
"param2" => array("default" => "foo")
|
||||
);
|
||||
}
|
||||
|
||||
public function exec($text)
|
||||
{
|
||||
|
||||
$res = "";
|
||||
if($this->param1 == 2 || $this->param1 == 3) {
|
||||
for($i = 0; $i < 4; $i++) {
|
||||
$tmp = str_replace("#ALFRED", "foo".$i, $text);
|
||||
if($i%2){
|
||||
$tmp = str_replace("#CHAPO", "bar".$i, $tmp);
|
||||
} else {
|
||||
$tmp = str_replace("#CHAPO", "", $tmp);
|
||||
}
|
||||
|
||||
|
||||
$res .= $tmp;
|
||||
}
|
||||
}
|
||||
|
||||
echo $this->param2;
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* User: manu
|
||||
* Date: 14/03/13
|
||||
* Time: 15:16
|
||||
* To change this template use File | Settings | File Templates.
|
||||
*/
|
||||
|
||||
namespace Test\Loop;
|
||||
|
||||
use Thelia\Tpex\Element\Loop\BaseLoop;
|
||||
|
||||
class Foo extends BaseLoop {
|
||||
|
||||
public function defineArgs()
|
||||
{
|
||||
return array();
|
||||
}
|
||||
|
||||
public function exec($text)
|
||||
{
|
||||
$res = "";
|
||||
for($i = 0; $i < 4; $i++) {
|
||||
$tmp = str_replace("#TOTO", "toto".$i, $text);
|
||||
$tmp = str_replace("#TUTU", "tutu".$i, $tmp);
|
||||
|
||||
$res .= $tmp;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Test\TestLoop;
|
||||
|
||||
use Thelia\Tpex\Element\TestLoop\BaseTestLoop;
|
||||
|
||||
class Equal extends BaseTestLoop
|
||||
{
|
||||
|
||||
public function exec($variable, $value)
|
||||
{
|
||||
return $variable == $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user