extract.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. #!/usr/bin/php -q
  2. <?php
  3. /* SVN FILE: $Id$ */
  4. /**
  5. * Short description for file.
  6. *
  7. * Long description for file
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  12. * Copyright 2005-2007, Cake Software Foundation, Inc.
  13. * 1785 E. Sahara Avenue, Suite 490-204
  14. * Las Vegas, Nevada 89104
  15. *
  16. * Licensed under The MIT License
  17. * Redistributions of files must retain the above copyright notice.
  18. *
  19. * @filesource
  20. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  21. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  22. * @package cake
  23. * @subpackage cake.cake.scripts
  24. * @since CakePHP(tm) v 1.2.0.4708
  25. * @version $Revision$
  26. * @modifiedby $LastChangedBy$
  27. * @lastmodified $Date$
  28. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  29. */
  30. define ('DS', DIRECTORY_SEPARATOR);
  31. if (function_exists('ini_set')) {
  32. ini_set('display_errors', '1');
  33. ini_set('error_reporting', '7');
  34. ini_set('memory_limit', '16M');
  35. ini_set('max_execution_time', 0);
  36. }
  37. $app = null;
  38. $root = dirname(dirname(dirname(__FILE__)));
  39. $core = null;
  40. $help = null;
  41. $files = array();
  42. $path = null;
  43. for ($i = 1; $i < count($argv); $i += 2) {
  44. switch ($argv[$i]) {
  45. case '-a':
  46. case '-app':
  47. $app = $argv[$i + 1];
  48. break;
  49. case '-c':
  50. case '-core':
  51. $core = $argv[$i + 1];
  52. break;
  53. case '-r':
  54. case '-root':
  55. $root = $argv[$i + 1];
  56. break;
  57. case '-h':
  58. case '-help':
  59. $help = true;
  60. break;
  61. case '-f' :
  62. case '-files' :
  63. $files = $argv[$i + 1];
  64. break;
  65. case '-p':
  66. case '-path':
  67. $path = $argv[$i + 1];
  68. break;
  69. case '-debug' :
  70. $files = array(__FILE__);
  71. break;
  72. }
  73. }
  74. if(!$app) {
  75. $app = 'app';
  76. }
  77. if(!is_dir($app)) {
  78. $project = true;
  79. $projectPath = $app;
  80. }
  81. if($project) {
  82. $app = $projectPath;
  83. }
  84. $shortPath = str_replace($root, '', $app);
  85. $shortPath = str_replace('..'.DS, '', $shortPath);
  86. $shortPath = str_replace(DS.DS, DS, $shortPath);
  87. $pathArray = explode(DS, $shortPath);
  88. if(end($pathArray) != '') {
  89. $appDir = array_pop($pathArray);
  90. } else {
  91. array_pop($pathArray);
  92. $appDir = array_pop($pathArray);
  93. }
  94. $rootDir = implode(DS, $pathArray);
  95. $rootDir = str_replace(DS.DS, DS, $rootDir);
  96. if(!$rootDir) {
  97. $rootDir = $root;
  98. $projectPath = $root.DS.$appDir;
  99. }
  100. define ('ROOT', $rootDir);
  101. define ('APP_DIR', $appDir);
  102. define ('DEBUG', 1);;
  103. define('CAKE_CORE_INCLUDE_PATH', $root);
  104. if(function_exists('ini_set')) {
  105. ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . ROOT . DS . APP_DIR . DS . PATH_SEPARATOR . ini_get('include_path'));
  106. define('APP_PATH', null);
  107. define('CORE_PATH', null);
  108. } else {
  109. define('APP_PATH', ROOT . DS . APP_DIR . DS);
  110. define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
  111. }
  112. require_once (CORE_PATH.'cake'.DS.'basics.php');
  113. require_once (CORE_PATH.'cake'.DS.'config'.DS.'paths.php');
  114. uses('object', 'configure');
  115. $extract = new I18nExtractor();
  116. if(!empty($files) && !is_array($files)){
  117. $extract->files = explode(',', $files);
  118. } else {
  119. $extract->files = $files;
  120. }
  121. if($path) {
  122. $extract->path = $path;
  123. }
  124. if($help === true){
  125. $extract->help();
  126. exit();
  127. }
  128. $extract->main();
  129. return;
  130. // Only used when -debug option
  131. $singularReturn = __('Singular string return __()', true);
  132. $singularEcho = __('Singular string echo __()');
  133. $pluralReturn = __n('% apple in the bowl (plural string return __n())', '% apples in the blowl (plural string 2 return __n())', 3, true);
  134. $pluralEcho = __n('% apple in the bowl (plural string 2 echo __n())', '% apples in the blowl (plural string 2 echo __n()', 3);
  135. $singularDomainReturn = __d('controllers', 'Singular string domain lookup return __d()', true);
  136. $singularDomainEcho = __d('controllers', 'Singular string domain lookup echo __d()');
  137. $pluralDomainReturn = __dn('controllers', '% pears in the bowl (plural string domain lookup return __dn())', '% pears in the blowl (plural string domain lookup return __dn())', 3, true);
  138. $pluralDomainEcho = __dn('controllers', '% pears in the bowl (plural string domain lookup echo __dn())', '% pears in the blowl (plural string domain lookup echo __dn())', 3);
  139. $singularDomainCategoryReturn = __dc('controllers', 'Singular string domain and category lookup return __dc()', 5, true);
  140. $singularDomainCategoryEcho = __dc('controllers', 'Singular string domain and category lookup echo __dc()', 5);
  141. $pluralDomainCategoryReturn = __dcn('controllers', '% apple in the bowl (plural string 1 domain and category lookup return __dcn())', '% apples in the blowl (plural string 2 domain and category lookup return __dcn())', 3, 5, true);
  142. $pluralDomainCategoryEcho = __dcn('controllers', '% apple in the bowl (plural string 1 domain and category lookup echo __dcn())', '% apples in the blowl (plural string 2 domain and category lookup echo __dcn())', 3, 5);
  143. $categoryReturn = __c('Category string lookup line return __c()', 5, true);
  144. $categoryEcho = __c('Category string lookup line echo __c()', 5);
  145. /**
  146. * Language string extractor
  147. *
  148. * @package cake
  149. * @subpackage cake.cake.scripts
  150. */
  151. class I18nExtractor {
  152. var $stdin;
  153. var $stdout;
  154. var $stderr;
  155. var $path = null;
  156. var $files = array();
  157. var $__filename = 'default';
  158. var $__oneFile = true;
  159. var $__file = null;
  160. var $__tokens = array();
  161. var $__strings = array();
  162. var $__fileVersions = array();
  163. var $__output = null;
  164. function __construct() {
  165. $this->stdin = fopen('php://stdin', 'r');
  166. $this->stdout = fopen('php://stdout', 'w');
  167. $this->stderr = fopen('php://stderr', 'w');
  168. $this->path = APP;
  169. $this->__output = APP . 'locale' . DS;
  170. $this->__welcome();
  171. }
  172. function I18nExtractor() {
  173. return $this->__construct();
  174. }
  175. function main() {
  176. $this->__stdout('');
  177. $this->__stdout('');
  178. $this->__stdout('Extracting...');
  179. $this->__hr();
  180. $this->__stdout('Path: '. $this->path);
  181. $this->__stdout('Output Directory: '. $this->__output);
  182. $this->__hr();
  183. $response = '';
  184. $filename = '';
  185. while($response == '') {
  186. $response = $this->__getInput('Would you like to merge all translations into one file?', array('y','n'), 'y');
  187. if(strtolower($response) == 'n') {
  188. $this->__oneFile = false;
  189. } else {
  190. while($filename == '') {
  191. $filename = $this->__getInput('What should we name this file?', null, $this->__filename);
  192. if ($filename == '') {
  193. $this->__stdout('The filesname you supplied was empty. Please try again.');
  194. }
  195. }
  196. $this->__filename = $filename;
  197. }
  198. }
  199. if(empty($this->files)){
  200. $this->files = $this->__searchDirectory();
  201. }
  202. $this->__extractTokens();
  203. }
  204. function help() {
  205. $this->__stdout('CakePHP Language String Extraction:');
  206. $this->__hr();
  207. $this->__stdout('The Extract script generates .pot file(s) with translations');
  208. $this->__stdout('The .pot file(s) will be place in the locale directory of -app');
  209. $this->__stdout('By default -app is ROOT/app');
  210. $this->__stdout('');
  211. $this->__hr('');
  212. $this->__stdout('usage: php extract.php [command] [path...]');
  213. $this->__stdout('');
  214. $this->__stdout('commands:');
  215. $this->__stdout(' -app or -a: directory where your application is located');
  216. $this->__stdout(' -root or -r: path to install');
  217. $this->__stdout(' -core or -c: path to cake directory');
  218. $this->__stdout(' -path or -p: [path...] Full path to directory to extract strings');
  219. $this->__stdout(' -files or -f: [comma separated list of files]');
  220. $this->__stdout(' -help or -h: Shows this help message.');
  221. $this->__stdout(' -debug or -d: Perform self test.');
  222. $this->__stdout('');
  223. }
  224. function __welcome() {
  225. $this->__stdout('');
  226. $this->__stdout(' ___ __ _ _ ___ __ _ _ __');
  227. $this->__stdout('| |__| |_/ |__ |__] |__| |__]');
  228. $this->__stdout('|___ | | | \_ |___ | | | |');
  229. $this->__hr();
  230. $this->__stdout('');
  231. }
  232. function __extractTokens(){
  233. foreach ($this->files as $file) {
  234. $this->__file = $file;
  235. $this->__stdout("Processing $file...");
  236. $code = file_get_contents($file);
  237. $this->__findVersion($code, $file);
  238. $allTokens = token_get_all($code);
  239. $this->__tokens = array();
  240. $lineNumber = 1;
  241. foreach($allTokens as $token) {
  242. if((!is_array($token)) || (($token[0] != T_WHITESPACE) && ($token[0] != T_INLINE_HTML))) {
  243. if(is_array($token)) {
  244. $token[] = $lineNumber;
  245. }
  246. $this->__tokens[] = $token;
  247. }
  248. if(is_array($token)) {
  249. $lineNumber += count(split("\n", $token[1])) - 1;
  250. } else {
  251. $lineNumber += count(split("\n", $token)) - 1;
  252. }
  253. }
  254. unset($allTokens);
  255. $this->basic();
  256. $this->basic('__c');
  257. $this->extended();
  258. $this->extended('__dc', 2);
  259. $this->extended('__n', 0, true);
  260. $this->extended('__dn', 2, true);
  261. $this->extended('__dcn', 4, true);
  262. }
  263. $this->__buildFiles();
  264. $this->__writeFiles();
  265. $this->__stdout('Done.');
  266. }
  267. /**
  268. * Will parse __(), __c() functions
  269. *
  270. * @param string $functionname
  271. */
  272. function basic($functionname = '__') {
  273. $count = 0;
  274. $tokenCount = count($this->__tokens);
  275. while(($tokenCount - $count) > 3) {
  276. list($countToken, $parenthesis, $middle, $right) = array($this->__tokens[$count], $this->__tokens[$count + 1], $this->__tokens[$count + 2], $this->__tokens[$count + 3]);
  277. if (!is_array($countToken)) {
  278. $count++;
  279. continue;
  280. }
  281. list($type, $string, $line) = $countToken;
  282. if(($type == T_STRING) && ($string == $functionname) && ($parenthesis == "(")) {
  283. if(in_array($right, array(")", ","))
  284. && (is_array($middle) && ($middle[0] == T_CONSTANT_ENCAPSED_STRING))) {
  285. if ($this->__oneFile === true) {
  286. $this->__strings[$this->__formatString($middle[1])][$this->__file][] = $line;
  287. } else {
  288. $this->__strings[$this->__file][$this->__formatString($middle[1])][] = $line;
  289. }
  290. } else {
  291. $this->__markerError($this->__file, $line, $functionname, $count);
  292. }
  293. }
  294. $count++;
  295. }
  296. }
  297. /**
  298. * Will parse __d(), __dc(), __n(), __dn(), __dcn()
  299. *
  300. * @param string $functionname
  301. * @param integer $shift
  302. * @param boolean $plural
  303. */
  304. function extended($functionname = '__d', $shift = 0, $plural = false) {
  305. $count = 0;
  306. $tokenCount = count($this->__tokens);
  307. while(($tokenCount - $count) > 7) {
  308. list($countToken, $firstParenthesis) = array($this->__tokens[$count], $this->__tokens[$count + 1]);
  309. if(!is_array($countToken)) {
  310. $count++;
  311. continue;
  312. }
  313. list($type, $string, $line) = $countToken;
  314. if (($type == T_STRING) && ($string == $functionname) && ($firstParenthesis == "(")) {
  315. $position = $count;
  316. $depth = 0;
  317. while($depth == 0) {
  318. if ($this->__tokens[$position] == "(") {
  319. $depth++;
  320. } elseif($this->__tokens[$position] == ")") {
  321. $depth--;
  322. }
  323. $position++;
  324. }
  325. if($plural) {
  326. $end = $position + $shift + 7;
  327. if($this->__tokens[$position + $shift + 5] === ')') {
  328. $end = $position + $shift + 5;
  329. }
  330. if(empty($shift)) {
  331. list($singular, $firstComma, $plural, $seoncdComma, $endParenthesis) = array($this->__tokens[$position], $this->__tokens[$position + 1], $this->__tokens[$position + 2], $this->__tokens[$position + 3], $this->__tokens[$end]);
  332. $condition = ($seoncdComma == ",");
  333. } else {
  334. list($domain, $firstComma, $singular, $seoncdComma, $plural, $comma3, $endParenthesis) = array($this->__tokens[$position], $this->__tokens[$position + 1], $this->__tokens[$position + 2], $this->__tokens[$position + 3], $this->__tokens[$position + 4], $this->__tokens[$position + 5], $this->__tokens[$end]);
  335. $condition = ($comma3 == ",");
  336. }
  337. $condition = $condition &&
  338. (is_array($singular) && ($singular[0] == T_CONSTANT_ENCAPSED_STRING)) &&
  339. (is_array($plural) && ($plural[0] == T_CONSTANT_ENCAPSED_STRING));
  340. } else {
  341. if($this->__tokens[$position + $shift + 5] === ')') {
  342. $comma = $this->__tokens[$position + $shift + 3];
  343. $end = $position + $shift + 5;
  344. } else {
  345. $comma = null;
  346. $end = $position + $shift + 3;
  347. }
  348. list($domain, $firstComma, $text, $seoncdComma, $endParenthesis) = array($this->__tokens[$position], $this->__tokens[$position + 1], $this->__tokens[$position + 2], $comma, $this->__tokens[$end]);
  349. $condition = ($seoncdComma == "," || $seoncdComma === null) &&
  350. (is_array($domain) && ($domain[0] == T_CONSTANT_ENCAPSED_STRING)) &&
  351. (is_array($text) && ($text[0] == T_CONSTANT_ENCAPSED_STRING));
  352. }
  353. if(($endParenthesis == ")") && $condition) {
  354. if($this->__oneFile === true) {
  355. if($plural) {
  356. $this->__strings[$this->__formatString($singular[1]) . "\0" . $this->__formatString($plural[1])][$this->__file][] = $line;
  357. } else {
  358. $this->__strings[$this->__formatString($text[1])][$this->__file][] = $line;
  359. }
  360. } else {
  361. if($plural) {
  362. $this->__strings[$this->__file][$this->__formatString($singular[1]) . "\0" . $this->__formatString($plural[1])][] = $line;
  363. } else {
  364. $this->__strings[$this->__file][$this->__formatString($text[1])][] = $line;
  365. }
  366. }
  367. } else {
  368. $this->__markerError($this->__file, $line, $functionname, $count);
  369. }
  370. }
  371. $count++;
  372. }
  373. }
  374. function __buildFiles() {
  375. $output = '';
  376. foreach($this->__strings as $str => $fileInfo) {
  377. $occured = $fileList = array();
  378. if($this->__oneFile === true) {
  379. foreach($fileInfo as $file => $lines) {
  380. $occured[] = "$file:" . join(";", $lines);
  381. if(isset($this->__fileVersions[$file])) {
  382. $fileList[] = $this->__fileVersions[$file];
  383. }
  384. }
  385. $occurances = join("\n#: ", $occured);
  386. $occurances = str_replace($this->path, '', $occurances);
  387. $output = "#: $occurances\n";
  388. $filename = $this->__filename;
  389. if(strpos($str, "\0") === false) {
  390. $output .= "msgid \"$str\"\n";
  391. $output .= "msgstr \"\"\n";
  392. } else {
  393. list($singular, $plural) = explode("\0", $str);
  394. $output .= "msgid \"$singular\"\n";
  395. $output .= "msgid_plural \"$plural\"\n";
  396. $output .= "msgstr[0] \"\"\n";
  397. $output .= "msgstr[1] \"\"\n";
  398. }
  399. $output .= "\n";
  400. } else {
  401. foreach($fileInfo as $file => $lines) {
  402. $filename = $str;
  403. $occured = array("$str:" . join(";", $lines));
  404. if(isset($this->__fileVersions[$str])) {
  405. $fileList[] = $this->__fileVersions[$str];
  406. }
  407. $occurances = join("\n#: ", $occured);
  408. $occurances = str_replace($this->path, '', $occurances);
  409. $output .= "#: $occurances\n";
  410. if(strpos($file, "\0") === false) {
  411. $output .= "msgid \"$file\"\n";
  412. $output .= "msgstr \"\"\n";
  413. } else {
  414. list($singular, $plural) = explode("\0", $file);
  415. $output .= "msgid \"$singular\"\n";
  416. $output .= "msgid_plural \"$plural\"\n";
  417. $output .= "msgstr[0] \"\"\n";
  418. $output .= "msgstr[1] \"\"\n";
  419. }
  420. $output .= "\n";
  421. }
  422. }
  423. $this->__store($filename, $output, $fileList);
  424. }
  425. }
  426. function __store($file = 0, $input = 0, $fileList = array(), $get = 0) {
  427. static $storage = array();
  428. if(!$get) {
  429. if(isset($storage[$file])) {
  430. $storage[$file][1] = array_unique(array_merge($storage[$file][1], $fileList));
  431. $storage[$file][] = $input;
  432. } else {
  433. $storage[$file] = array();
  434. $storage[$file][0] = $this->__writeHeader();
  435. $storage[$file][1] = $fileList;
  436. $storage[$file][2] = $input;
  437. }
  438. } else {
  439. return $storage;
  440. }
  441. }
  442. function __writeFiles() {
  443. $output = $this->__store(0, 0, array(), 1);
  444. $output = $this->__mergeFiles($output);
  445. foreach($output as $file => $content) {
  446. $tmp = str_replace(array($this->path, '.php','.ctp','.thtml', '.inc','.tpl' ), '', $file);
  447. $tmp = str_replace(DS, '.', $tmp);
  448. $file = str_replace('.', '-', $tmp) .'.pot';
  449. $fileList = $content[1];
  450. unset($content[1]);
  451. $fileList = str_replace(array($this->path), '', $fileList);
  452. if(count($fileList) > 1) {
  453. $fileList = "Generated from files:\n# " . join("\n# ", $fileList);
  454. } elseif(count($fileList) == 1) {
  455. $fileList = "Generated from file: " . join("", $fileList);
  456. } else {
  457. $fileList = "No version information was available in the source files.";
  458. }
  459. $fp = fopen($this->__output . $file, 'w');
  460. fwrite($fp, str_replace("--VERSIONS--", $fileList, join("", $content)));
  461. fclose($fp);
  462. }
  463. }
  464. function __mergeFiles($output){
  465. foreach($output as $file => $content) {
  466. if(count($content) <= 1 && $file != $this->__filename) {
  467. @$output[$this->__filename][1] = array_unique(array_merge($output[$this->__filename][1], $content[1]));
  468. if(!isset($output[$this->__filename][0])) {
  469. $output[$this->__filename][0] = $content[0];
  470. }
  471. unset($content[0]);
  472. unset($content[1]);
  473. foreach($content as $msgid) {
  474. $output[$this->__filename][] = $msgid;
  475. }
  476. unset($output[$file]);
  477. }
  478. }
  479. return $output;
  480. }
  481. function __writeHeader() {
  482. $output = "# LANGUAGE translation of CakePHP Application\n";
  483. $output .= "# Copyright YEAR NAME <EMAIL@ADDRESS>\n";
  484. $output .= "# --VERSIONS--\n";
  485. $output .= "#\n";
  486. $output .= "#, fuzzy\n";
  487. $output .= "msgid \"\"\n";
  488. $output .= "msgstr \"\"\n";
  489. $output .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n";
  490. $output .= "\"POT-Creation-Date: " . date("Y-m-d H:iO") . "\\n\"\n";
  491. $output .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n";
  492. $output .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n";
  493. $output .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n";
  494. $output .= "\"MIME-Version: 1.0\\n\"\n";
  495. $output .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n";
  496. $output .= "\"Content-Transfer-Encoding: 8bit\\n\"\n";
  497. $output .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n\n";
  498. return $output;
  499. }
  500. function __findVersion($code, $file) {
  501. $header = '$Id' . ':';
  502. if (preg_match('/\\' . $header . '[\\w.]* ([\\d]*)/', $code, $versionInfo)) {
  503. $version = str_replace(ROOT, '', 'Revision: ' . $versionInfo[1] . ' ' .$file);
  504. $this->__fileVersions[$file] = $version;
  505. }
  506. }
  507. function __formatString($string) {
  508. $quote = substr($str, 0, 1);
  509. $string = substr($string, 1, -1);
  510. if($quote == '"') {
  511. $string = stripcslashes($string);
  512. } else {
  513. $string = strtr($string, array("\\'" => "'", "\\\\" => "\\"));
  514. }
  515. return addcslashes($string, "\0..\37\\\"");
  516. }
  517. function __markerError($file, $line, $marker, $count) {
  518. $this->__stdout("Invalid marker content in $file:$line\n* $marker(", true);
  519. $count += 2;
  520. $tokenCount = count($this->__tokens);
  521. $parenthesis = 1;
  522. while((($tokenCount - $count) > 0) && $parenthesis) {
  523. if(is_array($this->__tokens[$count])) {
  524. $this->__stdout($this->__tokens[$count][1], false);
  525. } else {
  526. $this->__stdout($this->__tokens[$count], false);
  527. if($this->__tokens[$count] == "(") {
  528. $parenthesis++;
  529. }
  530. if($this->__tokens[$count] == ")") {
  531. $parenthesis--;
  532. }
  533. }
  534. $count++;
  535. }
  536. $this->__stdout("\n", true);
  537. }
  538. function __searchDirectory($path = null) {
  539. if($path === null){
  540. $path = $this->path;
  541. }
  542. $files = glob("$path*.{php,ctp,thtml,inc,tpl}", GLOB_BRACE);
  543. $dirs = glob("$path*", GLOB_ONLYDIR);
  544. foreach($dirs as $dir) {
  545. if(!preg_match("!(^|.+/)(CVS|.svn)$!", $dir)) {
  546. $files = array_merge($files, $this->__searchDirectory("$dir/"));
  547. if(($id = array_search($dir .DS . 'extract.php', $files)) !== FALSE) {
  548. unset($files[$id]);
  549. }
  550. }
  551. }
  552. return $files;
  553. }
  554. function __getInput($prompt, $options = null, $default = null) {
  555. if(!is_array($options)) {
  556. $printOptions = '';
  557. } else {
  558. $printOptions = '(' . implode('/', $options) . ')';
  559. }
  560. if($default == null) {
  561. $this->__stdout('');
  562. $this->__stdout($prompt . " $printOptions \n" . '> ', false);
  563. } else {
  564. $this->__stdout('');
  565. $this->__stdout($prompt . " $printOptions \n" . "[$default] > ", false);
  566. }
  567. $result = trim(fgets($this->stdin));
  568. if($default != null && empty($result)) {
  569. return $default;
  570. } else {
  571. return $result;
  572. }
  573. }
  574. function __stdout($string, $newline = true) {
  575. if($newline) {
  576. fwrite($this->stdout, $string . "\n");
  577. } else {
  578. fwrite($this->stdout, $string);
  579. }
  580. }
  581. function __stderr($string) {
  582. fwrite($this->stderr, $string, true);
  583. }
  584. function __hr() {
  585. $this->__stdout('---------------------------------------------------------------');
  586. }
  587. }
  588. ?>