14
14
* Extracts translations from twig templates.
15
15
*
16
16
* @author Саша Стаменковић <[email protected] >
17
+ * @author Raphaël Droz <[email protected] >
17
18
*/
18
19
19
20
if (file_exists ($ a = __DIR__ . '/../../autoload.php ' )) {
@@ -22,9 +23,46 @@ if (file_exists($a = __DIR__ . '/../../autoload.php')) {
22
23
require_once __DIR__ . '/vendor/autoload.php ' ;
23
24
}
24
25
25
- $ twig = new Twig_Environment (new Twig \Gettext \Loader \Filesystem (DIRECTORY_SEPARATOR ), [
26
- 'cache ' => implode (DIRECTORY_SEPARATOR , [sys_get_temp_dir (), 'cache ' , uniqid ()]),
27
- 'auto_reload ' => true ,
26
+ use Symfony \Component \Console \Input \ArgvInput ;
27
+ use Symfony \Component \Console \Input \InputDefinition ;
28
+ use Symfony \Component \Console \Input \InputArgument ;
29
+ use Symfony \Component \Console \Input \InputOption ;
30
+
31
+
32
+ // You can add more extensions here, or via command line with the --functions and --filter options
33
+ $ input = new ArgvInput ($ argv , new InputDefinition ([
34
+ new InputOption ('exec ' , null , InputOption::VALUE_REQUIRED , 'xgettext binary ' , 'xgettext ' ),
35
+ new InputOption ('files ' , null , InputOption::VALUE_REQUIRED , 'template files ' ),
36
+ new InputOption ('dir ' , null , InputOption::VALUE_REQUIRED , 'template directory ' , '. ' ),
37
+ new InputOption ('functions ' , null , InputOption::VALUE_REQUIRED , 'custom Twig extensions to load ' ),
38
+ new InputOption ('filters ' , null , InputOption::VALUE_REQUIRED , 'custom Twig filter to support ' ),
39
+ new InputOption ('require ' , null , InputOption::VALUE_REQUIRED , 'Additional PHP file to require ' ),
40
+ new InputOption ('debug ' , 'd ' , InputOption::VALUE_NONE ),
41
+ new InputArgument ('gettext_parameters ' , InputArgument::IS_ARRAY , 'Additional xgettext parameters ' , [])
42
+ ]));
43
+
44
+ // Twig Loaders options
45
+ $ loaders = new Twig_Loader_Chain ();
46
+
47
+ if ($ input ->getOption ('dir ' )) {
48
+ $ directory_loader = new \Twig_Loader_Filesystem ( explode (', ' , $ input ->getOption ('dir ' )) );
49
+ $ loaders ->addLoader ($ directory_loader );
50
+ }
51
+ if ($ input ->getOption ('files ' )) {
52
+ $ files = [];
53
+ foreach (explode (', ' , $ input ->getOption ('files ' )) as $ f ) {
54
+ if (file_exists ($ f )) {
55
+ $ files [$ f ] = file_get_contents ($ f );
56
+ }
57
+ }
58
+ $ loaders ->addLoader (new Twig_Loader_Array ($ files ));
59
+ }
60
+
61
+ // Loader initialized => Initialize Twig Environment
62
+ $ cachedir = implode (DIRECTORY_SEPARATOR , [sys_get_temp_dir (), 'cache ' , uniqid ()]);
63
+ $ twig = new Twig_Environment ($ loaders , [
64
+ 'cache ' => $ cachedir ,
65
+ 'auto_reload ' => true
28
66
]);
29
67
$ twig ->addExtension (new Twig_Extensions_Extension_I18n ());
30
68
$ twig ->addExtension (new Symfony \Bridge \Twig \Extension \TranslationExtension (
@@ -38,43 +76,39 @@ $twig->addExtension(new Symfony\Bridge\Twig\Extension\AssetExtension(
38
76
new Symfony \Component \Asset \Packages ()
39
77
));
40
78
41
- // You can add more extensions here, or via command line with the --functions and --filter options
42
-
43
- array_shift ($ _SERVER ['argv ' ]);
79
+ if ($ input ->getOption ('debug ' )) {
80
+ $ twig ->enableDebug ();
81
+ }
82
+ if ($ input ->getOption ('functions ' )) {
83
+ foreach (explode (', ' , $ input ->getOption ('functions ' )) as $ functionName ) {
84
+ $ twig ->addFunction (new \Twig_SimpleFunction ($ functionName , true ));
85
+ }
86
+ }
87
+ if ($ input ->getOption ('filters ' )) {
88
+ foreach (explode (', ' , $ input ->getOption ('filters ' )) as $ filterName ) {
89
+ $ twig ->addFilter (new \Twig_SimpleFilter ($ filterName , function ($ e ) { return "" ; }));
90
+ }
91
+ }
44
92
45
- $ setFunctions = false ;
46
- $ setFilters = false ;
47
- $ addTemplate = false ;
48
- $ setExecutable = false ;
93
+ // Twig Environment is up => initialize extractor
94
+ $ extractor = new Twig \ Gettext \ Extractor ( $ twig , $ input ) ;
95
+ $ extractor -> setGettextParameters ( $ input -> getArgument ( ' gettext_parameters ' )) ;
96
+ $ extractor -> setExecutable ( $ input -> getOption ( ' exec ' )) ;
49
97
50
- $ extractor = new Twig \Gettext \Extractor ($ twig );
98
+ if ($ input ->getOption ('require ' )) {
99
+ require_once ($ input ->getOption ('require ' ));
100
+ }
51
101
52
- foreach ($ _SERVER ['argv ' ] as $ arg ) {
53
- if ('--files ' === $ arg ) {
54
- $ addTemplate = true ;
55
- } else if ($ addTemplate ) {
56
- $ extractor ->addTemplate (getcwd () . DIRECTORY_SEPARATOR . $ arg );
57
- } else if ('--exec ' === $ arg ) {
58
- $ setExecutable = true ;
59
- } else if ($ setExecutable ) {
60
- $ extractor ->setExecutable ($ arg );
61
- $ setExecutable = false ;
62
- } else if ('--functions ' === $ arg ) {
63
- $ setFunctions = true ;
64
- } else if ($ setFunctions ) {
65
- foreach (explode (', ' , $ arg ) as $ functionName ) {
66
- $ twig ->addFunction (new \Twig_SimpleFunction ($ functionName , true ));
67
- }
68
- $ setFunctions = false ;
69
- } else if ('--filters ' === $ arg ) {
70
- $ setFilters = true ;
71
- } else if ($ setFilters ) {
72
- foreach (explode (', ' , $ arg ) as $ filterName ) {
73
- $ twig ->addFilter (new \Twig_SimpleFilter ($ filterName , true ));
74
- }
75
- $ setFilters = false ;
76
- } else {
77
- $ extractor ->addGettextParameter ($ arg );
102
+ // compile
103
+ if ($ input ->getOption ('files ' )) {
104
+ if ($ input ->getOption ('files ' ) == 'stdin ' ) {
105
+ $ files = explode ("\n" , trim (file_get_contents ('php://stdin ' )));
106
+ }
107
+ else {
108
+ $ files = explode (', ' , $ input ->getOption ('files ' ));
109
+ }
110
+ foreach ($ files as $ f ) {
111
+ $ extractor ->addTemplate ($ f );
78
112
}
79
113
}
80
114
0 commit comments