2010-12-05 05:48:38 +00:00
< ? php
/**
* A shell class to help developers upgrade applications to CakePHP 2.0
*
* @ package cake . console / shells
*/
class UpgradeShell extends Shell {
protected $_files = array ();
protected $_paths = array ();
2010-12-11 15:13:21 +00:00
/**
* Update helpers .
*
* - Converts helpers usage to new format .
*
* @ return void
*/
function helpers () {
2010-12-11 15:13:59 +00:00
$this -> _paths = array (
VIEWS
);
2010-12-11 15:13:21 +00:00
$patterns = array ();
foreach ( App :: objects ( 'helper' ) as $helper ) {
$oldHelper = strtolower ( substr ( $helper , 0 , 1 )) . substr ( $helper , 1 );
$patterns [] = array (
" \$ { $oldHelper } to \$ this-> { $helper } " ,
" / \\ \$ { $oldHelper } ->/ " ,
" \\ \$ this-> { $helper } -> "
);
}
2010-12-11 15:13:59 +00:00
$this -> _filesRegexpUpdate ( $patterns );
2010-12-11 15:13:21 +00:00
}
2010-12-05 05:48:38 +00:00
/**
* Update i18n .
*
* - Removes extra true param .
*
* @ return void
*/
function i18n () {
2010-12-11 15:13:59 +00:00
$this -> _paths = array (
CONTROLLERS ,
MODELS ,
VIEWS
);
2010-12-11 15:13:21 +00:00
$patterns = array (
array (
'<?php __*(*) to <?php echo __*(*)' ,
'/<\?php\s*(__[a-z]*\(.*?\))/' ,
'<?php echo \1'
),
array (
'<?php __*(*, true) to <?php echo __*()' ,
'/<\?php\s*(__[a-z]*\(.*?)(,\s*true)(\))/' ,
'<?php echo \1\3'
),
array ( '__*(*, true) to __*(*)' , '/(__[a-z]*\(.*?)(,\s*true)(\))/' , '\1\3' )
);
2010-12-11 15:13:59 +00:00
$this -> _filesRegexpUpdate ( $patterns );
}
protected function _filesRegexpUpdate ( $patterns ) {
if ( ! empty ( $this -> params [ 'plugin' ])) {
$this -> _paths = array ( App :: pluginPath ( $this -> params [ 'plugin' ]));
}
2010-12-11 15:14:54 +00:00
$extensions = 'php|ctp|thtml|inc|tpl' ;
if ( ! empty ( $this -> params [ 'ext' ])) {
$extensions = $this -> params [ 'ext' ];
}
$this -> _findFiles ( $extensions );
2010-12-05 05:48:38 +00:00
foreach ( $this -> _files as $file ) {
$this -> out ( 'Updating ' . $file . '...' , 1 , Shell :: VERBOSE );
2010-12-11 15:13:21 +00:00
$this -> _updateFile ( $file , $patterns );
2010-12-05 05:48:38 +00:00
}
}
2010-12-11 15:14:54 +00:00
protected function _findFiles ( $extensions = '' , $pattern = '' ) {
2010-12-05 05:48:38 +00:00
foreach ( $this -> _paths as $path ) {
$Folder = new Folder ( $path );
2010-12-11 15:14:54 +00:00
$files = $Folder -> findRecursive ( " .* \ .( $extensions ) " , true );
2010-12-05 05:48:38 +00:00
if ( ! empty ( $pattern )) {
foreach ( $files as $i => $file ) {
if ( preg_match ( $pattern , $file )) {
unset ( $files [ $i ]);
}
}
$files = array_values ( $files );
}
$this -> _files = array_merge ( $this -> _files , $files );
}
}
/**
* Update a single file .
*
* @ return void
*/
2010-12-11 15:13:21 +00:00
protected function _updateFile ( $file , $patterns ) {
2010-12-05 05:48:38 +00:00
$contents = file_get_contents ( $file );
2010-12-11 15:13:21 +00:00
2010-12-05 05:48:38 +00:00
foreach ( $patterns as $pattern ) {
$this -> out ( ' * Updating ' . $pattern [ 0 ], 1 , Shell :: VERBOSE );
$contents = preg_replace ( $pattern [ 1 ], $pattern [ 2 ], $contents );
}
$this -> out ( 'Done updating ' . $file , 1 );
file_put_contents ( $file , $contents );
}
/**
* get the option parser
*
* @ return void
*/
function getOptionParser () {
2010-12-11 15:14:54 +00:00
$subcommandParser = array (
'options' => array (
'plugin' => array ( 'short' => 'p' , 'help' => __ ( 'The plugin to update.' )),
'ext' => array ( 'short' => 'e' , 'help' => __ ( 'The extension(s) to search.' )),
)
);
2010-12-05 05:48:38 +00:00
return parent :: getOptionParser ()
2010-12-11 16:00:09 +00:00
-> description ( " A shell to help automate upgrading from CakePHP 1.3 to 2.0. \n Be sure to have a backup of your application before running these commands. " )
2010-12-05 05:48:38 +00:00
-> addSubcommand ( 'i18n' , array (
'help' => 'Update the i18n translation method calls.' ,
2010-12-11 15:14:54 +00:00
'parser' => $subcommandParser
2010-12-11 15:13:21 +00:00
))
-> addSubcommand ( 'helpers' , array (
'help' => 'Update calls to helpers.' ,
2010-12-11 15:14:54 +00:00
'parser' => $subcommandParser
2010-12-05 05:48:38 +00:00
));
}
}