Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created February 24, 2016 22:15
Show Gist options
  • Save mneuhaus/3ad7fbb0c35505870401 to your computer and use it in GitHub Desktop.
Save mneuhaus/3ad7fbb0c35505870401 to your computer and use it in GitHub Desktop.
<?php
namespace TYPO3Fluid\Fluid\Tests\Functional\Cases\Escaping;
use TYPO3Fluid\Fluid\Tests\Functional\BaseFunctionalTestCase;
/**
* Class RecursiveSectionRendering
*/
class RecursiveSectionRenderingTest extends BaseFunctionalTestCase {
/**
* Variables array constructed to expect exactly three
* recursive renderings followed by a single rendering.
*
* @var array
*/
protected $variables = array(
'settings' => array(
'test' => '<strong>Bla</strong>'
),
'items' => array(
array(
'id' => '1',
'items' => array(
array(
'id' => '1.1',
'items' => array(
)
)
)
),
array(
'id' => '2',
'items' => array()
)
)
);
/**
* @return array
*/
public function getTemplateCodeFixturesAndExpectations() {
return array(
'Recursive section rendering clones variable storage and restores after loop ends' => array(
file_get_contents(__DIR__ . '/../../Fixtures/Templates/RecursiveSectionRendering.html'),
$this->variables,
array('Item: 1.', 'Item: 2.', 'Item: 3.', 'Item: 4.'),
array(),
),
);
}
}
<?php
namespace TYPO3Fluid\Fluid\Core\Variables;
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
/**
* Class StandardVariableProvider
*/
class StandardVariableProvider implements VariableProviderInterface {
/**
* Variables stored in context
*
* @var mixed
*/
protected $variables = array();
/**
* Variables, if any, with which to initialize this
* VariableProvider.
*
* @param array $variables
*/
public function __construct(array $variables = array()) {
echo __METHOD__ . chr(10);
$this->variables = $variables;
}
/**
* @param array|\ArrayAccess $variables
* @return VariableProviderInterface
*/
public function getScopeCopy($variables) {
echo __METHOD__ . chr(10);
if (!array_key_exists('settings', $variables) && array_key_exists('settings', $this->variables)) {
$variables['settings'] = $this->variables['settings'];
}
$className = get_class($this);
return new $className($variables);
}
/**
* Set the source data used by this VariableProvider. The
* source can be any type, but the type must of course be
* supported by the VariableProvider itself.
*
* @param mixed $source
* @return void
*/
public function setSource($source) {
echo __METHOD__ . chr(10);
var_dump(
'setSource' . spl_object_hash($this),
$source
);
echo chr(10) . chr(10);
$this->variables = $source;
}
/**
* @return array
*/
public function getSource() {
echo __METHOD__ . chr(10);
return $this->variables;
}
/**
* Get every variable provisioned by the VariableProvider
* implementing the interface. Must return an array or
* ArrayAccess instance!
*
* @return array|\ArrayAccess
*/
public function getAll() {
echo __METHOD__ . chr(10);
return $this->variables;
}
/**
* Add a variable to the context
*
* @param string $identifier Identifier of the variable to add
* @param mixed $value The variable's value
* @return void
* @api
*/
public function add($identifier, $value) {
echo __METHOD__ . '(' . $identifier . ')' . chr(10);
if (empty($identifier)) {
var_dump(func_get_args());
throw new \Exception('');
}
$this->variables[$identifier] = $value;
if ($identifier == 'item') {
var_dump(
'add' . spl_object_hash($this),
$identifier,
$value
// ,$this->variables
);
echo chr(10) . chr(10);
}
}
/**
* Get a variable from the context. Throws exception if variable is not found in context.
*
* If "_all" is given as identifier, all variables are returned in an array,
* if one of the other reserved variables are given, their appropriate value
* they're representing is returned.
*
* @param string $identifier
* @return mixed The variable value identified by $identifier
* @api
*/
public function get($identifier) {
echo __METHOD__ . chr(10);
$value = $this->getByPath($identifier);
return $value;
}
/**
* Get a variable by dotted path expression, retrieving the
* variable from nested arrays/objects one segment at a time.
* If the second variable is passed, it is expected to contain
* extraction method names (constants from VariableExtractor)
* which indicate how each value is extracted.
*
* @param string $path
* @return mixed
*/
public function getByPath($path, array $accessors = array()) {
echo __METHOD__ . chr(10);
$value = VariableExtractor::extract($this->variables, $path, $accessors);
if (stristr($path, 'item')) {
var_dump(
'getByPath' . spl_object_hash($this),
$path,
$value,
$this->variables
);
echo chr(10) . chr(10);
}
return $value;
}
/**
* Remove a variable from context. Throws exception if variable is not found in context.
*
* @param string $identifier The identifier to remove
* @return void
* @api
*/
public function remove($identifier) {
echo __METHOD__ . chr(10);
if (stristr($identifier, 'item')) {
var_dump(
'remove' . spl_object_hash($this),
$identifier
);
echo chr(10) . chr(10);
}
if (array_key_exists($identifier, $this->variables)) {
unset($this->variables[$identifier]);
}
}
/**
* Returns an array of all identifiers available in the context.
*
* @return array Array of identifier strings
*/
public function getAllIdentifiers() {
echo __METHOD__ . chr(10);
return array_keys($this->variables);
}
/**
* Checks if this property exists in the VariableContainer.
*
* @param string $identifier
* @return boolean TRUE if $identifier exists, FALSE otherwise
* @api
*/
public function exists($identifier) {
echo __METHOD__ . chr(10);
return array_key_exists($identifier, $this->variables);
}
/**
* Clean up for serializing.
*
* @return string[]
*/
public function __sleep() {
echo __METHOD__ . chr(10);
return array('variables');
}
/**
* Adds a variable to the context.
*
* @param string $identifier Identifier of the variable to add
* @param mixed $value The variable's value
* @return void
*/
public function offsetSet($identifier, $value) {
echo __METHOD__ . chr(10);
$this->add($identifier, $value);
}
/**
* Remove a variable from context. Throws exception if variable is not found in context.
*
* @param string $identifier The identifier to remove
* @return void
*/
public function offsetUnset($identifier) {
echo __METHOD__ . chr(10);
if (stristr($identifier, 'item')) {
var_dump(
'remove' . spl_object_hash($this),
$identifier
);
echo chr(10) . chr(10);
}
$this->remove($identifier);
}
/**
* Checks if this property exists in the VariableContainer.
*
* @param string $identifier
* @return boolean TRUE if $identifier exists, FALSE otherwise
*/
public function offsetExists($identifier) {
echo __METHOD__ . chr(10);
return $this->exists($identifier);
}
/**
* Get a variable from the context. Throws exception if variable is not found in context.
*
* @param string $identifier
* @return mixed The variable identified by $identifier
*/
public function offsetGet($identifier) {
echo __METHOD__ . chr(10);
return $this->get($identifier);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment