This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,93 @@
<?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\FileStorage;
use Symfony\Component\Templating\Storage\Storage;
use Symfony\Component\Templating\TemplateReferenceInterface;
/**
* CacheLoader is a loader that caches other loaders responses
* on the filesystem.
*
* This cache only caches on disk to allow PHP accelerators to cache the opcodes.
* All other mechanism would imply the use of `eval()`.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class CacheLoader extends Loader
{
protected $loader;
protected $dir;
/**
* @param LoaderInterface $loader A Loader instance
* @param string $dir The directory where to store the cache files
*/
public function __construct(LoaderInterface $loader, $dir)
{
$this->loader = $loader;
$this->dir = $dir;
}
/**
* Loads a template.
*
* @return Storage|bool false if the template cannot be loaded, a Storage instance otherwise
*/
public function load(TemplateReferenceInterface $template)
{
$key = hash('sha256', $template->getLogicalName());
$dir = $this->dir.\DIRECTORY_SEPARATOR.substr($key, 0, 2);
$file = substr($key, 2).'.tpl';
$path = $dir.\DIRECTORY_SEPARATOR.$file;
if (is_file($path)) {
if (null !== $this->logger) {
$this->logger->debug('Fetching template from cache.', ['name' => $template->get('name')]);
}
return new FileStorage($path);
}
if (false === $storage = $this->loader->load($template)) {
return false;
}
$content = $storage->getContent();
if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new \RuntimeException(sprintf('Cache Loader was not able to create directory "%s".', $dir));
}
file_put_contents($path, $content);
if (null !== $this->logger) {
$this->logger->debug('Storing template in cache.', ['name' => $template->get('name')]);
}
return new FileStorage($path);
}
/**
* 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
*/
public function isFresh(TemplateReferenceInterface $template, $time)
{
return $this->loader->isFresh($template, $time);
}
}

View File

@@ -0,0 +1,76 @@
<?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\TemplateReferenceInterface;
/**
* ChainLoader is a loader that calls other loaders to load templates.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ChainLoader extends Loader
{
protected $loaders = [];
/**
* @param LoaderInterface[] $loaders An array of loader instances
*/
public function __construct(array $loaders = [])
{
foreach ($loaders as $loader) {
$this->addLoader($loader);
}
}
/**
* Adds a loader instance.
*/
public function addLoader(LoaderInterface $loader)
{
$this->loaders[] = $loader;
}
/**
* Loads a template.
*
* @return Storage|bool false if the template cannot be loaded, a Storage instance otherwise
*/
public function load(TemplateReferenceInterface $template)
{
foreach ($this->loaders as $loader) {
if (false !== $storage = $loader->load($template)) {
return $storage;
}
}
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
*/
public function isFresh(TemplateReferenceInterface $template, $time)
{
foreach ($this->loaders as $loader) {
return $loader->isFresh($template, $time);
}
return false;
}
}

View File

@@ -0,0 +1,116 @@
<?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\FileStorage;
use Symfony\Component\Templating\Storage\Storage;
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;
/**
* @param string|string[] $templatePathPatterns An array of path patterns to look for templates
*/
public function __construct($templatePathPatterns)
{
$this->templatePathPatterns = (array) $templatePathPatterns;
}
/**
* Loads 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 = [];
foreach ($template->all() as $key => $value) {
$replacements['%'.$key.'%'] = $value;
}
$fileFailures = [];
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.', ['file' => $file]);
}
return new FileStorage($file);
}
if (null !== $this->logger) {
$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.', ['file' => $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;
}
}

View File

@@ -0,0 +1,35 @@
<?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 Psr\Log\LoggerInterface;
/**
* Loader is the base class for all template loader classes.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
abstract class Loader implements LoaderInterface
{
/**
* @var LoggerInterface|null
*/
protected $logger;
/**
* Sets the debug logger to use for this loader.
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
}

View File

@@ -0,0 +1,40 @@
<?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\TemplateReferenceInterface;
/**
* LoaderInterface is the interface all loaders must implement.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface LoaderInterface
{
/**
* Loads a template.
*
* @return Storage|bool false if the template cannot be loaded, a Storage instance otherwise
*/
public function load(TemplateReferenceInterface $template);
/**
* 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
*/
public function isFresh(TemplateReferenceInterface $template, $time);
}