Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made a big refactoring of the templating sub-framework
* better separation of concerns * made TwigBundle independant of the PHP Engine from FrameworkBundle (WIP) * removed one layer of abstraction in the Templating component (renderers) * made it easier to create a new Engine for any templating library * made engines lazy-loaded (PHP engine for instance is not started if you only use Twig) * reduces memory footprint (if you only use one engine) * reduces size of compiled classes.php cache file
- Loading branch information
Showing
46 changed files
with
1,002 additions
and
864 deletions.
There are no files selected for viewing
41 changes: 0 additions & 41 deletions
41
...ymfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddTemplatingRenderersPass.php
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TemplatingPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
|
||
class TemplatingPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if ($container->hasDefinition('templating')) { | ||
return; | ||
} | ||
|
||
if ($container->hasDefinition('templating.engine.php')) { | ||
$helpers = array(); | ||
foreach ($container->findTaggedServiceIds('templating.helper') as $id => $attributes) { | ||
if (isset($attributes[0]['alias'])) { | ||
$helpers[$attributes[0]['alias']] = $id; | ||
} | ||
} | ||
|
||
$definition = $container->getDefinition('templating.engine.php'); | ||
$arguments = $definition->getArguments(); | ||
$definition->setArguments($arguments); | ||
|
||
if (count($helpers) > 0) { | ||
$definition->addMethodCall('setHelpers', array($helpers)); | ||
} | ||
} | ||
|
||
if ($container->hasDefinition('templating.engine.delegating')) { | ||
$queue = new \SplPriorityQueue(); | ||
foreach ($container->findTaggedServiceIds('templating.engine') as $id => $attributes) { | ||
$queue->insert($id, isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0); | ||
} | ||
|
||
$engines = array(); | ||
foreach ($queue as $engine) { | ||
$engines[] = $engine; | ||
} | ||
|
||
$container->getDefinition('templating.engine.delegating')->addMethodCall('setEngineIds', array($engines)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/Symfony/Bundle/FrameworkBundle/Templating/DelegatingEngine.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Templating; | ||
|
||
use Symfony\Component\Templating\DelegatingEngine as BaseDelegatingEngine; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* DelegatingEngine selects an engine for a given template. | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class DelegatingEngine extends BaseDelegatingEngine implements EngineInterface | ||
{ | ||
protected $container; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param ContainerInterface $container The DI container | ||
*/ | ||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->container = $container; | ||
$this->engines = array(); | ||
} | ||
|
||
public function setEngineIds($ids) | ||
{ | ||
$this->engines = $ids; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($name) | ||
{ | ||
foreach ($this->engines as $i => $engine) { | ||
if (is_string($engine)) { | ||
$engine = $this->engines[$i] = $this->container->get($engine); | ||
} | ||
|
||
if ($engine->supports($name)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getEngine($name) | ||
{ | ||
foreach ($this->engines as $i => $engine) { | ||
if (is_string($engine)) { | ||
$engine = $this->engines[$i] = $this->container->get($engine); | ||
} | ||
|
||
if ($engine->supports($name)) { | ||
return $engine; | ||
} | ||
} | ||
|
||
throw new \RuntimeException(sprintf('No engine is able to work with the "%s" template.', $name)); | ||
} | ||
|
||
/** | ||
* Renders a view and returns a Response. | ||
* | ||
* @param string $view The view name | ||
* @param array $parameters An array of parameters to pass to the view | ||
* @param Response $response A Response instance | ||
* | ||
* @return Response A Response instance | ||
*/ | ||
public function renderResponse($view, array $parameters = array(), Response $response = null) | ||
{ | ||
if (null === $response) { | ||
$response = $this->container->get('response'); | ||
} | ||
|
||
$response->setContent($this->render($view, $parameters)); | ||
|
||
return $response; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Symfony/Bundle/FrameworkBundle/Templating/EngineInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Templating; | ||
|
||
use Symfony\Component\Templating\EngineInterface as BaseEngineInterface; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/* | ||
* This file is part of the Symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* EngineInterface is the interface each engine must implement. | ||
* | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
interface EngineInterface extends BaseEngineInterface | ||
{ | ||
/** | ||
* Renders a view and returns a Response. | ||
* | ||
* @param string $view The view name | ||
* @param array $parameters An array of parameters to pass to the view | ||
* @param Response $response A Response instance | ||
* | ||
* @return Response A Response instance | ||
*/ | ||
function renderResponse($view, array $parameters = array(), Response $response = null); | ||
} |
Oops, something went wrong.