Skip to content

Commit 8279501

Browse files
committed
Рефакторинг сервисов Твига. Убраны устаревшие классы.
1 parent a4a0c24 commit 8279501

File tree

3 files changed

+117
-20
lines changed

3 files changed

+117
-20
lines changed

Resources/config/twig.yaml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515

1616
# Twig loader
1717
twig.loader:
18-
class: Twig_Loader_Filesystem
18+
class: Twig\Loader\FilesystemLoader
1919
arguments:
2020
- '%twig_paths%'
2121

@@ -30,5 +30,13 @@ services:
3030

3131
# Экземпляр Twig.
3232
twig.instance:
33-
class: Twig_Environment
33+
class: Twig\Environment
3434
factory: ['@twig', 'instance']
35+
36+
# Template cache warmer
37+
Prokl\BitrixOrdinaryToolsBundle\Services\Twig\TemplateCacheWarmer:
38+
autoconfigure: false
39+
arguments:
40+
- '@twig.instance'
41+
class:Prokl\BitrixOrdinaryToolsBundle\Services\Twig\TemplateCacheWarmer
42+
tags: [kernel.cache_warmer]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Twig;
4+
5+
use Symfony\Component\Finder\Finder;
6+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
7+
use Twig\Error\Error;
8+
use Twig\Environment;
9+
10+
/**
11+
* Class TemplateCacheWarmer
12+
* @package Prokl\BitrixOrdinaryToolsBundle\Services\Twig
13+
*
14+
* @since 03.08.2021
15+
*/
16+
class TemplateCacheWarmer implements CacheWarmerInterface
17+
{
18+
/**
19+
* @var Environment $router Twig.
20+
*/
21+
private $twig;
22+
23+
/**
24+
* TemplateCacheWarmer constructor.
25+
*
26+
* @param Environment $twig Twig.
27+
*/
28+
public function __construct(Environment $twig)
29+
{
30+
$this->twig = $twig;
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function isOptional()
37+
{
38+
return true;
39+
}
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
public function warmUp($cacheDir)
45+
{
46+
$paths = $this->twig->getLoader()->getPaths();
47+
$phpFiles = [];
48+
49+
foreach ($paths as $path) {
50+
$files = $this->findTemplatesInDirectory($path);
51+
foreach ($files as $template) {
52+
try {
53+
$template = $this->twig->load($template);
54+
55+
if (\is_callable([$template, 'unwrap'])) {
56+
$phpFiles[] = (new \ReflectionClass($template->unwrap()))->getFileName();
57+
}
58+
} catch (Error $e) {
59+
// problem during compilation, give up
60+
// might be a syntax error or a non-Twig template
61+
}
62+
}
63+
}
64+
65+
return $phpFiles;
66+
}
67+
68+
/**
69+
* Find templates in the given directory.
70+
*
71+
* @param string $dir Директория.
72+
* @param string|null $namespace Пространство имен.
73+
* @param array $excludeDirs Исключенные директории.
74+
* @return string[]
75+
*/
76+
private function findTemplatesInDirectory(string $dir, string $namespace = null, array $excludeDirs = []): array
77+
{
78+
if (!is_dir($dir)) {
79+
return [];
80+
}
81+
82+
$templates = [];
83+
foreach (Finder::create()->files()->followLinks()->in($dir)->exclude($excludeDirs) as $file) {
84+
$templates[] = (null !== $namespace ? '@'.$namespace.'/' : '').str_replace('\\', '/', $file->getRelativePathname());
85+
}
86+
87+
return $templates;
88+
}
89+
}

Services/Twig/TwigService.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Twig;
44

5+
use Twig\Environment;
56
use Twig\Error\LoaderError;
6-
use Twig_Environment;
7-
use Twig_Loader_Filesystem;
7+
use Twig\Loader\FilesystemLoader;
88

99
/**
1010
* Class TwigService
@@ -16,12 +16,12 @@
1616
class TwigService
1717
{
1818
/**
19-
* @var Twig_Environment Twig.
19+
* @var Environment Twig.
2020
*/
2121
private $twigEnvironment;
2222

2323
/**
24-
* @var Twig_Loader_Filesystem $loader Загрузчик Twig.
24+
* @var FilesystemLoader $loader Загрузчик Twig.
2525
*/
2626
private $loader;
2727

@@ -43,13 +43,13 @@ class TwigService
4343
/**
4444
* TwigService constructor.
4545
*
46-
* @param Twig_Loader_Filesystem $loader Загрузчик.
47-
* @param string $debug Среда.
48-
* @param string $cachePath Путь к кэшу (серверный).
49-
* @param array|null $twigOptions Опции Твига.
46+
* @param FilesystemLoader $loader Загрузчик.
47+
* @param string $debug Среда.
48+
* @param string $cachePath Путь к кэшу (серверный).
49+
* @param array|null $twigOptions Опции Твига.
5050
*/
5151
public function __construct(
52-
Twig_Loader_Filesystem $loader,
52+
FilesystemLoader $loader,
5353
string $debug,
5454
string $cachePath,
5555
?array $twigOptions = null
@@ -69,9 +69,9 @@ public function __construct(
6969
/**
7070
* Инстанс Твига.
7171
*
72-
* @return Twig_Environment
72+
* @return Environment
7373
*/
74-
public function instance() : Twig_Environment
74+
public function instance() : Environment
7575
{
7676
return $this->twigEnvironment;
7777
}
@@ -113,19 +113,19 @@ public function addPath(string $path) : void
113113
/**
114114
* Инициализация.
115115
*
116-
* @param Twig_Loader_Filesystem $loader Загрузчик.
117-
* @param string $debug Среда.
118-
* @param string $cachePath Путь к кэшу (серверный).
116+
* @param FilesystemLoader $loader Загрузчик.
117+
* @param string $debug Среда.
118+
* @param string $cachePath Путь к кэшу (серверный).
119119
*
120-
* @return Twig_Environment
120+
* @return Environment
121121
*/
122122
private function initTwig(
123-
Twig_Loader_Filesystem $loader,
123+
FilesystemLoader $loader,
124124
string $debug,
125125
string $cachePath
126-
) : Twig_Environment {
126+
) : Environment {
127127

128-
return new Twig_Environment(
128+
return new Environment(
129129
$loader,
130130
[
131131
'debug' => (bool)$debug,

0 commit comments

Comments
 (0)