22
33namespace Prokl \BitrixOrdinaryToolsBundle \Services \Twig ;
44
5+ use Symfony \Component \DependencyInjection \ContainerAwareTrait ;
56use Twig \Environment ;
67use Twig \Error \LoaderError ;
78use Twig \Loader \FilesystemLoader ;
1516 */
1617class 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