This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Templating/Loader/FilesystemLoader.php
Tobias Schultze 8573385a83 Merge branch '2.3' into 2.7
Conflicts:
	src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php
	src/Symfony/Component/ClassLoader/DebugClassLoader.php
	src/Symfony/Component/ClassLoader/UniversalClassLoader.php
	src/Symfony/Component/Console/Command/Command.php
	src/Symfony/Component/DependencyInjection/Definition.php
	src/Symfony/Component/DependencyInjection/DefinitionDecorator.php
	src/Symfony/Component/EventDispatcher/Event.php
	src/Symfony/Component/Filesystem/Exception/IOException.php
	src/Symfony/Component/HttpFoundation/File/File.php
	src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php
	src/Symfony/Component/HttpFoundation/Session/SessionInterface.php
	src/Symfony/Component/HttpFoundation/StreamedResponse.php
	src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php
	src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php
	src/Symfony/Component/HttpKernel/HttpKernel.php
	src/Symfony/Component/HttpKernel/Kernel.php
	src/Symfony/Component/HttpKernel/KernelInterface.php
	src/Symfony/Component/HttpKernel/Log/LoggerInterface.php
	src/Symfony/Component/HttpKernel/Log/NullLogger.php
	src/Symfony/Component/Process/Process.php
	src/Symfony/Component/Routing/RequestContext.php
	src/Symfony/Component/Routing/Route.php
	src/Symfony/Component/Templating/EngineInterface.php
	src/Symfony/Component/Templating/PhpEngine.php
	src/Symfony/Component/Templating/TemplateNameParser.php
	src/Symfony/Component/Templating/TemplateReference.php
	src/Symfony/Component/Templating/TemplateReferenceInterface.php
	src/Symfony/Component/Translation/IdentityTranslator.php
	src/Symfony/Component/Translation/Translator.php
	src/Symfony/Component/Validator/ConstraintViolationInterface.php
	src/Symfony/Component/Validator/Constraints/False.php
	src/Symfony/Component/Validator/Constraints/FalseValidator.php
	src/Symfony/Component/Validator/Constraints/GroupSequence.php
	src/Symfony/Component/Validator/Constraints/Image.php
	src/Symfony/Component/Validator/Constraints/Null.php
	src/Symfony/Component/Validator/Constraints/NullValidator.php
	src/Symfony/Component/Validator/Constraints/True.php
	src/Symfony/Component/Validator/Constraints/TrueValidator.php
	src/Symfony/Component/Validator/ExecutionContextInterface.php
	src/Symfony/Component/Validator/ValidatorInterface.php
2015-09-29 14:06:14 +02:00

127 lines
3.9 KiB
PHP

<?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\Templating\Loader;
use Symfony\Component\Templating\Storage\Storage;
use Symfony\Component\Templating\Storage\FileStorage;
use Symfony\Component\Templating\TemplateReferenceInterface;
/**
* FilesystemLoader is a loader that read templates from the filesystem.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class FilesystemLoader extends Loader
{
protected $templatePathPatterns;
/**
* Constructor.
*
* @param array $templatePathPatterns An array of path patterns to look for templates
*/
public function __construct($templatePathPatterns)
{
$this->templatePathPatterns = (array) $templatePathPatterns;
}
/**
* Loads a template.
*
* @param TemplateReferenceInterface $template A template
*
* @return Storage|bool false if the template cannot be loaded, a Storage instance otherwise
*/
public function load(TemplateReferenceInterface $template)
{
$file = $template->get('name');
if (self::isAbsolutePath($file) && is_file($file)) {
return new FileStorage($file);
}
$replacements = array();
foreach ($template->all() as $key => $value) {
$replacements['%'.$key.'%'] = $value;
}
$fileFailures = array();
foreach ($this->templatePathPatterns as $templatePathPattern) {
if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) {
if (null !== $this->logger) {
$this->logger->debug('Loaded template file.', array('file' => $file));
} elseif (null !== $this->debugger) {
// just for BC, to be removed in 3.0
$this->debugger->log(sprintf('Loaded template file "%s".', $file));
}
return new FileStorage($file);
}
if (null !== $this->logger || null !== $this->debugger) {
$fileFailures[] = $file;
}
}
// only log failures if no template could be loaded at all
foreach ($fileFailures as $file) {
if (null !== $this->logger) {
$this->logger->debug('Failed loading template file.', array('file' => $file));
} elseif (null !== $this->debugger) {
// just for BC, to be removed in 3.0
$this->debugger->log(sprintf('Failed loading template file "%s".', $file));
}
}
return false;
}
/**
* Returns true if the template is still fresh.
*
* @param TemplateReferenceInterface $template A template
* @param int $time The last modification time of the cached template (timestamp)
*
* @return bool true if the template is still fresh, false otherwise
*/
public function isFresh(TemplateReferenceInterface $template, $time)
{
if (false === $storage = $this->load($template)) {
return false;
}
return filemtime((string) $storage) < $time;
}
/**
* Returns true if the file is an existing absolute path.
*
* @param string $file A path
*
* @return bool true if the path exists and is absolute, false otherwise
*/
protected static function isAbsolutePath($file)
{
if ($file[0] == '/' || $file[0] == '\\'
|| (strlen($file) > 3 && ctype_alpha($file[0])
&& $file[1] == ':'
&& ($file[2] == '\\' || $file[2] == '/')
)
|| null !== parse_url($file, PHP_URL_SCHEME)
) {
return true;
}
return false;
}
}