ExtractTask.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. /**
  3. * Language string extractor
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @since CakePHP(tm) v 1.2.0.5012
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. App::uses('AppShell', 'Console/Command');
  19. App::uses('File', 'Utility');
  20. App::uses('Folder', 'Utility');
  21. App::uses('Hash', 'Utility');
  22. /**
  23. * Language string extractor
  24. *
  25. * @package Cake.Console.Command.Task
  26. */
  27. class ExtractTask extends AppShell {
  28. /**
  29. * Paths to use when looking for strings
  30. *
  31. * @var string
  32. */
  33. protected $_paths = array();
  34. /**
  35. * Files from where to extract
  36. *
  37. * @var array
  38. */
  39. protected $_files = array();
  40. /**
  41. * Merge all domains string into the default.pot file
  42. *
  43. * @var boolean
  44. */
  45. protected $_merge = false;
  46. /**
  47. * Current file being processed
  48. *
  49. * @var string
  50. */
  51. protected $_file = null;
  52. /**
  53. * Contains all content waiting to be write
  54. *
  55. * @var string
  56. */
  57. protected $_storage = array();
  58. /**
  59. * Extracted tokens
  60. *
  61. * @var array
  62. */
  63. protected $_tokens = array();
  64. /**
  65. * Extracted strings indexed by domain.
  66. *
  67. * @var array
  68. */
  69. protected $_translations = array();
  70. /**
  71. * Destination path
  72. *
  73. * @var string
  74. */
  75. protected $_output = null;
  76. /**
  77. * An array of directories to exclude.
  78. *
  79. * @var array
  80. */
  81. protected $_exclude = array();
  82. /**
  83. * Holds whether this call should extract model validation messages
  84. *
  85. * @var boolean
  86. */
  87. protected $_extractValidation = true;
  88. /**
  89. * Holds the validation string domain to use for validation messages when extracting
  90. *
  91. * @var boolean
  92. */
  93. protected $_validationDomain = 'default';
  94. /**
  95. * Method to interact with the User and get path selections.
  96. *
  97. * @return void
  98. */
  99. protected function _getPaths() {
  100. $defaultPath = APP;
  101. while (true) {
  102. $currentPaths = count($this->_paths) > 0 ? $this->_paths : array('None');
  103. $message = __d(
  104. 'cake_console',
  105. "Current paths: %s\nWhat is the path you would like to extract?\n[Q]uit [D]one",
  106. implode(', ', $currentPaths)
  107. );
  108. $response = $this->in($message, null, $defaultPath);
  109. if (strtoupper($response) === 'Q') {
  110. $this->out(__d('cake_console', 'Extract Aborted'));
  111. return $this->_stop();
  112. } elseif (strtoupper($response) === 'D' && count($this->_paths)) {
  113. $this->out();
  114. return;
  115. } elseif (strtoupper($response) === 'D') {
  116. $this->err(__d('cake_console', '<warning>No directories selected.</warning> Please choose a directory.'));
  117. } elseif (is_dir($response)) {
  118. $this->_paths[] = $response;
  119. $defaultPath = 'D';
  120. } else {
  121. $this->err(__d('cake_console', 'The directory path you supplied was not found. Please try again.'));
  122. }
  123. $this->out();
  124. }
  125. }
  126. /**
  127. * Execution method always used for tasks
  128. *
  129. * @return void
  130. */
  131. public function execute() {
  132. if (!empty($this->params['exclude'])) {
  133. $this->_exclude = explode(',', $this->params['exclude']);
  134. }
  135. if (isset($this->params['files']) && !is_array($this->params['files'])) {
  136. $this->_files = explode(',', $this->params['files']);
  137. }
  138. if (isset($this->params['paths'])) {
  139. $this->_paths = explode(',', $this->params['paths']);
  140. } elseif (isset($this->params['plugin'])) {
  141. $plugin = Inflector::camelize($this->params['plugin']);
  142. if (!CakePlugin::loaded($plugin)) {
  143. CakePlugin::load($plugin);
  144. }
  145. $this->_paths = array(CakePlugin::path($plugin));
  146. $this->params['plugin'] = $plugin;
  147. } else {
  148. $this->_getPaths();
  149. }
  150. if (!empty($this->params['exclude-plugins']) && $this->_isExtractingApp()) {
  151. $this->_exclude = array_merge($this->_exclude, App::path('plugins'));
  152. }
  153. if (!empty($this->params['ignore-model-validation']) || (!$this->_isExtractingApp() && empty($plugin))) {
  154. $this->_extractValidation = false;
  155. }
  156. if (!empty($this->params['validation-domain'])) {
  157. $this->_validationDomain = $this->params['validation-domain'];
  158. }
  159. if (isset($this->params['output'])) {
  160. $this->_output = $this->params['output'];
  161. } elseif (isset($this->params['plugin'])) {
  162. $this->_output = $this->_paths[0] . DS . 'Locale';
  163. } else {
  164. $message = __d('cake_console', "What is the path you would like to output?\n[Q]uit", $this->_paths[0] . DS . 'Locale');
  165. while (true) {
  166. $response = $this->in($message, null, rtrim($this->_paths[0], DS) . DS . 'Locale');
  167. if (strtoupper($response) === 'Q') {
  168. $this->out(__d('cake_console', 'Extract Aborted'));
  169. $this->_stop();
  170. } elseif (is_dir($response)) {
  171. $this->_output = $response . DS;
  172. break;
  173. } else {
  174. $this->err(__d('cake_console', 'The directory path you supplied was not found. Please try again.'));
  175. }
  176. $this->out();
  177. }
  178. }
  179. if (isset($this->params['merge'])) {
  180. $this->_merge = !(strtolower($this->params['merge']) === 'no');
  181. } else {
  182. $this->out();
  183. $response = $this->in(__d('cake_console', 'Would you like to merge all domains strings into the default.pot file?'), array('y', 'n'), 'n');
  184. $this->_merge = strtolower($response) === 'y';
  185. }
  186. if (empty($this->_files)) {
  187. $this->_searchFiles();
  188. }
  189. $this->_output = rtrim($this->_output, DS) . DS;
  190. $this->_extract();
  191. }
  192. /**
  193. * Add a translation to the internal translations property
  194. *
  195. * Takes care of duplicate translations
  196. *
  197. * @param string $domain
  198. * @param string $msgid
  199. * @param array $details
  200. */
  201. protected function _addTranslation($domain, $msgid, $details = array()) {
  202. if (empty($this->_translations[$domain][$msgid])) {
  203. $this->_translations[$domain][$msgid] = array(
  204. 'msgid_plural' => false
  205. );
  206. }
  207. if (isset($details['msgid_plural'])) {
  208. $this->_translations[$domain][$msgid]['msgid_plural'] = $details['msgid_plural'];
  209. }
  210. if (isset($details['file'])) {
  211. $line = 0;
  212. if (isset($details['line'])) {
  213. $line = $details['line'];
  214. }
  215. $this->_translations[$domain][$msgid]['references'][$details['file']][] = $line;
  216. }
  217. }
  218. /**
  219. * Extract text
  220. *
  221. * @return void
  222. */
  223. protected function _extract() {
  224. $this->out();
  225. $this->out();
  226. $this->out(__d('cake_console', 'Extracting...'));
  227. $this->hr();
  228. $this->out(__d('cake_console', 'Paths:'));
  229. foreach ($this->_paths as $path) {
  230. $this->out(' ' . $path);
  231. }
  232. $this->out(__d('cake_console', 'Output Directory: ') . $this->_output);
  233. $this->hr();
  234. $this->_extractTokens();
  235. $this->_extractValidationMessages();
  236. $this->_buildFiles();
  237. $this->_writeFiles();
  238. $this->_paths = $this->_files = $this->_storage = array();
  239. $this->_translations = $this->_tokens = array();
  240. $this->_extractValidation = true;
  241. $this->out();
  242. $this->out(__d('cake_console', 'Done.'));
  243. }
  244. /**
  245. * Get & configure the option parser
  246. *
  247. * @return void
  248. */
  249. public function getOptionParser() {
  250. $parser = parent::getOptionParser();
  251. return $parser->description(__d('cake_console', 'CakePHP Language String Extraction:'))
  252. ->addOption('app', array('help' => __d('cake_console', 'Directory where your application is located.')))
  253. ->addOption('paths', array('help' => __d('cake_console', 'Comma separated list of paths.')))
  254. ->addOption('merge', array(
  255. 'help' => __d('cake_console', 'Merge all domain strings into the default.po file.'),
  256. 'choices' => array('yes', 'no')
  257. ))
  258. ->addOption('output', array('help' => __d('cake_console', 'Full path to output directory.')))
  259. ->addOption('files', array('help' => __d('cake_console', 'Comma separated list of files.')))
  260. ->addOption('exclude-plugins', array(
  261. 'boolean' => true,
  262. 'default' => true,
  263. 'help' => __d('cake_console', 'Ignores all files in plugins if this command is run inside from the same app directory.')
  264. ))
  265. ->addOption('plugin', array(
  266. 'help' => __d('cake_console', 'Extracts tokens only from the plugin specified and puts the result in the plugin\'s Locale directory.')
  267. ))
  268. ->addOption('ignore-model-validation', array(
  269. 'boolean' => true,
  270. 'default' => false,
  271. 'help' => __d('cake_console', 'Ignores validation messages in the $validate property.' .
  272. ' If this flag is not set and the command is run from the same app directory,' .
  273. ' all messages in model validation rules will be extracted as tokens.')
  274. ))
  275. ->addOption('validation-domain', array(
  276. 'help' => __d('cake_console', 'If set to a value, the localization domain to be used for model validation messages.')
  277. ))
  278. ->addOption('exclude', array(
  279. 'help' => __d('cake_console', 'Comma separated list of directories to exclude.' .
  280. ' Any path containing a path segment with the provided values will be skipped. E.g. test,vendors')
  281. ))
  282. ->addOption('overwrite', array(
  283. 'boolean' => true,
  284. 'default' => false,
  285. 'help' => __d('cake_console', 'Always overwrite existing .pot files.')
  286. ));
  287. }
  288. /**
  289. * Extract tokens out of all files to be processed
  290. *
  291. * @return void
  292. */
  293. protected function _extractTokens() {
  294. foreach ($this->_files as $file) {
  295. $this->_file = $file;
  296. $this->out(__d('cake_console', 'Processing %s...', $file));
  297. $code = file_get_contents($file);
  298. $allTokens = token_get_all($code);
  299. $this->_tokens = array();
  300. foreach ($allTokens as $token) {
  301. if (!is_array($token) || ($token[0] != T_WHITESPACE && $token[0] != T_INLINE_HTML)) {
  302. $this->_tokens[] = $token;
  303. }
  304. }
  305. unset($allTokens);
  306. $this->_parse('__', array('singular'));
  307. $this->_parse('__n', array('singular', 'plural'));
  308. $this->_parse('__d', array('domain', 'singular'));
  309. $this->_parse('__c', array('singular'));
  310. $this->_parse('__dc', array('domain', 'singular'));
  311. $this->_parse('__dn', array('domain', 'singular', 'plural'));
  312. $this->_parse('__dcn', array('domain', 'singular', 'plural'));
  313. }
  314. }
  315. /**
  316. * Parse tokens
  317. *
  318. * @param string $functionName Function name that indicates translatable string (e.g: '__')
  319. * @param array $map Array containing what variables it will find (e.g: domain, singular, plural)
  320. * @return void
  321. */
  322. protected function _parse($functionName, $map) {
  323. $count = 0;
  324. $tokenCount = count($this->_tokens);
  325. while (($tokenCount - $count) > 1) {
  326. $countToken = $this->_tokens[$count];
  327. $firstParenthesis = $this->_tokens[$count + 1];
  328. if (!is_array($countToken)) {
  329. $count++;
  330. continue;
  331. }
  332. list($type, $string, $line) = $countToken;
  333. if (($type == T_STRING) && ($string == $functionName) && ($firstParenthesis == '(')) {
  334. $position = $count;
  335. $depth = 0;
  336. while ($depth == 0) {
  337. if ($this->_tokens[$position] == '(') {
  338. $depth++;
  339. } elseif ($this->_tokens[$position] == ')') {
  340. $depth--;
  341. }
  342. $position++;
  343. }
  344. $mapCount = count($map);
  345. $strings = $this->_getStrings($position, $mapCount);
  346. if ($mapCount == count($strings)) {
  347. extract(array_combine($map, $strings));
  348. $domain = isset($domain) ? $domain : 'default';
  349. $details = array(
  350. 'file' => $this->_file,
  351. 'line' => $line,
  352. );
  353. if (isset($plural)) {
  354. $details['msgid_plural'] = $plural;
  355. }
  356. $this->_addTranslation($domain, $singular, $details);
  357. } else {
  358. $this->_markerError($this->_file, $line, $functionName, $count);
  359. }
  360. }
  361. $count++;
  362. }
  363. }
  364. /**
  365. * Looks for models in the application and extracts the validation messages
  366. * to be added to the translation map
  367. *
  368. * @return void
  369. */
  370. protected function _extractValidationMessages() {
  371. if (!$this->_extractValidation) {
  372. return;
  373. }
  374. App::uses('AppModel', 'Model');
  375. $plugin = null;
  376. if (!empty($this->params['plugin'])) {
  377. App::uses($this->params['plugin'] . 'AppModel', $this->params['plugin'] . '.Model');
  378. $plugin = $this->params['plugin'] . '.';
  379. }
  380. $models = App::objects($plugin . 'Model', null, false);
  381. foreach ($models as $model) {
  382. App::uses($model, $plugin . 'Model');
  383. $reflection = new ReflectionClass($model);
  384. if (!$reflection->isSubClassOf('Model')) {
  385. continue;
  386. }
  387. $properties = $reflection->getDefaultProperties();
  388. $validate = $properties['validate'];
  389. if (empty($validate)) {
  390. continue;
  391. }
  392. $file = $reflection->getFileName();
  393. $domain = $this->_validationDomain;
  394. if (!empty($properties['validationDomain'])) {
  395. $domain = $properties['validationDomain'];
  396. }
  397. foreach ($validate as $field => $rules) {
  398. $this->_processValidationRules($field, $rules, $file, $domain);
  399. }
  400. }
  401. }
  402. /**
  403. * Process a validation rule for a field and looks for a message to be added
  404. * to the translation map
  405. *
  406. * @param string $field the name of the field that is being processed
  407. * @param array $rules the set of validation rules for the field
  408. * @param string $file the file name where this validation rule was found
  409. * @param string $domain default domain to bind the validations to
  410. * @return void
  411. */
  412. protected function _processValidationRules($field, $rules, $file, $domain) {
  413. if (!is_array($rules)) {
  414. return;
  415. }
  416. $dims = Hash::dimensions($rules);
  417. if ($dims == 1 || ($dims == 2 && isset($rules['message']))) {
  418. $rules = array($rules);
  419. }
  420. foreach ($rules as $rule => $validateProp) {
  421. $msgid = null;
  422. if (isset($validateProp['message'])) {
  423. if (is_array($validateProp['message'])) {
  424. $msgid = $validateProp['message'][0];
  425. } else {
  426. $msgid = $validateProp['message'];
  427. }
  428. } elseif (is_string($rule)) {
  429. $msgid = $rule;
  430. }
  431. if ($msgid) {
  432. $details = array(
  433. 'file' => $file,
  434. 'line' => 'validation for field ' . $field
  435. );
  436. $this->_addTranslation($domain, $msgid, $details);
  437. }
  438. }
  439. }
  440. /**
  441. * Build the translate template file contents out of obtained strings
  442. *
  443. * @return void
  444. */
  445. protected function _buildFiles() {
  446. $paths = $this->_paths;
  447. $paths[] = realpath(APP) . DS;
  448. foreach ($this->_translations as $domain => $translations) {
  449. foreach ($translations as $msgid => $details) {
  450. $plural = $details['msgid_plural'];
  451. $files = $details['references'];
  452. $occurrences = array();
  453. foreach ($files as $file => $lines) {
  454. $lines = array_unique($lines);
  455. $occurrences[] = $file . ':' . implode(';', $lines);
  456. }
  457. $occurrences = implode("\n#: ", $occurrences);
  458. $header = '#: ' . str_replace($paths, '', $occurrences) . "\n";
  459. if ($plural === false) {
  460. $sentence = "msgid \"{$msgid}\"\n";
  461. $sentence .= "msgstr \"\"\n\n";
  462. } else {
  463. $sentence = "msgid \"{$msgid}\"\n";
  464. $sentence .= "msgid_plural \"{$plural}\"\n";
  465. $sentence .= "msgstr[0] \"\"\n";
  466. $sentence .= "msgstr[1] \"\"\n\n";
  467. }
  468. $this->_store($domain, $header, $sentence);
  469. if ($domain != 'default' && $this->_merge) {
  470. $this->_store('default', $header, $sentence);
  471. }
  472. }
  473. }
  474. }
  475. /**
  476. * Prepare a file to be stored
  477. *
  478. * @param string $domain
  479. * @param string $header
  480. * @param string $sentence
  481. * @return void
  482. */
  483. protected function _store($domain, $header, $sentence) {
  484. if (!isset($this->_storage[$domain])) {
  485. $this->_storage[$domain] = array();
  486. }
  487. if (!isset($this->_storage[$domain][$sentence])) {
  488. $this->_storage[$domain][$sentence] = $header;
  489. } else {
  490. $this->_storage[$domain][$sentence] .= $header;
  491. }
  492. }
  493. /**
  494. * Write the files that need to be stored
  495. *
  496. * @return void
  497. */
  498. protected function _writeFiles() {
  499. $overwriteAll = false;
  500. if (!empty($this->params['overwrite'])) {
  501. $overwriteAll = true;
  502. }
  503. foreach ($this->_storage as $domain => $sentences) {
  504. $output = $this->_writeHeader();
  505. foreach ($sentences as $sentence => $header) {
  506. $output .= $header . $sentence;
  507. }
  508. $filename = $domain . '.pot';
  509. $File = new File($this->_output . $filename);
  510. $response = '';
  511. while ($overwriteAll === false && $File->exists() && strtoupper($response) !== 'Y') {
  512. $this->out();
  513. $response = $this->in(
  514. __d('cake_console', 'Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll', $filename),
  515. array('y', 'n', 'a'),
  516. 'y'
  517. );
  518. if (strtoupper($response) === 'N') {
  519. $response = '';
  520. while ($response == '') {
  521. $response = $this->in(__d('cake_console', "What would you like to name this file?"), null, 'new_' . $filename);
  522. $File = new File($this->_output . $response);
  523. $filename = $response;
  524. }
  525. } elseif (strtoupper($response) === 'A') {
  526. $overwriteAll = true;
  527. }
  528. }
  529. $File->write($output);
  530. $File->close();
  531. }
  532. }
  533. /**
  534. * Build the translation template header
  535. *
  536. * @return string Translation template header
  537. */
  538. protected function _writeHeader() {
  539. $output = "# LANGUAGE translation of CakePHP Application\n";
  540. $output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
  541. $output .= "#\n";
  542. $output .= "#, fuzzy\n";
  543. $output .= "msgid \"\"\n";
  544. $output .= "msgstr \"\"\n";
  545. $output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  546. $output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
  547. $output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
  548. $output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  549. $output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
  550. $output .= "\"MIME-Version: 1.0\\n\"\n";
  551. $output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  552. $output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  553. $output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
  554. return $output;
  555. }
  556. /**
  557. * Get the strings from the position forward
  558. *
  559. * @param integer $position Actual position on tokens array
  560. * @param integer $target Number of strings to extract
  561. * @return array Strings extracted
  562. */
  563. protected function _getStrings(&$position, $target) {
  564. $strings = array();
  565. $count = count($strings);
  566. while ($count < $target && ($this->_tokens[$position] == ',' || $this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING)) {
  567. $count = count($strings);
  568. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING && $this->_tokens[$position + 1] == '.') {
  569. $string = '';
  570. while ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING || $this->_tokens[$position] == '.') {
  571. if ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  572. $string .= $this->_formatString($this->_tokens[$position][1]);
  573. }
  574. $position++;
  575. }
  576. $strings[] = $string;
  577. } elseif ($this->_tokens[$position][0] == T_CONSTANT_ENCAPSED_STRING) {
  578. $strings[] = $this->_formatString($this->_tokens[$position][1]);
  579. }
  580. $position++;
  581. }
  582. return $strings;
  583. }
  584. /**
  585. * Format a string to be added as a translatable string
  586. *
  587. * @param string $string String to format
  588. * @return string Formatted string
  589. */
  590. protected function _formatString($string) {
  591. $quote = substr($string, 0, 1);
  592. $string = substr($string, 1, -1);
  593. if ($quote == '"') {
  594. $string = stripcslashes($string);
  595. } else {
  596. $string = strtr($string, array("\\'" => "'", "\\\\" => "\\"));
  597. }
  598. $string = str_replace("\r\n", "\n", $string);
  599. return addcslashes($string, "\0..\37\\\"");
  600. }
  601. /**
  602. * Indicate an invalid marker on a processed file
  603. *
  604. * @param string $file File where invalid marker resides
  605. * @param integer $line Line number
  606. * @param string $marker Marker found
  607. * @param integer $count Count
  608. * @return void
  609. */
  610. protected function _markerError($file, $line, $marker, $count) {
  611. $this->out(__d('cake_console', "Invalid marker content in %s:%s\n* %s(", $file, $line, $marker), true);
  612. $count += 2;
  613. $tokenCount = count($this->_tokens);
  614. $parenthesis = 1;
  615. while ((($tokenCount - $count) > 0) && $parenthesis) {
  616. if (is_array($this->_tokens[$count])) {
  617. $this->out($this->_tokens[$count][1], false);
  618. } else {
  619. $this->out($this->_tokens[$count], false);
  620. if ($this->_tokens[$count] == '(') {
  621. $parenthesis++;
  622. }
  623. if ($this->_tokens[$count] == ')') {
  624. $parenthesis--;
  625. }
  626. }
  627. $count++;
  628. }
  629. $this->out("\n", true);
  630. }
  631. /**
  632. * Search files that may contain translatable strings
  633. *
  634. * @return void
  635. */
  636. protected function _searchFiles() {
  637. $pattern = false;
  638. if (!empty($this->_exclude)) {
  639. $exclude = array();
  640. foreach ($this->_exclude as $e) {
  641. if (DS !== '\\' && $e[0] !== DS) {
  642. $e = DS . $e;
  643. }
  644. $exclude[] = preg_quote($e, '/');
  645. }
  646. $pattern = '/' . implode('|', $exclude) . '/';
  647. }
  648. foreach ($this->_paths as $path) {
  649. $Folder = new Folder($path);
  650. $files = $Folder->findRecursive('.*\.(php|ctp|thtml|inc|tpl)', true);
  651. if (!empty($pattern)) {
  652. foreach ($files as $i => $file) {
  653. if (preg_match($pattern, $file)) {
  654. unset($files[$i]);
  655. }
  656. }
  657. $files = array_values($files);
  658. }
  659. $this->_files = array_merge($this->_files, $files);
  660. }
  661. }
  662. /**
  663. * Returns whether this execution is meant to extract string only from directories in folder represented by the
  664. * APP constant, i.e. this task is extracting strings from same application.
  665. *
  666. * @return boolean
  667. */
  668. protected function _isExtractingApp() {
  669. return $this->_paths === array(APP);
  670. }
  671. }