PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/Console/Command/Task/ExtractTask.php

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