99
1010namespace Kenjis \CI4Twig ;
1111
12+ use Config \Services ;
13+ use Twig \Environment ;
14+ use Twig \Error \LoaderError ;
15+ use Twig \Error \RuntimeError ;
16+ use Twig \Error \SyntaxError ;
17+ use Twig \Extension \DebugExtension ;
18+ use Twig \Loader \FilesystemLoader ;
19+ use Twig \TwigFunction ;
20+
1221class Twig
1322{
1423 /**
@@ -26,7 +35,7 @@ class Twig
2635 /**
2736 * @var array Functions to add to Twig
2837 */
29- private $ functions_asis = [
38+ private array $ functions_asis = [
3039 'base_url ' ,
3140 'site_url ' ,
3241 ];
@@ -36,7 +45,7 @@ class Twig
3645 *
3746 * @see https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping
3847 */
39- private $ functions_safe = [
48+ private array $ functions_safe = [
4049 'form_open ' ,
4150 'form_close ' ,
4251 'form_error ' ,
@@ -50,15 +59,12 @@ class Twig
5059 /**
5160 * @var bool Whether functions are added or not
5261 */
53- private $ functions_added = false ;
62+ private bool $ functions_added = false ;
5463
55- /**
56- * @var \Twig\Environment|null
57- */
58- private $ twig ;
64+ private ?Environment $ twig = null ;
5965
6066 /**
61- * @var \Twig\Loader\ FilesystemLoader
67+ * @var FilesystemLoader
6268 */
6369 private $ loader ;
6470
@@ -112,13 +118,13 @@ protected function createTwig()
112118 }
113119
114120 if ($ this ->loader === null ) {
115- $ this ->loader = new \ Twig \ Loader \ FilesystemLoader ($ this ->paths );
121+ $ this ->loader = new FilesystemLoader ($ this ->paths );
116122 }
117123
118- $ twig = new \ Twig \ Environment ($ this ->loader , $ this ->config );
124+ $ twig = new Environment ($ this ->loader , $ this ->config );
119125
120126 if ($ this ->config ['debug ' ]) {
121- $ twig ->addExtension (new \ Twig \ Extension \ DebugExtension ());
127+ $ twig ->addExtension (new DebugExtension ());
122128 }
123129
124130 $ this ->twig = $ twig ;
@@ -147,9 +153,9 @@ public function addGlobal($name, $value)
147153 * @param string $view Template filename without `.twig`
148154 * @param array $params Array of parameters to pass to the template
149155 *
150- * @throws \Twig\Error\ LoaderError
151- * @throws \Twig\Error\ RuntimeError
152- * @throws \Twig\Error\ SyntaxError
156+ * @throws LoaderError
157+ * @throws RuntimeError
158+ * @throws SyntaxError
153159 */
154160 public function display ($ view , $ params = [])
155161 {
@@ -162,9 +168,9 @@ public function display($view, $params = [])
162168 * @param string $view Template filename without `.twig`
163169 * @param array $params Array of parameters to pass to the template
164170 *
165- * @throws \Twig\Error\ LoaderError
166- * @throws \Twig\Error\ RuntimeError
167- * @throws \Twig\Error\ SyntaxError
171+ * @throws LoaderError
172+ * @throws RuntimeError
173+ * @throws SyntaxError
168174 */
169175 public function render ($ view , $ params = []): string
170176 {
@@ -189,7 +195,7 @@ protected function addFunctions()
189195 foreach ($ this ->functions_asis as $ function ) {
190196 if (function_exists ($ function )) {
191197 $ this ->twig ->addFunction (
192- new \ Twig \ TwigFunction (
198+ new TwigFunction (
193199 $ function ,
194200 $ function
195201 )
@@ -201,7 +207,7 @@ protected function addFunctions()
201207 foreach ($ this ->functions_safe as $ function ) {
202208 if (function_exists ($ function )) {
203209 $ this ->twig ->addFunction (
204- new \ Twig \ TwigFunction (
210+ new TwigFunction (
205211 $ function ,
206212 $ function ,
207213 ['is_safe ' => ['html ' ]]
@@ -213,7 +219,7 @@ protected function addFunctions()
213219 // customized functions
214220 if (function_exists ('anchor ' )) {
215221 $ this ->twig ->addFunction (
216- new \ Twig \ TwigFunction (
222+ new TwigFunction (
217223 'anchor ' ,
218224 [$ this , 'safe_anchor ' ],
219225 ['is_safe ' => ['html ' ]]
@@ -222,7 +228,7 @@ protected function addFunctions()
222228 }
223229
224230 $ this ->twig ->addFunction (
225- new \ Twig \ TwigFunction (
231+ new TwigFunction (
226232 'validation_list_errors ' ,
227233 [$ this , 'validation_list_errors ' ],
228234 ['is_safe ' => ['html ' ]]
@@ -256,10 +262,10 @@ public function safe_anchor(
256262
257263 public function validation_list_errors (): string
258264 {
259- return \ Config \ Services::validation ()->listErrors ();
265+ return Services::validation ()->listErrors ();
260266 }
261267
262- public function getTwig (): \ Twig \ Environment
268+ public function getTwig (): Environment
263269 {
264270 $ this ->createTwig ();
265271
0 commit comments