Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged branch dantleech/twig_loader_pass (PR #6171)
This PR was merged into the master branch. Commits ------- f48b22a Added configuration pass that adds Twig Loaders Discussion ---------- [Twig] [DI Pass] Added configuration pass that adds Twig Loaders Bug fix: [no] Feature addition: [yes] Backwards compatibility break: [no] Symfony2 tests pass: [yes] Todo: Documentation? License of the code: MIT - Defined new Chain loader service with symfony Filesystem loader added by default. - Added compiler class which picks up any services tagged "twig.loader" - If there are any instances of "twig.loader" the "twig.loader" alias is set to the Twig_Loader_Chain service ID instead of the filesystem loader. I think I still like the explicitness of the other pull request, but I defer to your judgment :) This is certainly much easier for the developer. --------------------------------------------------------------------------- by dantleech at 2012-12-03T08:31:08Z Will update the PR later today / tomorrow --------------------------------------------------------------------------- by dantleech at 2012-12-03T18:19:09Z ok, updated. I throw a `Symfony\Component\DependencyInjection\Exception\LogicException` if there are no loaders -- not sure if that is the best thing to do, or if that is the best exception. --------------------------------------------------------------------------- by fabpot at 2012-12-05T15:28:24Z Looks good to me. Can you add a note in the CHANGELOG about this new possibility and update the documentation accordingly? Thanks. --------------------------------------------------------------------------- by dantleech at 2012-12-05T17:50:37Z ok. updated change log and changed both count() comparisons to be strict. @fabpot which documentation should I update? or should I add `cookbook/templating/registering_multiple_loaders.rst`? --------------------------------------------------------------------------- by stof at 2012-12-05T20:07:37Z @dantleech at least the DIC tags reference need to be updated to mention the new tag. I'm not sure a dedicated article is needed for it (but @weaverryan will decide if it is worth it) --------------------------------------------------------------------------- by dantleech at 2012-12-06T17:57:20Z Made a PR for documentation: symfony/symfony-docs#2005 --------------------------------------------------------------------------- by dantleech at 2012-12-07T16:44:00Z ok. have updated the correct CHANGELOG and squashed to one commit
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
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
53 changes: 53 additions & 0 deletions
53
src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/TwigLoaderPass.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,53 @@ | ||
<?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\Bundle\TwigBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Exception\LogicException; | ||
|
||
/** | ||
* If there are any tagged loaders replace | ||
* default filesystem loader with chain loader | ||
* | ||
* Add tagged loaders to chain loader | ||
* | ||
* @author Daniel Leech <daniel@dantleech.com> | ||
*/ | ||
class TwigLoaderPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (false === $container->hasDefinition('twig')) { | ||
return; | ||
} | ||
|
||
// register additional template loaders | ||
$loaderIds = $container->findTaggedServiceIds('twig.loader'); | ||
|
||
if (count($loaderIds) === 0) { | ||
throw new LogicException('No twig loaders found. You need to tag at least one loader with "twig.loader"'); | ||
} | ||
|
||
if (count($loaderIds) === 1) { | ||
$container->setAlias('twig.loader', key($loaderIds)); | ||
} else { | ||
$chainLoader = $container->getDefinition('twig.loader.chain'); | ||
foreach (array_keys($loaderIds) as $id) { | ||
$chainLoader->addMethodCall('addLoader', array(new Reference($id))); | ||
}; | ||
$container->setAlias('twig.loader', 'twig.loader.chain'); | ||
} | ||
} | ||
} | ||
|
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
87 changes: 87 additions & 0 deletions
87
src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Compiler/TwigLoaderPassTest.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,87 @@ | ||
<?php | ||
|
||
namespace symfony\src\Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigLoaderPass; | ||
|
||
class TwigLoaderPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->builder = $this->getMock('Symfony\Component\DependencyInjection\ContainerBuilder'); | ||
$this->chainLoader = new Definition('loader'); | ||
$this->pass = new TwigLoaderPass(); | ||
} | ||
|
||
public function testMapperPassWithOneTaggedLoaders() | ||
{ | ||
$serviceIds = array( | ||
'test_loader_1' => array( | ||
), | ||
); | ||
|
||
$this->builder->expects($this->once()) | ||
->method('hasDefinition') | ||
->with('twig') | ||
->will($this->returnValue(true)); | ||
$this->builder->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('twig.loader') | ||
->will($this->returnValue($serviceIds)); | ||
$this->builder->expects($this->once()) | ||
->method('setAlias') | ||
->with('twig.loader', 'test_loader_1'); | ||
|
||
$this->pass->process($this->builder); | ||
} | ||
|
||
public function testMapperPassWithTwoTaggedLoaders() | ||
{ | ||
$serviceIds = array( | ||
'test_loader_1' => array( | ||
), | ||
'test_loader_2' => array( | ||
), | ||
); | ||
|
||
$this->builder->expects($this->once()) | ||
->method('hasDefinition') | ||
->with('twig') | ||
->will($this->returnValue(true)); | ||
$this->builder->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('twig.loader') | ||
->will($this->returnValue($serviceIds)); | ||
$this->builder->expects($this->once()) | ||
->method('getDefinition') | ||
->with('twig.loader.chain') | ||
->will($this->returnValue($this->chainLoader)); | ||
$this->builder->expects($this->once()) | ||
->method('setAlias') | ||
->with('twig.loader', 'twig.loader.chain'); | ||
|
||
$this->pass->process($this->builder); | ||
$calls = $this->chainLoader->getMethodCalls(); | ||
$this->assertEquals(2, count($calls)); | ||
$this->assertEquals('addLoader', $calls[0][0]); | ||
} | ||
|
||
/** | ||
* @expectedException Symfony\Component\DependencyInjection\Exception\LogicException | ||
*/ | ||
public function testMapperPassWithZeroTaggedLoaders() | ||
{ | ||
$this->builder->expects($this->once()) | ||
->method('hasDefinition') | ||
->with('twig') | ||
->will($this->returnValue(true)); | ||
$this->builder->expects($this->once()) | ||
->method('findTaggedServiceIds') | ||
->with('twig.loader') | ||
->will($this->returnValue(array())); | ||
|
||
$this->pass->process($this->builder); | ||
} | ||
} |
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