2008-05-30 11:40:08 +00:00
< ? php
/**
2009-09-15 03:43:59 +00:00
* Language string extractor
2008-05-30 11:40:08 +00:00
*
2010-10-03 16:38:58 +00:00
* PHP 5
2008-05-30 11:40:08 +00:00
*
2009-11-06 06:46:59 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2010-01-26 19:18:20 +00:00
* Copyright 2005 - 2010 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
2010-01-26 19:18:20 +00:00
* @ copyright Copyright 2005 - 2010 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2009-11-06 06:00:11 +00:00
* @ link http :// cakephp . org CakePHP ( tm ) Project
2010-12-24 19:26:26 +00:00
* @ package cake . console . shells . tasks
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2 . 0.5012
2009-11-06 06:51:51 +00:00
* @ license MIT License ( http :// www . opensource . org / licenses / mit - license . php )
2008-05-30 11:40:08 +00:00
*/
2010-12-08 00:20:56 +00:00
App :: uses ( 'File' , 'Utility' );
2011-04-17 16:03:43 +00:00
App :: uses ( 'Folder' , 'Utility' );
2008-05-30 11:40:08 +00:00
/**
* Language string extractor
*
2010-12-24 19:26:26 +00:00
* @ package cake . console . shells . tasks
2008-05-30 11:40:08 +00:00
*/
2009-09-15 03:43:59 +00:00
class ExtractTask extends Shell {
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-09-15 03:43:59 +00:00
* Paths to use when looking for strings
2008-05-30 11:40:08 +00:00
*
* @ var string
2009-09-15 03:43:59 +00:00
* @ access private
2008-05-30 11:40:08 +00:00
*/
2010-04-04 06:33:39 +00:00
private $__paths = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Files from where to extract
*
* @ var array
* @ access private
*/
2010-04-04 06:33:39 +00:00
private $__files = array ();
2009-07-24 19:18:37 +00:00
2009-10-29 23:26:59 +00:00
/**
* Merge all domains string into the default . pot file
*
* @ var boolean
2009-11-02 23:54:03 +00:00
* @ access private
2009-10-29 23:26:59 +00:00
*/
2010-04-04 06:33:39 +00:00
private $__merge = false ;
2009-10-29 23:26:59 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-09-15 03:43:59 +00:00
* Current file being processed
2008-05-30 11:40:08 +00:00
*
2009-09-15 03:43:59 +00:00
* @ var string
2008-05-30 11:40:08 +00:00
* @ access private
*/
2010-04-04 06:33:39 +00:00
private $__file = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-09-15 03:43:59 +00:00
* Contains all content waiting to be write
2008-05-30 11:40:08 +00:00
*
* @ var string
* @ access private
*/
2010-04-04 06:33:39 +00:00
private $__storage = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Extracted tokens
*
* @ var array
* @ access private
*/
2010-04-04 06:33:39 +00:00
private $__tokens = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Extracted strings
*
* @ var array
* @ access private
*/
2010-04-04 06:33:39 +00:00
private $__strings = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Destination path
*
* @ var string
* @ access private
*/
2010-04-04 06:33:39 +00:00
private $__output = null ;
2009-07-24 19:18:37 +00:00
2010-10-29 04:40:04 +00:00
/**
* An array of directories to exclude .
*
* @ var array
*/
protected $_exclude = array ();
2008-05-30 11:40:08 +00:00
/**
* Execution method always used for tasks
*
2009-09-15 03:43:59 +00:00
* @ return void
* @ access private
2008-05-30 11:40:08 +00:00
*/
function execute () {
2010-10-29 04:40:04 +00:00
if ( ! empty ( $this -> params [ 'exclude' ])) {
$this -> _exclude = explode ( ',' , $this -> params [ 'exclude' ]);
}
2008-05-30 11:40:08 +00:00
if ( isset ( $this -> params [ 'files' ]) && ! is_array ( $this -> params [ 'files' ])) {
2009-09-15 03:43:59 +00:00
$this -> __files = explode ( ',' , $this -> params [ 'files' ]);
2008-05-30 11:40:08 +00:00
}
2009-09-15 03:43:59 +00:00
if ( isset ( $this -> params [ 'paths' ])) {
$this -> __paths = explode ( ',' , $this -> params [ 'paths' ]);
2008-05-30 11:40:08 +00:00
} else {
2010-10-23 04:11:31 +00:00
$defaultPath = APP_PATH ;
2011-04-17 16:17:25 +00:00
$message = __d ( 'cake_console' , " What is the path you would like to extract? \n [Q]uit [D]one " );
2009-09-15 03:43:59 +00:00
while ( true ) {
$response = $this -> in ( $message , null , $defaultPath );
2008-05-30 11:40:08 +00:00
if ( strtoupper ( $response ) === 'Q' ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Extract Aborted' ));
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2009-09-15 03:43:59 +00:00
} elseif ( strtoupper ( $response ) === 'D' ) {
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-09-15 03:43:59 +00:00
break ;
} elseif ( is_dir ( $response )) {
$this -> __paths [] = $response ;
$defaultPath = 'D' ;
} else {
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , 'The directory path you supplied was not found. Please try again.' ));
2008-05-30 11:40:08 +00:00
}
2009-09-26 21:08:37 +00:00
$this -> out ();
2008-05-30 11:40:08 +00:00
}
}
if ( isset ( $this -> params [ 'output' ])) {
$this -> __output = $this -> params [ 'output' ];
} else {
2011-04-17 16:17:25 +00:00
$message = __d ( 'cake_console' , " What is the path you would like to output? \n [Q]uit " , $this -> __paths [ 0 ] . DS . 'locale' );
2009-09-15 03:43:59 +00:00
while ( true ) {
$response = $this -> in ( $message , null , $this -> __paths [ 0 ] . DS . 'locale' );
2008-05-30 11:40:08 +00:00
if ( strtoupper ( $response ) === 'Q' ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Extract Aborted' ));
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2009-09-15 03:43:59 +00:00
} elseif ( is_dir ( $response )) {
$this -> __output = $response . DS ;
break ;
} else {
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , 'The directory path you supplied was not found. Please try again.' ));
2008-05-30 11:40:08 +00:00
}
2009-09-26 21:08:37 +00:00
$this -> out ();
2008-05-30 11:40:08 +00:00
}
}
2009-10-29 23:26:59 +00:00
if ( isset ( $this -> params [ 'merge' ])) {
$this -> __merge = ! ( strtolower ( $this -> params [ 'merge' ]) === 'no' );
} else {
$this -> out ();
2011-03-19 17:32:35 +00:00
$response = $this -> in ( __d ( 'cake_console' , 'Would you like to merge all domains strings into the default.pot file?' ), array ( 'y' , 'n' ), 'n' );
2009-10-29 23:26:59 +00:00
$this -> __merge = strtolower ( $response ) === 'y' ;
}
2009-09-15 03:43:59 +00:00
if ( empty ( $this -> __files )) {
$this -> __searchFiles ();
2008-05-30 11:40:08 +00:00
}
$this -> __extract ();
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Extract text
*
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access private
*/
function __extract () {
2009-09-26 21:08:37 +00:00
$this -> out ();
$this -> out ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Extracting...' ));
2008-05-30 11:40:08 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Paths:' ));
2009-09-15 03:43:59 +00:00
foreach ( $this -> __paths as $path ) {
$this -> out ( ' ' . $path );
}
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Output Directory: ' ) . $this -> __output );
2008-05-30 11:40:08 +00:00
$this -> hr ();
$this -> __extractTokens ();
2010-02-12 13:20:43 +00:00
$this -> __buildFiles ();
$this -> __writeFiles ();
$this -> __paths = $this -> __files = $this -> __storage = array ();
$this -> __strings = $this -> __tokens = array ();
$this -> out ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Done.' ));
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2010-10-10 05:56:23 +00:00
/**
* Get & configure the option parser
*
* @ return void
*/
public function getOptionParser () {
$parser = parent :: getOptionParser ();
2011-03-19 17:32:35 +00:00
return $parser -> description ( __d ( 'cake_console' , 'CakePHP Language String Extraction:' ))
-> addOption ( 'app' , array ( 'help' => __d ( 'cake_console' , 'Directory where your application is located.' )))
2011-04-17 16:08:10 +00:00
-> addOption ( 'paths' , array ( 'help' => __d ( 'cake_console' , 'Comma separted list of paths.' )))
2010-10-24 19:27:44 +00:00
-> addOption ( 'merge' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Merge all domain strings into the default.po file.' ),
2010-10-24 19:27:44 +00:00
'choices' => array ( 'yes' , 'no' )
))
2011-03-19 17:32:35 +00:00
-> addOption ( 'output' , array ( 'help' => __d ( 'cake_console' , 'Full path to output directory.' )))
2011-04-17 16:08:10 +00:00
-> addOption ( 'files' , array ( 'help' => __d ( 'cake_console' , 'Comma separated list of files.' )))
2010-10-29 04:40:04 +00:00
-> addOption ( 'exclude' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Comma separated list of directories to exclude. Any path containing a path segment with the provided values will be skipped. E.g. test,vendors' )
2010-10-29 04:40:04 +00:00
));
2010-10-10 05:56:23 +00:00
}
2008-05-30 11:40:08 +00:00
/**
* Show help options
*
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function help () {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'CakePHP Language String Extraction:' ));
2008-05-30 11:40:08 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'The Extract script generates .pot file(s) with translations' ));
$this -> out ( __d ( 'cake_console' , 'By default the .pot file(s) will be place in the locale directory of -app' ));
$this -> out ( __d ( 'cake_console' , 'By default -app is ROOT/app' ));
2008-05-30 11:40:08 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Usage: cake i18n extract <command> <param1> <param2>...' ));
2009-09-26 21:08:37 +00:00
$this -> out ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Params:' ));
$this -> out ( __d ( 'cake_console' , ' -app [path...]: directory where your application is located' ));
$this -> out ( __d ( 'cake_console' , ' -root [path...]: path to install' ));
$this -> out ( __d ( 'cake_console' , ' -core [path...]: path to cake directory' ));
2011-04-17 16:08:10 +00:00
$this -> out ( __d ( 'cake_console' , ' -paths [comma separated list of paths]' ));
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , ' -merge [yes|no]: Merge all domains strings into the default.pot file' ));
$this -> out ( __d ( 'cake_console' , ' -output [path...]: Full path to output directory' ));
2011-04-17 16:08:10 +00:00
$this -> out ( __d ( 'cake_console' , ' -files: [comma separated list of files]' ));
2009-10-29 23:26:59 +00:00
$this -> out ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Commands:' ));
$this -> out ( __d ( 'cake_console' , ' cake i18n extract help: Shows this help message.' ));
2009-09-26 21:08:37 +00:00
$this -> out ();
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Extract tokens out of all files to be processed
*
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access private
*/
function __extractTokens () {
2009-09-15 03:43:59 +00:00
foreach ( $this -> __files as $file ) {
2008-05-30 11:40:08 +00:00
$this -> __file = $file ;
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Processing %s...' , $file ));
2008-05-30 11:40:08 +00:00
$code = file_get_contents ( $file );
$allTokens = token_get_all ( $code );
$this -> __tokens = array ();
$lineNumber = 1 ;
foreach ( $allTokens as $token ) {
if (( ! is_array ( $token )) || (( $token [ 0 ] != T_WHITESPACE ) && ( $token [ 0 ] != T_INLINE_HTML ))) {
if ( is_array ( $token )) {
$token [] = $lineNumber ;
}
$this -> __tokens [] = $token ;
}
if ( is_array ( $token )) {
2009-11-19 22:17:47 +00:00
$lineNumber += count ( explode ( " \n " , $token [ 1 ])) - 1 ;
2008-05-30 11:40:08 +00:00
} else {
2009-11-19 22:17:47 +00:00
$lineNumber += count ( explode ( " \n " , $token )) - 1 ;
2008-05-30 11:40:08 +00:00
}
}
unset ( $allTokens );
2009-08-06 21:14:25 +00:00
$this -> __parse ( '__' , array ( 'singular' ));
$this -> __parse ( '__n' , array ( 'singular' , 'plural' ));
$this -> __parse ( '__d' , array ( 'domain' , 'singular' ));
$this -> __parse ( '__c' , array ( 'singular' ));
$this -> __parse ( '__dc' , array ( 'domain' , 'singular' ));
$this -> __parse ( '__dn' , array ( 'domain' , 'singular' , 'plural' ));
$this -> __parse ( '__dcn' , array ( 'domain' , 'singular' , 'plural' ));
2008-05-30 11:40:08 +00:00
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-08-06 21:14:25 +00:00
* Parse tokens
2008-05-30 11:40:08 +00:00
*
* @ param string $functionName Function name that indicates translatable string ( e . g : '__' )
2009-08-06 21:14:25 +00:00
* @ param array $map Array containing what variables it will find ( e . g : domain , singular , plural )
2009-09-15 03:43:59 +00:00
* @ return void
* @ access private
2008-05-30 11:40:08 +00:00
*/
2009-08-06 21:14:25 +00:00
function __parse ( $functionName , $map ) {
2008-05-30 11:40:08 +00:00
$count = 0 ;
$tokenCount = count ( $this -> __tokens );
2010-02-12 13:52:58 +00:00
while (( $tokenCount - $count ) > 1 ) {
2008-05-30 11:40:08 +00:00
list ( $countToken , $firstParenthesis ) = array ( $this -> __tokens [ $count ], $this -> __tokens [ $count + 1 ]);
if ( ! is_array ( $countToken )) {
$count ++ ;
continue ;
}
list ( $type , $string , $line ) = $countToken ;
if (( $type == T_STRING ) && ( $string == $functionName ) && ( $firstParenthesis == '(' )) {
$position = $count ;
$depth = 0 ;
while ( $depth == 0 ) {
if ( $this -> __tokens [ $position ] == '(' ) {
$depth ++ ;
} elseif ( $this -> __tokens [ $position ] == ')' ) {
$depth -- ;
}
$position ++ ;
}
2009-08-06 21:14:25 +00:00
$mapCount = count ( $map );
2011-04-25 17:22:14 +00:00
$strings = $this -> __getStrings ( $position , $mapCount );
2008-05-30 11:40:08 +00:00
2009-08-06 21:14:25 +00:00
if ( $mapCount == count ( $strings )) {
extract ( array_combine ( $map , $strings ));
2011-04-25 17:22:14 +00:00
$domain = isset ( $domain ) ? $domain : 'default' ;
$string = isset ( $plural ) ? $singular . " \0 " . $plural : $singular ;
$this -> __strings [ $domain ][ $string ][ $this -> __file ][] = $line ;
2008-05-30 11:40:08 +00:00
} else {
$this -> __markerError ( $this -> __file , $line , $functionName , $count );
}
}
$count ++ ;
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Build the translate template file contents out of obtained strings
*
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access private
*/
function __buildFiles () {
2009-09-15 03:43:59 +00:00
foreach ( $this -> __strings as $domain => $strings ) {
foreach ( $strings as $string => $files ) {
2010-02-18 00:41:15 +00:00
$occurrences = array ();
2009-09-15 03:43:59 +00:00
foreach ( $files as $file => $lines ) {
2010-02-18 00:41:15 +00:00
$occurrences [] = $file . ':' . implode ( ';' , $lines );
2008-05-30 11:40:08 +00:00
}
2010-02-18 00:41:15 +00:00
$occurrences = implode ( " \n #: " , $occurrences );
$header = '#: ' . str_replace ( $this -> __paths , '' , $occurrences ) . " \n " ;
2009-09-15 03:43:59 +00:00
if ( strpos ( $string , " \0 " ) === false ) {
$sentence = " msgid \" { $string } \" \n " ;
$sentence .= " msgstr \" \" \n \n " ;
2008-05-30 11:40:08 +00:00
} else {
2009-09-15 03:43:59 +00:00
list ( $singular , $plural ) = explode ( " \0 " , $string );
$sentence = " msgid \" { $singular } \" \n " ;
$sentence .= " msgid_plural \" { $plural } \" \n " ;
$sentence .= " msgstr[0] \" \" \n " ;
$sentence .= " msgstr[1] \" \" \n \n " ;
2008-05-30 11:40:08 +00:00
}
2009-09-15 03:43:59 +00:00
$this -> __store ( $domain , $header , $sentence );
2009-10-29 23:26:59 +00:00
if ( $domain != 'default' && $this -> __merge ) {
2009-09-15 03:43:59 +00:00
$this -> __store ( 'default' , $header , $sentence );
2008-05-30 11:40:08 +00:00
}
}
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Prepare a file to be stored
*
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access private
*/
2009-09-15 03:43:59 +00:00
function __store ( $domain , $header , $sentence ) {
if ( ! isset ( $this -> __storage [ $domain ])) {
$this -> __storage [ $domain ] = array ();
}
if ( ! isset ( $this -> __storage [ $domain ][ $sentence ])) {
$this -> __storage [ $domain ][ $sentence ] = $header ;
2008-05-30 11:40:08 +00:00
} else {
2009-09-15 03:43:59 +00:00
$this -> __storage [ $domain ][ $sentence ] .= $header ;
2008-05-30 11:40:08 +00:00
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Write the files that need to be stored
*
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access private
*/
function __writeFiles () {
2010-02-18 00:41:15 +00:00
$overwriteAll = false ;
2009-09-15 03:43:59 +00:00
foreach ( $this -> __storage as $domain => $sentences ) {
$output = $this -> __writeHeader ();
foreach ( $sentences as $sentence => $header ) {
$output .= $header . $sentence ;
2008-05-30 11:40:08 +00:00
}
2009-09-15 03:43:59 +00:00
$filename = $domain . '.pot' ;
$File = new File ( $this -> __output . $filename );
2010-02-18 00:41:15 +00:00
$response = '' ;
while ( $overwriteAll === false && $File -> exists () && strtoupper ( $response ) !== 'Y' ) {
$this -> out ();
2011-03-19 17:32:35 +00:00
$response = $this -> in ( __d ( 'cake_console' , 'Error: %s already exists in this location. Overwrite? [Y]es, [N]o, [A]ll' , $filename ), array ( 'y' , 'n' , 'a' ), 'y' );
2010-02-18 00:41:15 +00:00
if ( strtoupper ( $response ) === 'N' ) {
$response = '' ;
while ( $response == '' ) {
2011-04-17 16:17:25 +00:00
$response = $this -> in ( __d ( 'cake_console' , " What would you like to name this file? " ), null , 'new_' . $filename );
2010-02-18 00:41:15 +00:00
$File = new File ( $this -> __output . $response );
$filename = $response ;
2008-05-30 11:40:08 +00:00
}
2010-02-18 00:41:15 +00:00
} elseif ( strtoupper ( $response ) === 'A' ) {
$overwriteAll = true ;
2008-05-30 11:40:08 +00:00
}
}
2009-09-15 03:43:59 +00:00
$File -> write ( $output );
$File -> close ();
2008-05-30 11:40:08 +00:00
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Build the translation template header
*
* @ return string Translation template header
* @ access private
*/
function __writeHeader () {
$output = " # LANGUAGE translation of CakePHP Application \n " ;
$output .= " # Copyright YEAR NAME <EMAIL@ADDRESS> \n " ;
$output .= " # \n " ;
$output .= " #, fuzzy \n " ;
$output .= " msgid \" \" \n " ;
$output .= " msgstr \" \" \n " ;
$output .= " \" Project-Id-Version: PROJECT VERSION \\ n \" \n " ;
$output .= " \" POT-Creation-Date: " . date ( " Y-m-d H:iO " ) . " \\ n \" \n " ;
$output .= " \" PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ \\ n \" \n " ;
$output .= " \" Last-Translator: NAME <EMAIL@ADDRESS> \\ n \" \n " ;
$output .= " \" Language-Team: LANGUAGE <EMAIL@ADDRESS> \\ n \" \n " ;
$output .= " \" MIME-Version: 1.0 \\ n \" \n " ;
$output .= " \" Content-Type: text/plain; charset=utf-8 \\ n \" \n " ;
$output .= " \" Content-Transfer-Encoding: 8bit \\ n \" \n " ;
$output .= " \" Plural-Forms: nplurals=INTEGER; plural=EXPRESSION; \\ n \" \n \n " ;
return $output ;
}
2011-04-25 17:22:14 +00:00
/**
* Get the strings from the position forward
*
* @ param int $position Actual position on tokens array
* @ param int $target Number of strings to extract
* @ return array Strings extracted
* @ access private
*/
function __getStrings ( & $position , $target ) {
$strings = array ();
while ( count ( $strings ) < $target && ( $this -> __tokens [ $position ] == ',' || $this -> __tokens [ $position ][ 0 ] == T_CONSTANT_ENCAPSED_STRING )) {
if ( $this -> __tokens [ $position ][ 0 ] == T_CONSTANT_ENCAPSED_STRING && $this -> __tokens [ $position + 1 ] == '.' ) {
$string = '' ;
while ( $this -> __tokens [ $position ][ 0 ] == T_CONSTANT_ENCAPSED_STRING || $this -> __tokens [ $position ] == '.' ) {
if ( $this -> __tokens [ $position ][ 0 ] == T_CONSTANT_ENCAPSED_STRING ) {
$string .= $this -> __formatString ( $this -> __tokens [ $position ][ 1 ]);
}
$position ++ ;
}
$strings [] = $string ;
} else if ( $this -> __tokens [ $position ][ 0 ] == T_CONSTANT_ENCAPSED_STRING ) {
$strings [] = $this -> __formatString ( $this -> __tokens [ $position ][ 1 ]);
}
$position ++ ;
}
return $strings ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Format a string to be added as a translateable string
*
* @ param string $string String to format
* @ return string Formatted string
* @ access private
*/
function __formatString ( $string ) {
$quote = substr ( $string , 0 , 1 );
$string = substr ( $string , 1 , - 1 );
if ( $quote == '"' ) {
$string = stripcslashes ( $string );
} else {
$string = strtr ( $string , array ( " \\ ' " => " ' " , " \\ \\ " => " \\ " ));
}
2008-11-06 22:57:50 +00:00
$string = str_replace ( " \r \n " , " \n " , $string );
2008-05-30 11:40:08 +00:00
return addcslashes ( $string , " \0 .. \37 \\ \" " );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Indicate an invalid marker on a processed file
*
* @ param string $file File where invalid marker resides
* @ param integer $line Line number
* @ param string $marker Marker found
* @ param integer $count Count
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access private
*/
function __markerError ( $file , $line , $marker , $count ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , " Invalid marker content in %s:%s \n * %s( " , $file , $line , $marker ), true );
2008-05-30 11:40:08 +00:00
$count += 2 ;
$tokenCount = count ( $this -> __tokens );
$parenthesis = 1 ;
while ((( $tokenCount - $count ) > 0 ) && $parenthesis ) {
if ( is_array ( $this -> __tokens [ $count ])) {
$this -> out ( $this -> __tokens [ $count ][ 1 ], false );
} else {
$this -> out ( $this -> __tokens [ $count ], false );
if ( $this -> __tokens [ $count ] == '(' ) {
$parenthesis ++ ;
}
if ( $this -> __tokens [ $count ] == ')' ) {
$parenthesis -- ;
}
}
$count ++ ;
}
$this -> out ( " \n " , true );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-09-15 03:43:59 +00:00
* Search files that may contain translateable strings
2008-05-30 11:40:08 +00:00
*
2009-09-15 03:43:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
* @ access private
*/
2009-09-15 03:43:59 +00:00
function __searchFiles () {
2010-10-29 04:40:04 +00:00
$pattern = false ;
if ( ! empty ( $this -> _exclude )) {
2011-04-17 16:03:43 +00:00
$pattern = '/[\/\\\\]' . implode ( '|' , $this -> _exclude ) . '[\/\\\\]/' ;
2010-10-29 04:40:04 +00:00
}
2009-09-15 03:43:59 +00:00
foreach ( $this -> __paths as $path ) {
$Folder = new Folder ( $path );
$files = $Folder -> findRecursive ( '.*\.(php|ctp|thtml|inc|tpl)' , true );
2010-10-29 04:40:04 +00:00
if ( ! empty ( $pattern )) {
foreach ( $files as $i => $file ) {
if ( preg_match ( $pattern , $file )) {
unset ( $files [ $i ]);
}
}
$files = array_values ( $files );
}
2010-06-07 02:39:04 +00:00
$this -> __files = array_merge ( $this -> __files , $files );
2008-05-30 11:40:08 +00:00
}
}
}