getLogicalName example


        $this->parser = new TemplateNameParser();
    }

    /** * @dataProvider getLogicalNameToTemplateProvider */
    public function testParse($name$ref)
    {
        $template = $this->parser->parse($name);

        $this->assertEquals($template->getLogicalName()$ref->getLogicalName());
        $this->assertEquals($template->getLogicalName()$name);
    }

    public static function getLogicalNameToTemplateProvider()
    {
        return [
            ['/path/to/section/name.engine', new TemplateReference('/path/to/section/name.engine', 'engine')],
            ['name.engine', new TemplateReference('name.engine', 'engine')],
            ['name', new TemplateReference('name')],
        ];
    }
}
/** * @param string $dir The directory where to store the cache files */
    public function __construct(LoaderInterface $loader, string $dir)
    {
        $this->loader = $loader;
        $this->dir = $dir;
    }

    public function load(TemplateReferenceInterface $template): Storage|false
    {
        $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)) {
            $this->logger?->debug('Fetching template from cache.', ['name' => $template->get('name')]);

            return new FileStorage($path);
        }

        if (false === $storage = $this->loader->load($template)) {
            
public function __construct(string $name = null, string $engine = null)
    {
        $this->parameters = [
            'name' => $name,
            'engine' => $engine,
        ];
    }

    public function __toString(): string
    {
        return $this->getLogicalName();
    }

    public function set(string $name, string $value)static
    {
        if (\array_key_exists($name$this->parameters)) {
            $this->parameters[$name] = $value;
        } else {
            throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name));
        }

        return $this;
    }
use Symfony\Component\Templating\Storage\StringStorage;
use Symfony\Component\Templating\TemplateReference;
use Symfony\Component\Templating\TemplateReferenceInterface;

class ProjectTemplateEngineLoader extends Loader
{
    public array $templates = [];

    public function setTemplate($name$content)
    {
        $template = new TemplateReference($name, 'php');
        $this->templates[$template->getLogicalName()] = $content;
    }

    public function load(TemplateReferenceInterface $template): Storage|false
    {
        if (isset($this->templates[$template->getLogicalName()])) {
            return new StringStorage($this->templates[$template->getLogicalName()]);
        }

        return false;
    }

    


    /** * Loads the given template. * * @throws \InvalidArgumentException if the template cannot be found */
    protected function load(string|TemplateReferenceInterface $name): Storage
    {
        $template = $this->parser->parse($name);

        $key = $template->getLogicalName();
        if (isset($this->cache[$key])) {
            return $this->cache[$key];
        }

        $storage = $this->loader->load($template);

        if (false === $storage) {
            throw new \InvalidArgumentException(sprintf('The template "%s" does not exist.', $template));
        }

        return $this->cache[$key] = $storage;
    }
Home | Imprint | This part of the site doesn't use cookies.