2012-07-15 17:58:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Form;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default implementation of {@link FormRendererEngineInterface}.
|
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
|
|
|
abstract class AbstractRendererEngine implements FormRendererEngineInterface
|
|
|
|
{
|
|
|
|
/**
|
2012-07-16 13:57:09 +01:00
|
|
|
* The variable in {@link FormView} used as cache key.
|
2012-07-15 17:58:13 +01:00
|
|
|
*/
|
2012-07-25 12:42:52 +01:00
|
|
|
const CACHE_KEY_VAR = 'cache_key';
|
2012-08-27 01:03:52 +01:00
|
|
|
|
2012-07-15 17:58:13 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultThemes;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $themes = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $resources = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $resourceHierarchyLevels = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new renderer engine.
|
|
|
|
*
|
|
|
|
* @param array $defaultThemes The default themes. The type of these
|
|
|
|
* themes is open to the implementation.
|
|
|
|
*/
|
|
|
|
public function __construct(array $defaultThemes = array())
|
|
|
|
{
|
|
|
|
$this->defaultThemes = $defaultThemes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-16 13:57:09 +01:00
|
|
|
public function setTheme(FormView $view, $themes)
|
2012-07-15 17:58:13 +01:00
|
|
|
{
|
2012-07-16 13:57:09 +01:00
|
|
|
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
// Do not cast, as casting turns objects into arrays of properties
|
|
|
|
$this->themes[$cacheKey] = is_array($themes) ? $themes : array($themes);
|
2012-07-17 08:00:22 +01:00
|
|
|
|
|
|
|
// Unset instead of resetting to an empty array, in order to allow
|
|
|
|
// implementations (like TwigRendererEngine) to check whether $cacheKey
|
|
|
|
// is set at all.
|
|
|
|
unset($this->resources[$cacheKey]);
|
|
|
|
unset($this->resourceHierarchyLevels[$cacheKey]);
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-21 18:44:09 +01:00
|
|
|
public function getResourceForBlockName(FormView $view, $blockName)
|
2012-07-15 17:58:13 +01:00
|
|
|
{
|
2012-07-16 13:57:09 +01:00
|
|
|
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
if (!isset($this->resources[$cacheKey][$blockName])) {
|
|
|
|
$this->loadResourceForBlockName($cacheKey, $view, $blockName);
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
return $this->resources[$cacheKey][$blockName];
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-21 18:44:09 +01:00
|
|
|
public function getResourceForBlockNameHierarchy(FormView $view, array $blockNameHierarchy, $hierarchyLevel)
|
2012-07-15 17:58:13 +01:00
|
|
|
{
|
2012-07-16 13:57:09 +01:00
|
|
|
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
|
2012-07-21 18:44:09 +01:00
|
|
|
$blockName = $blockNameHierarchy[$hierarchyLevel];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
if (!isset($this->resources[$cacheKey][$blockName])) {
|
|
|
|
$this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel);
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
return $this->resources[$cacheKey][$blockName];
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2012-07-21 18:44:09 +01:00
|
|
|
public function getResourceHierarchyLevel(FormView $view, array $blockNameHierarchy, $hierarchyLevel)
|
2012-07-15 17:58:13 +01:00
|
|
|
{
|
2012-07-16 13:57:09 +01:00
|
|
|
$cacheKey = $view->vars[self::CACHE_KEY_VAR];
|
2012-07-21 18:44:09 +01:00
|
|
|
$blockName = $blockNameHierarchy[$hierarchyLevel];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
if (!isset($this->resources[$cacheKey][$blockName])) {
|
|
|
|
$this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $hierarchyLevel);
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If $block was previously rendered loaded with loadTemplateForBlock(), the template
|
|
|
|
// is cached but the hierarchy level is not. In this case, we know that the block
|
|
|
|
// exists at this very hierarchy level, so we can just set it.
|
2012-07-21 18:44:09 +01:00
|
|
|
if (!isset($this->resourceHierarchyLevels[$cacheKey][$blockName])) {
|
|
|
|
$this->resourceHierarchyLevels[$cacheKey][$blockName] = $hierarchyLevel;
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
return $this->resourceHierarchyLevels[$cacheKey][$blockName];
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the cache with the resource for a given block name.
|
|
|
|
*
|
|
|
|
* @see getResourceForBlock()
|
|
|
|
*
|
2012-07-21 18:44:09 +01:00
|
|
|
* @param string $cacheKey The cache key of the form view.
|
|
|
|
* @param FormView $view The form view for finding the applying themes.
|
|
|
|
* @param string $blockName The name of the block to load.
|
2012-07-15 17:58:13 +01:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool True if the resource could be loaded, false otherwise.
|
2012-07-15 17:58:13 +01:00
|
|
|
*/
|
2012-07-21 18:44:09 +01:00
|
|
|
abstract protected function loadResourceForBlockName($cacheKey, FormView $view, $blockName);
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the cache with the resource for a specific level of a block hierarchy.
|
|
|
|
*
|
|
|
|
* @see getResourceForBlockHierarchy()
|
|
|
|
*
|
2012-07-21 18:44:09 +01:00
|
|
|
* @param string $cacheKey The cache key used for storing the
|
|
|
|
* resource.
|
|
|
|
* @param FormView $view The form view for finding the applying
|
|
|
|
* themes.
|
|
|
|
* @param array $blockNameHierarchy The block hierarchy, with the most
|
|
|
|
* specific block name at the end.
|
2014-04-12 18:54:57 +01:00
|
|
|
* @param int $hierarchyLevel The level in the block hierarchy that
|
2012-07-21 18:44:09 +01:00
|
|
|
* should be loaded.
|
2012-07-15 17:58:13 +01:00
|
|
|
*
|
2014-04-16 11:30:19 +01:00
|
|
|
* @return bool True if the resource could be loaded, false otherwise.
|
2012-07-15 17:58:13 +01:00
|
|
|
*/
|
2012-07-21 18:44:09 +01:00
|
|
|
private function loadResourceForBlockNameHierarchy($cacheKey, FormView $view, array $blockNameHierarchy, $hierarchyLevel)
|
2012-07-15 17:58:13 +01:00
|
|
|
{
|
2012-07-21 18:44:09 +01:00
|
|
|
$blockName = $blockNameHierarchy[$hierarchyLevel];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
// Try to find a template for that block
|
2012-07-21 18:44:09 +01:00
|
|
|
if ($this->loadResourceForBlockName($cacheKey, $view, $blockName)) {
|
2012-07-15 17:58:13 +01:00
|
|
|
// If loadTemplateForBlock() returns true, it was able to populate the
|
|
|
|
// cache. The only missing thing is to set the hierarchy level at which
|
|
|
|
// the template was found.
|
2012-07-21 18:44:09 +01:00
|
|
|
$this->resourceHierarchyLevels[$cacheKey][$blockName] = $hierarchyLevel;
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($hierarchyLevel > 0) {
|
|
|
|
$parentLevel = $hierarchyLevel - 1;
|
2012-07-21 18:44:09 +01:00
|
|
|
$parentBlockName = $blockNameHierarchy[$parentLevel];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
// The next two if statements contain slightly duplicated code. This is by intention
|
|
|
|
// and tries to avoid execution of unnecessary checks in order to increase performance.
|
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
if (isset($this->resources[$cacheKey][$parentBlockName])) {
|
2012-07-15 17:58:13 +01:00
|
|
|
// It may happen that the parent block is already loaded, but its level is not.
|
|
|
|
// In this case, the parent block must have been loaded by loadResourceForBlock(),
|
|
|
|
// which does not check the hierarchy of the block. Subsequently the block must have
|
|
|
|
// been found directly on the parent level.
|
2012-07-21 18:44:09 +01:00
|
|
|
if (!isset($this->resourceHierarchyLevels[$cacheKey][$parentBlockName])) {
|
|
|
|
$this->resourceHierarchyLevels[$cacheKey][$parentBlockName] = $parentLevel;
|
2012-07-15 17:58:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the shortcuts for further accesses
|
2012-07-21 18:44:09 +01:00
|
|
|
$this->resources[$cacheKey][$blockName] = $this->resources[$cacheKey][$parentBlockName];
|
|
|
|
$this->resourceHierarchyLevels[$cacheKey][$blockName] = $this->resourceHierarchyLevels[$cacheKey][$parentBlockName];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-21 18:44:09 +01:00
|
|
|
if ($this->loadResourceForBlockNameHierarchy($cacheKey, $view, $blockNameHierarchy, $parentLevel)) {
|
2012-07-15 17:58:13 +01:00
|
|
|
// Cache the shortcuts for further accesses
|
2012-07-21 18:44:09 +01:00
|
|
|
$this->resources[$cacheKey][$blockName] = $this->resources[$cacheKey][$parentBlockName];
|
|
|
|
$this->resourceHierarchyLevels[$cacheKey][$blockName] = $this->resourceHierarchyLevels[$cacheKey][$parentBlockName];
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache the result for further accesses
|
2012-07-21 18:44:09 +01:00
|
|
|
$this->resources[$cacheKey][$blockName] = false;
|
|
|
|
$this->resourceHierarchyLevels[$cacheKey][$blockName] = false;
|
2012-07-15 17:58:13 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|