| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- /**
- * A shell class to help developers upgrade applications to CakePHP 2.0
- *
- * @package cake.console/shells
- */
- App::uses('Folder', 'Utility');
- class UpgradeShell extends Shell {
- protected $_files = array();
- protected $_paths = array();
- /**
- * Shell startup, prints info message about dry run.
- *
- * @return void
- */
- function startup() {
- parent::startup();
- if ($this->params['dry-run']) {
- $this->out('<warning>Dry-run mode enabled!</warning>', 1, Shell::QUIET);
- }
- }
- function all() {
- foreach($this->OptionParser->subcommands() as $command) {
- $name = $command->name();
- if ($name === 'all') {
- continue;
- }
- $this->out('Running ' . $name);
- $this->$name();
- }
- }
- function locations() {
- $moves = array(
- 'controllers' . DS . 'components' => 'Controller' . DS . 'Component',
- 'controllers' => 'Controller',
- 'libs' => 'Lib',
- 'models' . DS . 'behaviors' => 'Model' . DS . 'Behavior',
- 'models' . DS . 'datasources' => 'Model' . DS . 'Datasource',
- 'models' => 'Model',
- 'tests' . DS . 'cases' => 'tests' . DS . 'Case',
- 'tests' . DS . 'fixtures' => 'tests' . DS . 'Fixture',
- 'vendors' . DS . 'shells' . DS . 'templates' => 'Console' . DS . 'templates',
- 'vendors' . DS . 'shells' => 'Console' . DS . 'Command',
- 'views' . DS . 'helpers' => 'View' . DS . 'Helper',
- 'views' => 'View'
- );
- foreach($moves as $old => $new) {
- if (is_dir($old)) {
- $this->out("Moving $old to $new");
- $Folder = new Folder($old);
- $Folder->move($new);
- }
- }
- }
- /**
- * Update helpers.
- *
- * - Converts helpers usage to new format.
- *
- * @return void
- */
- function helpers() {
- $this->_paths = array_diff(App::path('views'), App::core('views'));
- if (!empty($this->params['plugin'])) {
- $this->_paths = array(App::pluginPath($this->params['plugin']) . 'views' . DS);
- }
- $patterns = array();
- $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) {
- $oldHelper = strtolower(substr($helper, 0, 1)).substr($helper, 1);
- $patterns[] = array(
- "\${$oldHelper} to \$this->{$helper}",
- "/\\\${$oldHelper}->/",
- "\\\$this->{$helper}->"
- );
- }
- $this->_filesRegexpUpdate($patterns);
- }
- /**
- * Update i18n.
- *
- * - Removes extra true param.
- * - Add the echo to __*() calls that didn't need them before.
- *
- * @return void
- */
- function i18n() {
- $this->_paths = array(
- APP
- );
- if (!empty($this->params['plugin'])) {
- $this->_paths = array(App::pluginPath($this->params['plugin']));
- }
- $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')
- );
- $this->_filesRegexpUpdate($patterns);
- }
- /**
- * 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(*)',
- '/\ba\((.*)\)/',
- '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)'
- ),
- );
- $this->_filesRegexpUpdate($patterns);
- }
- /**
- * Update the properties moved to CakeRequest.
- *
- * @return void
- */
- public function request() {
- $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);
- if (!empty($this->params['plugin'])) {
- $pluginPath = App::pluginPath($this->params['plugin']);
- $this->_paths = array(
- $pluginPath . 'controllers' . DS,
- $pluginPath . 'controllers' . DS . 'components' .DS,
- $pluginPath . 'views' . DS,
- );
- }
- $patterns = array(
- array(
- '$this->data -> $this->request->data',
- '/(\$this->data\b(?!\())/',
- '$this->request->data'
- ),
- array(
- '$this->params -> $this->request->params',
- '/(\$this->params\b(?!\())/',
- '$this->request->params'
- ),
- array(
- '$this->webroot -> $this->request->webroot',
- '/(\$this->webroot\b(?!\())/',
- '$this->request->webroot'
- ),
- array(
- '$this->base -> $this->request->base',
- '/(\$this->base\b(?!\())/',
- '$this->request->base'
- ),
- array(
- '$this->here -> $this->request->here',
- '/(\$this->here\b(?!\())/',
- '$this->request->here'
- ),
- array(
- '$this->action -> $this->request->action',
- '/(\$this->action\b(?!\())/',
- '$this->request->action'
- ),
- );
- $this->_filesRegexpUpdate($patterns);
- }
- /**
- * 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);
- }
- /**
- * Updates files based on regular expressions.
- *
- * @param array $patterns Array of search and replacement patterns.
- * @return void
- */
- protected function _filesRegexpUpdate($patterns) {
- $this->_findFiles($this->params['ext']);
- foreach ($this->_files as $file) {
- $this->out('Updating ' . $file . '...', 1, Shell::VERBOSE);
- $this->_updateFile($file, $patterns);
- }
- }
- /**
- * Searches the paths and finds files based on extension.
- *
- * @param string $extensions
- * @return void
- */
- protected function _findFiles($extensions = '') {
- foreach ($this->_paths as $path) {
- if (!is_dir($path)) {
- continue;
- }
- $files = array();
- $Iterator = new RegexIterator(
- new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)),
- '/^.+\.(' . $extensions . ')$/i',
- RegexIterator::MATCH
- );
- foreach ($Iterator as $file) {
- if ($file->isFile()) {
- $files[] = $file->getPathname();
- }
- }
- $this->_files = array_merge($this->_files, $files);
- }
- }
- /**
- * Update a single file.
- *
- * @param string $file The file to update
- * @param array $patterns The replacement patterns to run.
- * @return void
- */
- protected function _updateFile($file, $patterns) {
- $contents = file_get_contents($file);
- 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);
- if (!$this->params['dry-run']) {
- file_put_contents($file, $contents);
- }
- }
- /**
- * get the option parser
- *
- * @return ConsoleOptionParser
- */
- function getOptionParser() {
- $subcommandParser = array(
- 'options' => array(
- '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'
- ),
- 'dry-run'=> array(
- 'short' => 'd',
- 'help' => __('Dry run the update, no files will actually be modified.'),
- 'boolean' => true
- )
- )
- );
- return parent::getOptionParser()
- ->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.")
- ->addSubcommand('all', array(
- 'help' => 'Run all upgrade commands.',
- 'parser' => $subcommandParser
- ))
- ->addSubcommand('locations', array(
- 'help' => 'Move files and folders to their new homes.',
- 'parser' => $subcommandParser
- ))
- ->addSubcommand('i18n', array(
- 'help' => 'Update the i18n translation method calls.',
- 'parser' => $subcommandParser
- ))
- ->addSubcommand('helpers', array(
- 'help' => 'Update calls to helpers.',
- 'parser' => $subcommandParser
- ))
- ->addSubcommand('basics', array(
- 'help' => 'Update removed basics functions to PHP native functions.',
- 'parser' => $subcommandParser
- ))
- ->addSubcommand('request', array(
- 'help' => 'Update removed request access, and replace with $this->request.',
- 'parser' => $subcommandParser
- ))
- ->addSubcommand('configure', array(
- 'help' => "Update Configure::read() to Configure::read('debug')",
- 'parser' => $subcommandParser
- ));
- }
- }
|