Skip to content

Commit 680a292

Browse files
committed
[FrameworkBundle] Use relative paths in templates paths cache
1 parent 9dca27d commit 680a292

File tree

6 files changed

+117
-3
lines changed

6 files changed

+117
-3
lines changed

CacheWarmer/TemplatePathsCacheWarmer.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
1313

14+
use Symfony\Component\Filesystem\Filesystem;
1415
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
1516
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
1617

@@ -34,6 +35,7 @@ public function __construct(TemplateFinderInterface $finder, TemplateLocator $lo
3435
{
3536
$this->finder = $finder;
3637
$this->locator = $locator;
38+
$this->filesystem = new Filesystem();
3739
}
3840

3941
/**
@@ -46,10 +48,12 @@ public function warmUp($cacheDir)
4648
$templates = array();
4749

4850
foreach ($this->finder->findAllTemplates() as $template) {
49-
$templates[$template->getLogicalName()] = $this->locator->locate($template);
51+
$templates[$template->getLogicalName()] = rtrim($this->filesystem->makePathRelative($this->locator->locate($template), $cacheDir), '/');
5052
}
5153

52-
$this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
54+
$templates = str_replace("' => '", "' => __DIR__.'/", var_export($templates, true));
55+
56+
$this->writeCacheFile($cacheDir.'/templates.php', sprintf("<?php return %s;\n", $templates));
5357
}
5458

5559
/**

Templating/Loader/TemplateLocator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TemplateLocator implements FileLocatorInterface
3232
*/
3333
public function __construct(FileLocatorInterface $locator, $cacheDir = null)
3434
{
35-
if (null !== $cacheDir && is_file($cache = $cacheDir.'/templates.php')) {
35+
if (null !== $cacheDir && file_exists($cache = $cacheDir.'/templates.php')) {
3636
$this->cache = require $cache;
3737
}
3838

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
13+
14+
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
15+
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplatePathsCacheWarmer;
16+
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
17+
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
18+
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
19+
use Symfony\Component\Config\FileLocator;
20+
use Symfony\Component\Filesystem\Filesystem;
21+
22+
class TemplatePathsCacheWarmerTest extends TestCase
23+
{
24+
/** @var Filesystem */
25+
private $filesystem;
26+
27+
/** @var TemplateFinderInterface */
28+
private $templateFinder;
29+
30+
/** @var FileLocator */
31+
private $fileLocator;
32+
33+
/** @var TemplateLocator */
34+
private $templateLocator;
35+
36+
private $tmpDir;
37+
38+
public function setUp()
39+
{
40+
$this->templateFinder = $this
41+
->getMockBuilder(TemplateFinderInterface::class)
42+
->setMethods(array('findAllTemplates'))
43+
->getMock();
44+
45+
$this->fileLocator = $this
46+
->getMockBuilder(FileLocator::class)
47+
->setMethods(array('locate'))
48+
->setConstructorArgs(array('/path/to/fallback'))
49+
->getMock();
50+
51+
$this->templateLocator = new TemplateLocator($this->fileLocator);
52+
53+
$this->tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.uniqid('cache_template_paths_', true);
54+
55+
$this->filesystem = new Filesystem();
56+
$this->filesystem->mkdir($this->tmpDir);
57+
}
58+
59+
public function tearDown()
60+
{
61+
$this->filesystem->remove($this->tmpDir);
62+
}
63+
64+
public function testWarmUp()
65+
{
66+
$template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');
67+
68+
$this->templateFinder
69+
->expects($this->once())
70+
->method('findAllTemplates')
71+
->will($this->returnValue(array($template)));
72+
73+
$this->fileLocator
74+
->expects($this->once())
75+
->method('locate')
76+
->with($template->getPath())
77+
->will($this->returnValue(dirname($this->tmpDir).'/path/to/template.html.twig'));
78+
79+
$warmer = new TemplatePathsCacheWarmer($this->templateFinder, $this->templateLocator);
80+
$warmer->warmUp($this->tmpDir);
81+
82+
$this->assertFileEquals(__DIR__.'/../Fixtures/TemplatePathsCache/templates.php', $this->tmpDir.'/templates.php');
83+
}
84+
85+
public function testWarmUpEmpty()
86+
{
87+
$this->templateFinder
88+
->expects($this->once())
89+
->method('findAllTemplates')
90+
->will($this->returnValue(array()));
91+
92+
$this->fileLocator
93+
->expects($this->never())
94+
->method('locate');
95+
96+
$warmer = new TemplatePathsCacheWarmer($this->templateFinder, $this->templateLocator);
97+
$warmer->warmUp($this->tmpDir);
98+
99+
$this->assertFileExists($this->tmpDir.'/templates.php');
100+
$this->assertSame(file_get_contents(__DIR__.'/../Fixtures/TemplatePathsCache/templates-empty.php'), file_get_contents($this->tmpDir.'/templates.php'));
101+
}
102+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php return array (
2+
);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php return array (
2+
'bundle:controller:name.format.engine' => __DIR__.'/../path/to/template.html.twig',
3+
);

Tests/Templating/Loader/TemplateLocatorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public function testLocateATemplate()
3333
$locator = new TemplateLocator($fileLocator);
3434

3535
$this->assertEquals('/path/to/template', $locator->locate($template));
36+
37+
// Assert cache is used as $fileLocator->locate should be called only once
38+
$this->assertEquals('/path/to/template', $locator->locate($template));
3639
}
3740

3841
public function testThrowsExceptionWhenTemplateNotFound()

0 commit comments

Comments
 (0)