Skip to content

Commit c6b5943

Browse files
committed
Доработки
1 parent 53e5e9a commit c6b5943

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Resources/config/twig.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ services:
1313
Twig\Extension\ExtensionInterface:
1414
tags: ['twig.extension']
1515

16+
Twig\Loader\LoaderInterface:
17+
tags: [ 'twig.loader' ]
18+
19+
Twig\Extension\RuntimeExtensionInterface:
20+
tags: [ 'twig.runtime' ]
21+
1622
# Twig loader
1723
twig.loader:
1824
class: Twig\Loader\FilesystemLoader
@@ -27,6 +33,8 @@ services:
2733
- '%debug%'
2834
- '%twig_cache_dir%'
2935
- '%twig_config%'
36+
calls:
37+
- setContainer: ['@service_container']
3038

3139
# Экземпляр Twig.
3240
twig.instance:

Services/Twig/TwigService.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Prokl\BitrixOrdinaryToolsBundle\Services\Twig;
44

5+
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
56
use Twig\Environment;
67
use Twig\Error\LoaderError;
78
use Twig\Loader\FilesystemLoader;
@@ -15,6 +16,8 @@
1516
*/
1617
class TwigService
1718
{
19+
use ContainerAwareTrait;
20+
1821
/**
1922
* @var Environment Twig.
2023
*/
@@ -47,6 +50,8 @@ class TwigService
4750
* @param string $debug Среда.
4851
* @param string $cachePath Путь к кэшу (серверный).
4952
* @param array|null $twigOptions Опции Твига.
53+
*
54+
* @throws LoaderError Ошибки Твига.
5055
*/
5156
public function __construct(
5257
FilesystemLoader $loader,
@@ -60,7 +65,7 @@ public function __construct(
6065
$this->cachePath = $cachePath;
6166

6267
$this->twigEnvironment = $this->initTwig(
63-
$loader,
68+
$this->loader,
6469
$debug,
6570
$cachePath
6671
);
@@ -142,6 +147,21 @@ private function initTwig(
142147
}
143148
}
144149

150+
if (!empty($this->twigOptions['globals'])) {
151+
foreach ($this->twigOptions['globals'] as $key => $global) {
152+
if (isset($global['type']) && 'service' === $global['type']) {
153+
$this->twigEnvironment->addGlobal($key, $this->container->get($global['id']));
154+
} else {
155+
$this->twigEnvironment->addGlobal($key, $global['value']);
156+
}
157+
}
158+
}
159+
160+
if (isset($this->twigOptions['autoescape_service']) && isset($this->twigOptions['autoescape_service_method'])) {
161+
$service = $this->container->get($this->twigOptions['autoescape_service']);
162+
$this->twigOptions['autoescape'] = [$service, $this->twigOptions['autoescape_service']['autoescape_service_method']];
163+
}
164+
145165
return new Environment(
146166
$loader,
147167
[

0 commit comments

Comments
 (0)