Skip to content
Permalink
Browse files

First draft of the namespace upgrader.

  • Loading branch information...
markstory committed Nov 4, 2012
1 parent ef0e816 commit 1d7e275fce4fd2918e7fb14de176ebf7940d2345
Showing with 117 additions and 8 deletions.
  1. +117 −8 lib/Cake/Console/Command/UpgradeShell.php
@@ -50,9 +50,6 @@ public function startup() {
if ($this->params['dry-run']) {
$this->out(__d('cake_console', '<warning>Dry-run mode enabled!</warning>'), 1, Shell::QUIET);
}
if ($this->params['git'] && !is_dir('.git')) {
$this->out(__d('cake_console', '<warning>No git repository detected!</warning>'), 1, Shell::QUIET);
}
}
/**
@@ -71,6 +68,108 @@ public function all() {
}
}
/**
* Convert App::uses() to normal use statements.
*
* @return void
*/
public function app_uses() {
$path = isset($this->args[0]) ? $this->args[0] : APP;
$Folder = new Folder($path);
$this->_paths = $Folder->tree(null, false, 'dir');
$this->_findFiles('php');
debug($this->_files);
}
/**
* Add namespaces to files.
*
* @return void
*/
public function namespaces() {
$path = isset($this->args[0]) ? $this->args[0] : APP;
$ns = $this->params['namespace'];
if ($ns === 'App' && isset($this->params['plugin'])) {
$ns = Inflector::camelize($this->params['plugin']);
}
$Folder = new Folder($path);
$exclude = ['vendor', 'Vendor', 'webroot', 'Plugin', 'tmp'];
if (!empty($this->params['exclude'])) {
$exclude = array_merge($exclude, explode(',', $this->params['exclude']));
}
list($dirs, $files) = $Folder->read(true, true, true);
$this->_paths = $this->_filterPaths($dirs, $exclude);
$this->_findFiles('php', ['index.php', 'test.php', 'cake.php']);
foreach ($this->_files as $filePath) {
$this->_addNamespace($path, $filePath, $ns, $this->params['dry-run']);
}
$this->out(__d('cake_console', '<success>Namespaces added successfully</success>'));
}
/**
* Filter paths to remove webroot, Plugin, tmp directories
*/
protected function _filterPaths($paths, $directories) {
return array_filter($paths, function ($path) use ($directories) {
foreach ($directories as $dir) {
if (strpos($path, DS . $dir) !== false) {
return false;
}
}
return true;
});
}
/**
* Adds the namespace to a given file.
*
* @param string $filePath The file to add a namespace to.
* @param string $ns The base namespace to use.
* @param bool $dry Whether or not to operate in dry-run mode.
* @return void
*/
protected function _addNamespace($path, $filePath, $ns, $dry) {
$result = true;
$shortPath = str_replace($path, '', $filePath);
$contents = file_get_contents($filePath);
if (preg_match('/namespace\s+[a-z0-9\\\]+;/', $contents)) {
$this->out(__d(
'cake_console',
"<warning>Skipping %s as it already has a namespace.</warning>",
$shortPath
));
return;
}
$namespace = trim($ns . str_replace(DS, '\\', dirname($shortPath)), '\\');
$this->out(
__d('cake_console', "<info>Adding namespace %s to %s </info>", $namespace, $shortPath),
1,
Shell::VERBOSE
);
if (!$dry) {
$contents = preg_replace(
'#^(<\?(?:php)?\s+(?:\/\*.*?\*\/\s{0,1})?)#s',
"\\1namespace " . $namespace . ";\n",
$contents
);
$result = file_put_contents($filePath, $contents);
}
if (!$result) {
$this->err(__d(
'cake_console',
'<error>Error</error> Was unable to update %s',
$filePath
));
}
}
/**
* Updates files based on regular expressions.
*
@@ -91,19 +190,19 @@ protected function _filesRegexpUpdate($patterns) {
* @param string $extensions
* @return void
*/
protected function _findFiles($extensions = '') {
protected function _findFiles($extensions = '', $exclude = []) {
$this->_files = array();
foreach ($this->_paths as $path) {
if (!is_dir($path)) {
continue;
}
$Iterator = new RegexIterator(
$Iterator = new \RegexIterator(
new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)),
'/^.+\.(' . $extensions . ')$/i',
\RegexIterator::MATCH
);
foreach ($Iterator as $file) {
if ($file->isFile()) {
if ($file->isFile() && !in_array($file->getFilename(), $exclude)) {
$this->_files[] = $file->getPathname();
}
}
@@ -146,7 +245,7 @@ public function getOptionParser() {
'ext' => array(
'short' => 'e',
'help' => __d('cake_console', 'The extension(s) to search. A pipe delimited list, or a preg_match compatible subpattern'),
'default' => 'php|ctp|thtml|inc|tpl'
'default' => 'php|ctp'
),
'dry-run' => array(
'short' => 'd',
@@ -156,6 +255,16 @@ public function getOptionParser() {
)
);
$namespaceParser = $subcommandParser;
$namespaceParser['options']['namespace'] = [
'help' => __d('cake_console', 'Set the base namespace you want to use. Defaults to App or the plugin name.'),
'default' => 'App',
];
$namespaceParser['options']['exclude'] = [
'help' => __d('cake_console', 'Comma separated list of top level diretories to exclude.'),
'default' => '',
];
return parent::getOptionParser()
->description(__d('cake_console', "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."))
@@ -169,7 +278,7 @@ public function getOptionParser() {
))
->addSubcommand('namespaces', array(
'help' => __d('cake_console', 'Add namespaces to files based on their file path.'),
'parser' => $subcommandParser
'parser' => $namespaceParser
))
->addSubcommand('cache', array(
'help' => __d('cake_console', "Replace Cache::config() with Configure."),

0 comments on commit 1d7e275

Please sign in to comment.
You can’t perform that action at this time.