Skip to content

[FrameworkBundle] Use relative paths in templates paths cache #19687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;

Expand All @@ -34,6 +35,7 @@ public function __construct(TemplateFinderInterface $finder, TemplateLocator $lo
{
$this->finder = $finder;
$this->locator = $locator;
$this->filesystem = new Filesystem();
}

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

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

$this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
$templates = str_replace("' => '", "' => __DIR__.'/", var_export($templates, true));

$this->writeCacheFile($cacheDir.'/templates.php', sprintf("<?php return %s;\n", $templates));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class TemplateLocator implements FileLocatorInterface
*/
public function __construct(FileLocatorInterface $locator, $cacheDir = null)
{
if (null !== $cacheDir && is_file($cache = $cacheDir.'/templates.php')) {
if (null !== $cacheDir && file_exists($cache = $cacheDir.'/templates.php')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file_exists is faster and is_file have no advantage over it here.

$this->cache = require $cache;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\FrameworkBundle\Tests\CacheWarmer;

use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinderInterface;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplatePathsCacheWarmer;
use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Filesystem\Filesystem;

class TemplatePathsCacheWarmerTest extends TestCase
{
/** @var Filesystem */
private $filesystem;

/** @var TemplateFinderInterface */
private $templateFinder;

/** @var FileLocator */
private $fileLocator;

/** @var TemplateLocator */
private $templateLocator;

private $tmpDir;

public function setUp()
{
$this->templateFinder = $this
->getMockBuilder(TemplateFinderInterface::class)
->setMethods(array('findAllTemplates'))
->getMock();

$this->fileLocator = $this
->getMockBuilder(FileLocator::class)
->setMethods(array('locate'))
->setConstructorArgs(array('/path/to/fallback'))
->getMock();

$this->templateLocator = new TemplateLocator($this->fileLocator);

$this->tmpDir = sys_get_temp_dir().DIRECTORY_SEPARATOR.uniqid('cache_template_paths_', true);

$this->filesystem = new Filesystem();
$this->filesystem->mkdir($this->tmpDir);
}

public function tearDown()
{
$this->filesystem->remove($this->tmpDir);
}

public function testWarmUp()
{
$template = new TemplateReference('bundle', 'controller', 'name', 'format', 'engine');

$this->templateFinder
->expects($this->once())
->method('findAllTemplates')
->will($this->returnValue(array($template)));

$this->fileLocator
->expects($this->once())
->method('locate')
->with($template->getPath())
->will($this->returnValue(dirname($this->tmpDir).'/path/to/template.html.twig'));

$warmer = new TemplatePathsCacheWarmer($this->templateFinder, $this->templateLocator);
$warmer->warmUp($this->tmpDir);

$this->assertFileEquals(__DIR__.'/../Fixtures/TemplatePathsCache/templates.php', $this->tmpDir.'/templates.php');
}

public function testWarmUpEmpty()
{
$this->templateFinder
->expects($this->once())
->method('findAllTemplates')
->will($this->returnValue(array()));

$this->fileLocator
->expects($this->never())
->method('locate');

$warmer = new TemplatePathsCacheWarmer($this->templateFinder, $this->templateLocator);
$warmer->warmUp($this->tmpDir);

$this->assertFileExists($this->tmpDir.'/templates.php');
$this->assertSame(file_get_contents(__DIR__.'/../Fixtures/TemplatePathsCache/templates-empty.php'), file_get_contents($this->tmpDir.'/templates.php'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php return array (
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php return array (
'bundle:controller:name.format.engine' => __DIR__.'/../path/to/template.html.twig',
);
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public function testLocateATemplate()
$locator = new TemplateLocator($fileLocator);

$this->assertEquals('/path/to/template', $locator->locate($template));

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

public function testThrowsExceptionWhenTemplateNotFound()
Expand Down