33namespace TimePHP \Foundation ;
44
55use Closure ;
6+ use Twig \Markup ;
67use Twig \TwigFilter ;
78use Twig \Environment ;
89use Twig \TwigFunction ;
910use Twig \Loader \FilesystemLoader ;
1011use TimePHP \Exception \TwigException ;
1112
12- class Twig {
13+ class Twig
14+ {
1315
1416 /**
1517 * Twig variable
@@ -23,7 +25,8 @@ class Twig {
2325 *
2426 * @param array $options
2527 */
26- public function __construct (array $ options ) {
28+ public function __construct (array $ options )
29+ {
2730
2831 $ this ->twig = new Environment (new FilesystemLoader (__DIR__ . "/../../../../../App/Bundle/Views " ));
2932
@@ -36,7 +39,9 @@ public function __construct(array $options) {
3639 $ this ->twig ->addFunction (new TwigFunction ('generate ' , function (string $ nameUrl , array $ params = [], array $ flags = []): string {
3740 return sprintf (Router::generate ($ nameUrl , $ params , $ flags ));
3841 }));
39-
42+ $ this ->twig ->addFunction (new TwigFunction ('provideCsrf ' , function (string $ csrfInputName = "csrf_token " ): string {
43+ return !empty ($ _SESSION ) ? new Markup ("<input type= \"hidden \" name= \"$ csrfInputName \" value \"" .$ _SESSION ["csrf_token " ]."\"/> " , "utf-8 " ) : "" ;
44+ }, ['is_safe ' => ['html ' ]]));
4045 $ this ->twig ->addFunction (new TwigFunction ('dump ' , function ($ object ): string {
4146 ob_start ();
4247 dump ($ object );
@@ -48,40 +53,41 @@ public function __construct(array $options) {
4853 $ name = $ function ["name " ];
4954
5055 if (array_key_exists ("function " , $ function ) && is_object ($ function ["function " ])) {
51- if ($ function ["type " ] === "function " ) {
56+ if ($ function ["type " ] === "function " ) {
5257 $ this ->twig ->addFunction (new TwigFunction ($ name , $ function ["function " ]));
53- } elseif ($ function ["type " ] === "filter " ) {
58+ } elseif ($ function ["type " ] === "filter " ) {
5459 $ this ->twig ->addFilter (new TwigFilter ($ name , $ function ["function " ]));
5560 }
5661 } else if (array_key_exists ("class " , $ function ) && array_key_exists ("function " , $ function ) && is_callable ([new $ function ["class " ], $ function ["function " ]])) {
5762 $ callable = Closure::fromCallable ([new $ function ["class " ], $ function ["function " ]]);
58- if ($ function ["type " ] === "function " ) {
63+ if ($ function ["type " ] === "function " ) {
5964 $ this ->twig ->addFunction (new TwigFunction ($ name , $ callable ));
60- } elseif ($ function ["type " ] === "filter " ) {
65+ } elseif ($ function ["type " ] === "filter " ) {
6166 $ this ->twig ->addFilter (new TwigFilter ($ name , $ callable ));
6267 }
6368 } else {
6469 if ($ _ENV ["APP_ENV " ] == 0 ) {
6570 header ('HTTP/1.1 500 Internal Server Error ' );
6671 } else {
67- if ($ type === "addFunction " ) {
72+ if ($ function [ " type " ] === "function " ) {
6873 throw new TwigException ("Cannot add the custom twig function : $ name " , 4001 );
69- } elseif ($ type === "addFilter " ) {
70- throw new TwigException ("Cannot add the custom twig filter : $ name " , 4001 );
74+ } elseif ($ function ["type " ] === "filter " ) {
75+ throw new TwigException ("Cannot add the custom twig filter : $ name " , 4002 );
76+ } else {
77+ throw new TwigException ("{$ function ["type " ]} is not a valid twig option type. Must be either function or filter. " , 4003 );
7178 }
7279 }
7380 }
7481 }
75-
7682 }
7783
7884 /**
7985 * Return Twig variable
8086 *
8187 * @return Environment
8288 */
83- public function getRenderer (): Environment {
89+ public function getRenderer (): Environment
90+ {
8491 return $ this ->twig ;
8592 }
86-
87- }
93+ }
0 commit comments