]>
Commit | Line | Data |
---|---|---|
a4565e88 NL |
1 | <?php |
2 | ||
3 | /** | |
4 | * This file is part of the Twig Gettext utility. | |
5 | * | |
6 | * (c) Саша Стаменковић <umpirsky@gmail.com> | |
7 | * | |
8 | * For the full copyright and license information, please view the LICENSE | |
9 | * file that was distributed with this source code. | |
10 | */ | |
11 | ||
12 | namespace Twig\Gettext; | |
13 | ||
14 | use Symfony\Component\Filesystem\Filesystem; | |
15 | ||
16 | /** | |
17 | * Extracts translations from twig templates. | |
18 | * | |
19 | * @author Саша Стаменковић <umpirsky@gmail.com> | |
20 | */ | |
21 | class Extractor | |
22 | { | |
23 | /** | |
24 | * @var \Twig_Environment | |
25 | */ | |
26 | protected $environment; | |
27 | ||
28 | /** | |
29 | * Template cached file names. | |
30 | * | |
31 | * @var string[] | |
32 | */ | |
33 | protected $templates; | |
34 | ||
35 | /** | |
36 | * Gettext parameters. | |
37 | * | |
38 | * @var string[] | |
39 | */ | |
40 | protected $parameters; | |
41 | ||
42 | public function __construct(\Twig_Environment $environment) | |
43 | { | |
44 | $this->environment = $environment; | |
45 | $this->reset(); | |
46 | } | |
47 | ||
48 | protected function reset() | |
49 | { | |
50 | $this->templates = array(); | |
51 | $this->parameters = array(); | |
52 | } | |
53 | ||
54 | public function addTemplate($path) | |
55 | { | |
56 | $this->environment->loadTemplate($path); | |
57 | $this->templates[] = $this->environment->getCacheFilename($path); | |
58 | } | |
59 | ||
60 | public function addGettextParameter($parameter) | |
61 | { | |
62 | $this->parameters[] = $parameter; | |
63 | } | |
64 | ||
65 | public function setGettextParameters(array $parameters) | |
66 | { | |
67 | $this->parameters = $parameters; | |
68 | } | |
69 | ||
70 | public function extract() | |
71 | { | |
72 | $command = 'xgettext'; | |
73 | $command .= ' '.join(' ', $this->parameters); | |
74 | $command .= ' '.join(' ', $this->templates); | |
75 | ||
76 | $error = 0; | |
77 | $output = system($command, $error); | |
78 | if (0 !== $error) { | |
79 | throw new \RuntimeException(sprintf( | |
80 | 'Gettext command "%s" failed with error code %s and output: %s', | |
81 | $command, | |
82 | $error, | |
83 | $output | |
84 | )); | |
85 | } | |
86 | ||
87 | $this->reset(); | |
88 | } | |
89 | ||
90 | public function __destruct() | |
91 | { | |
92 | $filesystem = new Filesystem(); | |
93 | $filesystem->remove($this->environment->getCache()); | |
94 | } | |
95 | } |