Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console-core / src / Utils / TwigRenderer.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Core\Utils\TwigRenderer.
6  */
7
8 namespace Drupal\Console\Core\Utils;
9
10 /**
11  * Class TwigRenderer
12  *
13  * @package Drupal\Console\Core\Utils
14  */
15 class TwigRenderer
16 {
17     /**
18      * @var TranslatorManagerInterface
19      */
20     protected $translator;
21
22     /**
23      * @var \Twig_Environment
24      */
25     protected $engine;
26
27     /**
28      * @var array
29      */
30     protected $skeletonDirs;
31
32     /**
33      * @var StringConverter
34      */
35     protected $stringConverter;
36
37     /**
38      * TwigRenderer constructor.
39      *
40      * @param TranslatorManagerInterface $translator
41      * @param StringConverter            $stringConverter
42      */
43     public function __construct(
44         TranslatorManagerInterface $translator,
45         StringConverter $stringConverter
46     ) {
47         $this->translator = $translator;
48         $this->stringConverter = $stringConverter;
49     }
50
51     /**
52      * @param array $skeletonDirs
53      */
54     public function setSkeletonDirs(array $skeletonDirs)
55     {
56         foreach ($skeletonDirs as $skeletonDir) {
57             $this->addSkeletonDir($skeletonDir);
58         }
59     }
60
61     /**
62      * @param $skeletonDir
63      */
64     public function addSkeletonDir($skeletonDir)
65     {
66         if (is_dir($skeletonDir)) {
67             $this->skeletonDirs[] = $skeletonDir;
68         }
69     }
70
71     /**
72      * @return array
73      */
74     public function getSkeletonDirs()
75     {
76         if (!$this->skeletonDirs) {
77             $this->skeletonDirs[] = __DIR__ . '/../../templates';
78         }
79
80         return $this->skeletonDirs;
81     }
82
83     /**
84      * @param string $template
85      * @param array  $parameters
86      *
87      * @return string
88      */
89     public function render($template, $parameters = [])
90     {
91         if (!$this->engine) {
92             $this->engine = new \Twig_Environment(
93                 new \Twig_Loader_Filesystem($this->getSkeletonDirs()), [
94                 'debug' => true,
95                 'cache' => false,
96                 'strict_variables' => true,
97                 'autoescape' => false,
98                 ]
99             );
100
101             $this->engine->addFunction($this->getServicesAsParameters());
102             $this->engine->addFunction($this->getServicesAsParametersKeys());
103             $this->engine->addFunction($this->getArgumentsFromRoute());
104             $this->engine->addFunction($this->getServicesClassInitialization());
105             $this->engine->addFunction($this->getServicesClassInjection());
106             $this->engine->addFunction($this->getTagsAsArray());
107             $this->engine->addFunction($this->getTranslationAsYamlComment());
108             $this->engine->addFilter($this->createMachineName());
109         }
110
111         return $this->engine->render($template, $parameters);
112     }
113
114     /**
115      * @return \Twig_SimpleFunction
116      */
117     public function getServicesAsParameters()
118     {
119         $servicesAsParameters = new \Twig_SimpleFunction(
120             'servicesAsParameters', function ($services) {
121                 $returnValues = [];
122                 foreach ($services as $service) {
123                     $returnValues[] = sprintf('%s $%s', $service['short'], $service['machine_name']);
124                 }
125
126                 return $returnValues;
127             }
128         );
129
130         return $servicesAsParameters;
131     }
132
133     /**
134      * @return \Twig_SimpleFunction
135      */
136     public function getServicesAsParametersKeys()
137     {
138         $servicesAsParametersKeys = new \Twig_SimpleFunction(
139             'servicesAsParametersKeys', function ($services) {
140                 $returnValues = [];
141                 foreach ($services as $service) {
142                     $returnValues[] = sprintf('\'@%s\'', $service['name']);
143                 }
144
145                 return $returnValues;
146             }
147         );
148
149         return $servicesAsParametersKeys;
150     }
151
152     /**
153      * @return \Twig_SimpleFunction
154      */
155     public function getArgumentsFromRoute()
156     {
157         $argumentsFromRoute = new \Twig_SimpleFunction(
158             'argumentsFromRoute', function ($route) {
159                 $returnValues = '';
160                 preg_match_all('/{(.*?)}/', $route, $returnValues);
161
162                 $returnValues = array_map(
163                     function ($value) {
164                         return sprintf('$%s', $value);
165                     }, $returnValues[1]
166                 );
167
168                 return $returnValues;
169             }
170         );
171
172         return $argumentsFromRoute;
173     }
174
175     /**
176      * @return \Twig_SimpleFunction
177      */
178     public function getServicesClassInitialization()
179     {
180         $returnValue = new \Twig_SimpleFunction(
181             'serviceClassInitialization', function ($services) {
182                 $returnValues = [];
183                 foreach ($services as $service) {
184                     $returnValues[] = sprintf('    $this->%s = $%s;', $service['camel_case_name'], $service['machine_name']);
185                 }
186
187                 return implode(PHP_EOL, $returnValues);
188             }
189         );
190
191         return $returnValue;
192     }
193
194     /**
195      * @return \Twig_SimpleFunction
196      */
197     public function getServicesClassInjection()
198     {
199         $returnValue = new \Twig_SimpleFunction(
200             'serviceClassInjection', function ($services) {
201                 $returnValues = [];
202                 foreach ($services as $service) {
203                     $returnValues[] = sprintf('      $container->get(\'%s\')', $service['name']);
204                 }
205
206                 return implode(','.PHP_EOL, $returnValues);
207             }
208         );
209
210         return $returnValue;
211     }
212
213     /**
214      * @return \Twig_SimpleFunction
215      */
216     public function getTagsAsArray()
217     {
218         $returnValue = new \Twig_SimpleFunction(
219             'tagsAsArray', function ($tags) {
220                 $returnValues = [];
221                 foreach ($tags as $key => $value) {
222                     $returnValues[] = sprintf('%s: %s', $key, $value);
223                 }
224
225                 return $returnValues;
226             }
227         );
228
229         return $returnValue;
230     }
231
232     /**
233      * @return \Twig_SimpleFunction
234      */
235     public function getTranslationAsYamlComment()
236     {
237         $returnValue = new \Twig_SimpleFunction(
238             'yaml_comment', function (\Twig_Environment $environment, $context, $key) {
239                 $message = $this->translator->trans($key);
240                 $messages = explode("\n", $message);
241                 $returnValues = [];
242                 foreach ($messages as $message) {
243                     $returnValues[] = '# '.$message;
244                 }
245
246                 $message = implode("\n", $returnValues);
247                 $template = $environment->createTemplate($message);
248
249                 return $template->render($context);
250             }, [
251             'needs_environment' => true,
252             'needs_context' => true,
253             ]
254         );
255
256         return $returnValue;
257     }
258
259     /**
260      * @return \Twig_SimpleFilter
261      */
262     public function createMachineName()
263     {
264         return new \Twig_SimpleFilter(
265             'machine_name', function ($var) {
266                 return $this->stringConverter->createMachineName($var);
267             }
268         );
269     }
270 }