1 | <?php |
||
19 | class Extractor |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Hook class instance |
||
24 | * @var \gplcart\core\Hook $hook |
||
25 | */ |
||
26 | protected $hook; |
||
27 | |||
28 | /** |
||
29 | * Module class instance |
||
30 | * @var \gplcart\core\Module $module |
||
31 | */ |
||
32 | protected $module; |
||
33 | |||
34 | /** |
||
35 | * Max parsing width in columns |
||
36 | */ |
||
37 | const MAX_COLS = 500; |
||
38 | |||
39 | /** |
||
40 | * Max rows to parse per file |
||
41 | */ |
||
42 | const MAX_LINES = 5000; |
||
43 | |||
44 | /** |
||
45 | * Pattern to extract strings from JS function Gplcart.text() |
||
46 | */ |
||
47 | const PATTERN_JS = '/Gplcart.text\s*\(\s*([\'"])(.+?)\1\s*([\),])/s'; |
||
48 | |||
49 | /** |
||
50 | * Pattern to extract strings from TWIG function {{ text() }} |
||
51 | */ |
||
52 | const PATTERN_TWIG = '/text\s*\(\s*([\'"])(.+?)\1\s*([\),])/s'; |
||
53 | |||
54 | /** |
||
55 | * Pattern to extract strings using inline @text annotation that goes before the translatable staring |
||
56 | */ |
||
57 | const PATTERN_ANNOTATION_BEFORE = '/\/\*(?:\*|\s*)@text(?:\*|\s*)\*\/\s*([\'"])(.+?)\1\s*/'; |
||
58 | |||
59 | /** |
||
60 | * Pattern to extract strings using inline // @text annotation that goes after the translatable staring |
||
61 | */ |
||
62 | const PATTERN_ANNOTATION_AFTER = '/([\'"])((?:(?!\1).)+)\1(?:\s*)(?:;*|,*)\s*\/\/\s*@text\s*$/'; |
||
63 | |||
64 | /** |
||
65 | * Pattern to extract strings from Language::text() method |
||
66 | */ |
||
67 | const PATTERN_PHP_METHOD = '/->text\(\s*([\'"])(.+?)\1\s*([\),])/s'; |
||
68 | |||
69 | /** |
||
70 | * Pattern to extract strings from gplcart_text() function |
||
71 | */ |
||
72 | const PATTERN_PHP_FUNCTION = '/gplcart_text\(\s*([\'"])(.+?)\1\s*([\),])/s'; |
||
73 | |||
74 | /** |
||
75 | * @param Hook $hook |
||
76 | * @param Module $module |
||
77 | */ |
||
78 | public function __construct(Hook $hook, Module $module) |
||
83 | |||
84 | /** |
||
85 | * Returns an extractor by a file extension or an array of extractors keyed by extension |
||
86 | * @param null|string $extension |
||
87 | * @return array|string |
||
88 | */ |
||
89 | public function get($extension = null) |
||
108 | |||
109 | /** |
||
110 | * Returns an array of default extractors keyed by supported file extension |
||
111 | * @return array |
||
112 | */ |
||
113 | protected function getDefault() |
||
132 | |||
133 | /** |
||
134 | * Extract strings from module.json |
||
135 | * @param string $file |
||
136 | * @return array |
||
137 | */ |
||
138 | protected function extractFromFileJson($file) |
||
154 | |||
155 | /** |
||
156 | * Returns an array of extracted strings from a file |
||
157 | * @param string $file |
||
158 | * @return array |
||
159 | */ |
||
160 | public function extractFromFile($file) |
||
189 | |||
190 | /** |
||
191 | * Returns an array of extracted strings from a source string |
||
192 | * @param string $string |
||
193 | * @param string|array $regexp_patterns |
||
194 | * @return array |
||
195 | */ |
||
196 | public function extractFromString($string, $regexp_patterns) |
||
216 | |||
217 | /** |
||
218 | * Clean up an array of extracted strings or a single string |
||
219 | * @param array|string $items |
||
220 | * @return array |
||
221 | */ |
||
222 | protected function clean($items) |
||
235 | |||
236 | /** |
||
237 | * Returns an array of scanned files to extract from or counts them |
||
238 | * @param array $options |
||
239 | * @return integer|array |
||
240 | */ |
||
241 | public function scan(array $options) |
||
266 | |||
267 | /** |
||
268 | * Whether the file can be parsed |
||
269 | * @param string $file |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function isSupportedFile($file) |
||
276 | |||
277 | /** |
||
278 | * Returns an array of directories to be scanned |
||
279 | * @return array |
||
280 | */ |
||
281 | public function getScannedDirectories() |
||
293 | |||
294 | /** |
||
295 | * Recursive scans files in a directory |
||
296 | * @param string $directory |
||
297 | * @param array $results |
||
298 | * @return array |
||
299 | */ |
||
300 | protected function scanRecursive($directory, &$results = array()) |
||
324 | |||
325 | } |
||
326 |