|
12 | 12 |
|
13 | 13 | use Twig\Gettext\Util\DebugLogger; |
14 | 14 |
|
| 15 | + |
| 16 | +// Init composer |
15 | 17 | if (file_exists($a = __DIR__.'/../../autoload.php')) { |
16 | 18 | require_once $a; |
17 | 19 | } else { |
18 | 20 | require_once __DIR__.'/vendor/autoload.php'; |
19 | 21 | } |
20 | 22 |
|
21 | | -try { |
22 | | - $twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem(DIRECTORY_SEPARATOR), array( |
23 | | - 'cache' => implode(DIRECTORY_SEPARATOR, array(sys_get_temp_dir(), 'cache', uniqid())), |
24 | | - 'auto_reload' => true |
25 | | - )); |
| 23 | +// Init twig + core extensions |
| 24 | +$twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem(DIRECTORY_SEPARATOR), array( |
| 25 | + 'cache' => implode(DIRECTORY_SEPARATOR, array(sys_get_temp_dir(), 'cache', uniqid()) ), |
| 26 | + 'auto_reload' => true |
| 27 | +)); |
| 28 | + |
| 29 | +$twig->addExtension(new Twig_Extensions_Extension_I18n()); |
26 | 30 |
|
27 | | - $twig->addExtension(new Twig_Extensions_Extension_I18n()); |
| 31 | +// Read argv for config |
| 32 | +array_shift($_SERVER['argv']); |
28 | 33 |
|
29 | | - array_shift($_SERVER['argv']); |
30 | | - $addTemplate = false; |
| 34 | +$argvReadingTemplates = false; |
| 35 | +$argvReadingFilters = false; |
31 | 36 |
|
| 37 | +$resetArgv = function () use (&$argvReadingFilters, &$argvReadingTemplates) { |
| 38 | + $argvReadingFilters = false; |
| 39 | + $argvReadingTemplates = false; |
| 40 | +}; |
| 41 | + |
| 42 | +try { |
32 | 43 | $extractor = new Twig\Gettext\Extractor($twig); |
33 | 44 |
|
34 | | - foreach ($_SERVER['argv'] as $arg) { |
35 | | - if ('--files' == $arg) { |
36 | | - $addTemplate = true; |
37 | | - } else if ($addTemplate) { |
38 | | - $extractor->addTemplate(getcwd() . DIRECTORY_SEPARATOR . $arg); |
| 45 | + /** |
| 46 | + * Syntax for argv: |
| 47 | + * |
| 48 | + * twig-gettext-extractor <xgettext arguments> <extractor arguments> |
| 49 | + * |
| 50 | + * xgettext arguments can be "--sort-output", "--force-po", etc and are processed directly. |
| 51 | + * extractor arguments are specific to the twig extractor |
| 52 | + * |
| 53 | + * --files Add a template file to the list of files that need to be extracted. |
| 54 | + * --filters Add a dummy filter that is custom to the project to prevent the extractor breaking. |
| 55 | + */ |
| 56 | + |
| 57 | + foreach ($_SERVER['argv'] as $argument) { |
| 58 | + // Command starters ------------------------------------------------------------------------------------------------ |
| 59 | + if ($argument === '--filters') { |
| 60 | + $resetArgv(); |
| 61 | + $argvReadingFilters = true; |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if ($argument === '--files') { |
| 66 | + $resetArgv(); |
| 67 | + $argvReadingTemplates = true; |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // Command parameters ---------------------------------------------------------------------------------------------- |
| 72 | + if ($argvReadingFilters) { |
| 73 | + // We are in a --filters statement |
| 74 | + $twig->addFilter(new Twig_SimpleFilter($argument, function ($input) { |
| 75 | + return $input; |
| 76 | + })); |
| 77 | + } else if ($argvReadingTemplates) { |
| 78 | + // We are in a --files statement |
| 79 | + $extractor->addTemplate(getcwd() . DIRECTORY_SEPARATOR . $argument); |
39 | 80 | } else { |
40 | | - $extractor->addGettextParameter($arg); |
| 81 | + // We are not in a statement, process as gettext parameter |
| 82 | + $extractor->addGettextParameter($argument); |
41 | 83 | } |
42 | 84 | } |
43 | 85 |
|
|
0 commit comments