2010-12-05 05:48:38 +00:00
|
|
|
<?php
|
2011-07-26 01:46:52 +00:00
|
|
|
App::uses('Folder', 'Utility');
|
|
|
|
|
2010-12-05 05:48:38 +00:00
|
|
|
/**
|
|
|
|
* A shell class to help developers upgrade applications to CakePHP 2.0
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console.Command
|
2010-12-05 05:48:38 +00:00
|
|
|
*/
|
|
|
|
class UpgradeShell extends Shell {
|
|
|
|
|
|
|
|
protected $_files = array();
|
2011-05-08 21:55:16 +00:00
|
|
|
|
2010-12-05 05:48:38 +00:00
|
|
|
protected $_paths = array();
|
2010-12-11 15:13:21 +00:00
|
|
|
|
2011-05-08 21:55:16 +00:00
|
|
|
protected $_map = array(
|
|
|
|
'Controller' => 'Controller',
|
|
|
|
'Component' => 'Controller/Component',
|
|
|
|
'Model' => 'Model',
|
|
|
|
'Behavior' => 'Model/Behavior',
|
|
|
|
'Datasource' => 'Model/Datasource',
|
|
|
|
'Dbo' => 'Model/Datasource/Database',
|
|
|
|
'View' => 'View',
|
|
|
|
'Helper' => 'View/Helper',
|
|
|
|
'Shell' => 'Console/Command',
|
|
|
|
'Task' => 'Console/Command/Task',
|
|
|
|
'Case' => 'tests/Case',
|
|
|
|
'Fixture' => 'tests/Fixture',
|
2011-05-08 22:46:51 +00:00
|
|
|
'Error' => 'Lib/Error',
|
2011-05-08 21:55:16 +00:00
|
|
|
);
|
|
|
|
|
2011-01-02 03:13:45 +00:00
|
|
|
/**
|
|
|
|
* Shell startup, prints info message about dry run.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function startup() {
|
2011-01-02 03:13:45 +00:00
|
|
|
parent::startup();
|
|
|
|
if ($this->params['dry-run']) {
|
|
|
|
$this->out('<warning>Dry-run mode enabled!</warning>', 1, Shell::QUIET);
|
|
|
|
}
|
2011-05-08 21:55:16 +00:00
|
|
|
if ($this->params['git'] && !is_dir('.git')) {
|
|
|
|
$this->out('<warning>No git repository detected!</warning>', 1, Shell::QUIET);
|
|
|
|
}
|
2011-01-02 03:13:45 +00:00
|
|
|
}
|
2011-05-01 19:55:15 +00:00
|
|
|
|
2011-05-08 21:55:16 +00:00
|
|
|
/**
|
|
|
|
* Run all upgrade steps one at a time
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function all() {
|
2011-05-01 19:55:15 +00:00
|
|
|
foreach($this->OptionParser->subcommands() as $command) {
|
|
|
|
$name = $command->name();
|
|
|
|
if ($name === 'all') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->out('Running ' . $name);
|
|
|
|
$this->$name();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-08 21:55:16 +00:00
|
|
|
/**
|
|
|
|
* Move files and folders to their new homes
|
|
|
|
*
|
|
|
|
* Moves folders containing files which cannot necessarily be autodetected (libs and templates)
|
|
|
|
* and then looks for all php files except vendors, and moves them to where Cake 2.0 expects
|
|
|
|
* to find them.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function locations() {
|
2011-05-08 21:55:16 +00:00
|
|
|
$cwd = getcwd();
|
|
|
|
|
|
|
|
if (is_dir('plugins')) {
|
|
|
|
|
|
|
|
$Folder = new Folder('plugins');
|
|
|
|
list($plugins) = $Folder->read();
|
|
|
|
foreach($plugins as $plugin) {
|
|
|
|
chdir($cwd . DS . 'plugins' . DS . $plugin);
|
|
|
|
$this->locations();
|
|
|
|
}
|
|
|
|
$this->_files = array();
|
|
|
|
chdir($cwd);
|
|
|
|
}
|
|
|
|
|
2011-05-01 20:40:40 +00:00
|
|
|
$moves = array(
|
|
|
|
'libs' => 'Lib',
|
2011-05-31 03:02:39 +00:00
|
|
|
'vendors' . DS . 'shells' . DS . 'templates' => 'Console' . DS . 'Templates',
|
2011-05-01 20:40:40 +00:00
|
|
|
);
|
|
|
|
foreach($moves as $old => $new) {
|
|
|
|
if (is_dir($old)) {
|
|
|
|
$this->out("Moving $old to $new");
|
2011-05-08 21:55:16 +00:00
|
|
|
if (!$this->params['dry-run']) {
|
|
|
|
$Folder = new Folder($old);
|
|
|
|
$Folder->move($new);
|
|
|
|
}
|
|
|
|
if ($this->params['git']) {
|
|
|
|
exec('git mv -f ' . escapeshellarg($old) . ' ' . escapeshellarg($new));
|
|
|
|
}
|
2011-05-01 20:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
2011-05-08 21:55:16 +00:00
|
|
|
$sourceDirs = array(
|
|
|
|
'.' => array('recursive' => false),
|
|
|
|
'Console',
|
|
|
|
'Controller',
|
|
|
|
'controllers',
|
|
|
|
'Lib' => array('checkFolder' => false),
|
|
|
|
'Model',
|
|
|
|
'models',
|
|
|
|
'tests',
|
|
|
|
'View',
|
|
|
|
'views',
|
|
|
|
'vendors/shells',
|
|
|
|
);
|
2011-05-01 21:07:22 +00:00
|
|
|
|
2011-05-08 21:55:16 +00:00
|
|
|
$defaultOptions = array(
|
|
|
|
'recursive' => true,
|
|
|
|
'checkFolder' => true,
|
|
|
|
);
|
|
|
|
foreach($sourceDirs as $dir => $options) {
|
|
|
|
if (is_numeric($dir)) {
|
|
|
|
$dir = $options;
|
|
|
|
$options = array();
|
|
|
|
}
|
|
|
|
$options = array_merge($defaultOptions, $options);
|
|
|
|
$this->_movePhpFiles($dir, $options);
|
2011-05-01 21:07:22 +00:00
|
|
|
}
|
2011-05-01 20:40:40 +00:00
|
|
|
}
|
|
|
|
|
2010-12-11 15:13:21 +00:00
|
|
|
/**
|
|
|
|
* Update helpers.
|
|
|
|
*
|
|
|
|
* - Converts helpers usage to new format.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function helpers() {
|
2011-01-02 03:13:45 +00:00
|
|
|
$this->_paths = array_diff(App::path('views'), App::core('views'));
|
|
|
|
|
2010-12-12 04:13:21 +00:00
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
$this->_paths = array(App::pluginPath($this->params['plugin']) . 'views' . DS);
|
|
|
|
}
|
2010-12-11 15:13:21 +00:00
|
|
|
|
|
|
|
$patterns = array();
|
2010-12-12 04:19:07 +00:00
|
|
|
$helpers = App::objects('helper');
|
|
|
|
$plugins = App::objects('plugin');
|
|
|
|
$pluginHelpers = array();
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$pluginHelpers = array_merge(
|
|
|
|
$pluginHelpers,
|
|
|
|
App::objects('helper', App::pluginPath($plugin) . DS . 'views' . DS . 'helpers' . DS, false)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$helpers = array_merge($pluginHelpers, $helpers);
|
|
|
|
foreach ($helpers as $helper) {
|
2010-12-11 15:13:21 +00:00
|
|
|
$oldHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1);
|
|
|
|
$patterns[] = array(
|
|
|
|
"\${$oldHelper} to \$this->{$helper}",
|
|
|
|
"/\\\${$oldHelper}->/",
|
|
|
|
"\\\$this->{$helper}->"
|
|
|
|
);
|
|
|
|
}
|
2011-05-01 17:32:09 +00:00
|
|
|
|
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.
|
2010-12-12 04:13:21 +00:00
|
|
|
* - Add the echo to __*() calls that didn't need them before.
|
2010-12-05 05:48:38 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function i18n() {
|
2010-12-11 15:13:59 +00:00
|
|
|
$this->_paths = array(
|
2010-12-12 04:13:21 +00:00
|
|
|
APP
|
2010-12-11 15:13:59 +00:00
|
|
|
);
|
2010-12-12 04:13:21 +00:00
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
$this->_paths = array(App::pluginPath($this->params['plugin']));
|
|
|
|
}
|
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);
|
|
|
|
}
|
|
|
|
|
2010-12-12 04:38:23 +00:00
|
|
|
/**
|
|
|
|
* Upgrade the removed basics functions.
|
|
|
|
*
|
|
|
|
* - a(*) -> array(*)
|
|
|
|
* - e(*) -> echo *
|
|
|
|
* - ife(*, *, *) -> empty(*) ? * : *
|
|
|
|
* - a(*) -> array(*)
|
|
|
|
* - r(*, *, *) -> str_replace(*, *, *)
|
|
|
|
* - up(*) -> strtoupper(*)
|
|
|
|
* - low(*, *, *) -> strtolower(*)
|
|
|
|
* - getMicrotime() -> microtime(true)
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function basics() {
|
|
|
|
$this->_paths = array(
|
|
|
|
APP
|
|
|
|
);
|
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
$this->_paths = array(App::pluginPath($this->params['plugin']));
|
|
|
|
}
|
|
|
|
$patterns = array(
|
|
|
|
array(
|
|
|
|
'a(*) -> array(*)',
|
2010-12-19 14:06:45 +00:00
|
|
|
'/\ba\((.*)\)/',
|
2010-12-12 04:38:23 +00:00
|
|
|
'array(\1)'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'e(*) -> echo *',
|
|
|
|
'/\be\((.*)\)/',
|
|
|
|
'echo \1'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'ife(*, *, *) -> empty(*) ? * : *',
|
|
|
|
'/ife\((.*), (.*), (.*)\)/',
|
|
|
|
'empty(\1) ? \2 : \3'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'r(*, *, *) -> str_replace(*, *, *)',
|
|
|
|
'/\br\(/',
|
|
|
|
'str_replace('
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'up(*) -> strtoupper(*)',
|
|
|
|
'/\bup\(/',
|
|
|
|
'strtoupper('
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'low(*) -> strtolower(*)',
|
|
|
|
'/\blow\(/',
|
|
|
|
'strtolower('
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'getMicrotime() -> microtime(true)',
|
|
|
|
'/getMicrotime\(\)/',
|
|
|
|
'microtime(true)'
|
|
|
|
),
|
|
|
|
);
|
2010-12-12 04:46:39 +00:00
|
|
|
$this->_filesRegexpUpdate($patterns);
|
|
|
|
}
|
2010-12-12 04:38:23 +00:00
|
|
|
|
2010-12-12 04:46:39 +00:00
|
|
|
/**
|
|
|
|
* Update the properties moved to CakeRequest.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function request() {
|
2011-01-02 03:13:45 +00:00
|
|
|
$views = array_diff(App::path('views'), App::core('views'));
|
|
|
|
$controllers = array_diff(App::path('controllers'), App::core('controllers'), array(APP));
|
|
|
|
$components = array_diff(App::path('components'), App::core('components'));
|
|
|
|
|
|
|
|
$this->_paths = array_merge($views, $controllers, $components);
|
|
|
|
|
2010-12-12 04:46:39 +00:00
|
|
|
if (!empty($this->params['plugin'])) {
|
2011-01-02 03:13:45 +00:00
|
|
|
$pluginPath = App::pluginPath($this->params['plugin']);
|
|
|
|
$this->_paths = array(
|
|
|
|
$pluginPath . 'controllers' . DS,
|
|
|
|
$pluginPath . 'controllers' . DS . 'components' .DS,
|
|
|
|
$pluginPath . 'views' . DS,
|
|
|
|
);
|
2010-12-12 04:46:39 +00:00
|
|
|
}
|
|
|
|
$patterns = array(
|
|
|
|
array(
|
|
|
|
'$this->data -> $this->request->data',
|
2011-01-02 03:58:40 +00:00
|
|
|
'/(\$this->data\b(?!\())/',
|
2010-12-12 04:46:39 +00:00
|
|
|
'$this->request->data'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'$this->params -> $this->request->params',
|
2011-01-02 03:58:40 +00:00
|
|
|
'/(\$this->params\b(?!\())/',
|
2010-12-12 04:46:39 +00:00
|
|
|
'$this->request->params'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'$this->webroot -> $this->request->webroot',
|
2011-01-02 03:58:40 +00:00
|
|
|
'/(\$this->webroot\b(?!\())/',
|
2010-12-12 04:46:39 +00:00
|
|
|
'$this->request->webroot'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'$this->base -> $this->request->base',
|
2011-01-02 03:58:40 +00:00
|
|
|
'/(\$this->base\b(?!\())/',
|
2010-12-12 04:46:39 +00:00
|
|
|
'$this->request->base'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'$this->here -> $this->request->here',
|
2011-01-02 03:58:40 +00:00
|
|
|
'/(\$this->here\b(?!\())/',
|
2010-12-12 04:46:39 +00:00
|
|
|
'$this->request->here'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'$this->action -> $this->request->action',
|
2011-01-02 03:58:40 +00:00
|
|
|
'/(\$this->action\b(?!\())/',
|
2010-12-12 04:46:39 +00:00
|
|
|
'$this->request->action'
|
|
|
|
),
|
|
|
|
);
|
2010-12-12 04:38:23 +00:00
|
|
|
$this->_filesRegexpUpdate($patterns);
|
|
|
|
}
|
|
|
|
|
2010-12-19 14:06:45 +00:00
|
|
|
/**
|
|
|
|
* Update Configure::read() calls with no params.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function configure() {
|
|
|
|
$this->_paths = array(
|
|
|
|
APP
|
|
|
|
);
|
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
$this->_paths = array(App::pluginPath($this->params['plugin']));
|
|
|
|
}
|
|
|
|
$patterns = array(
|
|
|
|
array(
|
|
|
|
"Configure::read() -> Configure::read('debug')",
|
|
|
|
'/Configure::read\(\)/',
|
|
|
|
'Configure::read(\'debug\')'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
$this->_filesRegexpUpdate($patterns);
|
|
|
|
}
|
|
|
|
|
2011-05-15 17:10:53 +00:00
|
|
|
/**
|
|
|
|
* constants
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function constants() {
|
|
|
|
$this->_paths = array(
|
|
|
|
APP
|
|
|
|
);
|
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
$this->_paths = array(App::pluginPath($this->params['plugin']));
|
|
|
|
}
|
|
|
|
$patterns = array(
|
|
|
|
array(
|
|
|
|
"LIBS -> CAKE",
|
|
|
|
'/\bLIBS\b/',
|
|
|
|
'CAKE'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"CONFIGS -> APP . 'Config' . DS",
|
|
|
|
'/\bCONFIGS\b/',
|
|
|
|
'APP . \'Config\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"CONTROLLERS -> APP . 'Controller' . DS",
|
|
|
|
'/\bCONTROLLERS\b/',
|
|
|
|
'APP . \'Controller\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"COMPONENTS -> APP . 'Controller' . DS . 'Component' . DS",
|
|
|
|
'/\bCOMPONENTS\b/',
|
|
|
|
'APP . \'Controller\' . DS . \'Component\''
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"MODELS -> APP . 'Model' . DS",
|
|
|
|
'/\bMODELS\b/',
|
|
|
|
'APP . \'Model\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"BEHAVIORS -> APP . 'Model' . DS . 'Behavior' . DS",
|
|
|
|
'/\bBEHAVIORS\b/',
|
|
|
|
'APP . \'Model\' . DS . \'Behavior\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"VIEWS -> APP . 'View' . DS",
|
|
|
|
'/\bVIEWS\b/',
|
|
|
|
'APP . \'View\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"HELPERS -> APP . 'View' . DS . 'Helper' . DS",
|
|
|
|
'/\bHELPERS\b/',
|
|
|
|
'APP . \'View\' . DS . \'Helper\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"LAYOUTS -> APP . 'View' . DS . 'Layouts' . DS",
|
|
|
|
'/\bLAYOUTS\b/',
|
|
|
|
'APP . \'View\' . DS . \'Layouts\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"ELEMENTS -> APP . 'View' . DS . 'Elements' . DS",
|
|
|
|
'/\bELEMENTS\b/',
|
|
|
|
'APP . \'View\' . DS . \'Elements\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"CONSOLE_LIBS -> CAKE . 'Console' . DS",
|
|
|
|
'/\bCONSOLE_LIBS\b/',
|
|
|
|
'CAKE . \'Console\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"CAKE_TESTS_LIB -> CAKE . 'TestSuite' . DS",
|
|
|
|
'/\bCAKE_TESTS_LIB\b/',
|
|
|
|
'CAKE . \'TestSuite\' . DS'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"CAKE_TESTS -> CAKE . 'Test' . DS",
|
|
|
|
'/\bCAKE_TESTS\b/',
|
|
|
|
'CAKE . \'Test\' . DS'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->_filesRegexpUpdate($patterns);
|
|
|
|
}
|
|
|
|
|
2011-07-02 15:49:29 +00:00
|
|
|
/**
|
|
|
|
* Update components.
|
|
|
|
*
|
|
|
|
* - Make components that extend Object to extend Component.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function components() {
|
|
|
|
$this->_paths = App::Path('Controller/Component');
|
|
|
|
if (!empty($this->params['plugin'])) {
|
|
|
|
$this->_paths = App::Path('Controller/Component', $this->params['plugin']);
|
|
|
|
}
|
|
|
|
$patterns = array(
|
|
|
|
array(
|
2011-07-02 21:21:27 +00:00
|
|
|
'*Component extends Object to *Component extends Component',
|
2011-07-02 15:49:29 +00:00
|
|
|
'/([a-zA-Z]*Component extends) Object/',
|
|
|
|
'\1 Component'
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->_filesRegexpUpdate($patterns);
|
|
|
|
}
|
|
|
|
|
2011-05-08 21:55:16 +00:00
|
|
|
/**
|
|
|
|
* Move application php files to where they now should be
|
|
|
|
*
|
|
|
|
* Find all php files in the folder (honoring recursive) and determine where cake expects the file to be
|
|
|
|
* If the file is not exactly where cake expects it - move it.
|
|
|
|
*
|
|
|
|
* @param mixed $path
|
|
|
|
* @param mixed $options array(recursive, checkFolder)
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _movePhpFiles($path, $options) {
|
|
|
|
if (!is_dir($path)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-01 21:07:22 +00:00
|
|
|
$paths = $this->_paths;
|
|
|
|
|
2011-05-08 21:55:16 +00:00
|
|
|
$this->_paths = array($path);
|
2011-05-01 21:16:00 +00:00
|
|
|
$this->_files = array();
|
2011-05-08 21:55:16 +00:00
|
|
|
if ($options['recursive']) {
|
|
|
|
$this->_findFiles('php');
|
|
|
|
} else {
|
|
|
|
$this->_files = scandir($path);
|
|
|
|
foreach($this->_files as $i => $file) {
|
|
|
|
if (strlen($file) < 5 || substr($file, -4) !== '.php') {
|
|
|
|
unset($this->_files[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cwd = getcwd();
|
|
|
|
foreach ($this->_files as &$file) {
|
|
|
|
$file = $cwd . DS . $file;
|
|
|
|
|
2011-05-01 21:07:22 +00:00
|
|
|
$contents = file_get_contents($file);
|
2011-05-08 21:55:16 +00:00
|
|
|
preg_match('@class (\S*) .*{@', $contents, $match);
|
2011-05-01 21:07:22 +00:00
|
|
|
if (!$match) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$class = $match[1];
|
|
|
|
|
2011-05-08 22:00:19 +00:00
|
|
|
if (substr($class, 0, 3) === 'Dbo') {
|
|
|
|
$type = 'Dbo';
|
2011-05-08 21:55:16 +00:00
|
|
|
} else {
|
2011-05-08 22:00:19 +00:00
|
|
|
preg_match('@([A-Z][^A-Z]*)$@', $class, $match);
|
|
|
|
if ($match) {
|
|
|
|
$type = $match[1];
|
|
|
|
} else {
|
|
|
|
$type = 'unknown';
|
|
|
|
}
|
2011-05-08 21:55:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
preg_match('@^.*[\\\/]plugins[\\\/](.*?)[\\\/]@', $file, $match);
|
|
|
|
$base = $cwd . DS;
|
|
|
|
$plugin = false;
|
|
|
|
if ($match) {
|
|
|
|
$base = $match[0];
|
|
|
|
$plugin = $match[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['checkFolder'] && !empty($this->_map[$type])) {
|
|
|
|
$folder = str_replace('/', DS, $this->_map[$type]);
|
|
|
|
$new = $base . $folder . DS . $class . '.php';
|
|
|
|
} else {
|
|
|
|
$new = dirname($file) . DS . $class . '.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($file === $new) {
|
2011-05-01 21:07:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-05-08 21:55:16 +00:00
|
|
|
$dir = dirname($new);
|
|
|
|
if (!is_dir($dir)) {
|
|
|
|
new Folder($dir, true);
|
2011-05-01 21:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->out('Moving ' . $file . ' to ' . $new, 1, Shell::VERBOSE);
|
2011-05-08 21:55:16 +00:00
|
|
|
if (!$this->params['dry-run']) {
|
|
|
|
if ($this->params['git']) {
|
|
|
|
exec('git mv -f ' . escapeshellarg($file) . ' ' . escapeshellarg($new));
|
|
|
|
} else {
|
|
|
|
rename($file, $new);
|
|
|
|
}
|
|
|
|
}
|
2011-05-01 21:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_paths = $paths;
|
|
|
|
}
|
|
|
|
|
2010-12-12 04:13:21 +00:00
|
|
|
/**
|
|
|
|
* Updates files based on regular expressions.
|
|
|
|
*
|
|
|
|
* @param array $patterns Array of search and replacement patterns.
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-12-11 15:13:59 +00:00
|
|
|
protected function _filesRegexpUpdate($patterns) {
|
2010-12-12 04:13:21 +00:00
|
|
|
$this->_findFiles($this->params['ext']);
|
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-12 04:13:21 +00:00
|
|
|
/**
|
|
|
|
* Searches the paths and finds files based on extension.
|
|
|
|
*
|
2011-05-01 17:32:09 +00:00
|
|
|
* @param string $extensions
|
2010-12-12 04:13:21 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _findFiles($extensions = '') {
|
2010-12-05 05:48:38 +00:00
|
|
|
foreach ($this->_paths as $path) {
|
2011-05-01 17:32:09 +00:00
|
|
|
if (!is_dir($path)) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-12-12 04:13:21 +00:00
|
|
|
$files = array();
|
|
|
|
$Iterator = new RegexIterator(
|
|
|
|
new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
|
|
|
|
'/^.+\.(' . $extensions . ')$/i',
|
|
|
|
RegexIterator::MATCH
|
|
|
|
);
|
|
|
|
foreach ($Iterator as $file) {
|
|
|
|
if ($file->isFile()) {
|
|
|
|
$files[] = $file->getPathname();
|
2010-12-05 05:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->_files = array_merge($this->_files, $files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a single file.
|
|
|
|
*
|
2010-12-12 04:13:21 +00:00
|
|
|
* @param string $file The file to update
|
|
|
|
* @param array $patterns The replacement patterns to run.
|
2010-12-05 05:48:38 +00:00
|
|
|
* @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);
|
2011-05-01 17:32:09 +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);
|
2011-01-02 03:13:45 +00:00
|
|
|
if (!$this->params['dry-run']) {
|
|
|
|
file_put_contents($file, $contents);
|
|
|
|
}
|
2010-12-05 05:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the option parser
|
|
|
|
*
|
2010-12-12 04:13:21 +00:00
|
|
|
* @return ConsoleOptionParser
|
2010-12-05 05:48:38 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function getOptionParser() {
|
2010-12-11 15:14:54 +00:00
|
|
|
$subcommandParser = array(
|
|
|
|
'options' => array(
|
2010-12-12 04:13:21 +00:00
|
|
|
'plugin' => array(
|
|
|
|
'short' => 'p',
|
|
|
|
'help' => __('The plugin to update. Only the specified plugin will be updated.'
|
|
|
|
)),
|
|
|
|
'ext' => array(
|
|
|
|
'short' => 'e',
|
|
|
|
'help' => __('The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
|
|
|
|
'default' => 'php|ctp|thtml|inc|tpl'
|
|
|
|
),
|
2011-05-08 21:55:16 +00:00
|
|
|
'git'=> array(
|
|
|
|
'help' => __('use git command for moving files around.'),
|
|
|
|
'default' => 0
|
|
|
|
),
|
2011-01-02 03:13:45 +00:00
|
|
|
'dry-run'=> array(
|
|
|
|
'short' => 'd',
|
|
|
|
'help' => __('Dry run the update, no files will actually be modified.'),
|
|
|
|
'boolean' => true
|
|
|
|
)
|
2010-12-11 15:14:54 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2010-12-05 05:48:38 +00:00
|
|
|
return parent::getOptionParser()
|
2011-05-01 17:45: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.")
|
2011-05-01 19:55:15 +00:00
|
|
|
->addSubcommand('all', array(
|
|
|
|
'help' => 'Run all upgrade commands.',
|
|
|
|
'parser' => $subcommandParser
|
|
|
|
))
|
2011-05-01 20:40:40 +00:00
|
|
|
->addSubcommand('locations', array(
|
|
|
|
'help' => 'Move files and folders to their new homes.',
|
|
|
|
'parser' => $subcommandParser
|
|
|
|
))
|
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-12 04:38:23 +00:00
|
|
|
))
|
|
|
|
->addSubcommand('basics', array(
|
|
|
|
'help' => 'Update removed basics functions to PHP native functions.',
|
|
|
|
'parser' => $subcommandParser
|
2010-12-12 04:46:39 +00:00
|
|
|
))
|
|
|
|
->addSubcommand('request', array(
|
|
|
|
'help' => 'Update removed request access, and replace with $this->request.',
|
|
|
|
'parser' => $subcommandParser
|
2010-12-19 14:06:45 +00:00
|
|
|
))
|
|
|
|
->addSubcommand('configure', array(
|
|
|
|
'help' => "Update Configure::read() to Configure::read('debug')",
|
|
|
|
'parser' => $subcommandParser
|
2011-05-15 17:10:53 +00:00
|
|
|
))
|
|
|
|
->addSubcommand('constants', array(
|
|
|
|
'help' => "Replace Obsolete constants",
|
|
|
|
'parser' => $subcommandParser
|
2011-07-02 15:49:29 +00:00
|
|
|
))
|
|
|
|
->addSubcommand('components', array(
|
|
|
|
'help' => 'Update components to extend Component class.',
|
|
|
|
'parser' => $subcommandParser
|
2010-12-05 05:48:38 +00:00
|
|
|
));
|
|
|
|
}
|
2011-05-01 17:32:09 +00:00
|
|
|
}
|