Skip to content

Commit

Permalink
Add support for Commands to autodiscovery.
Browse files Browse the repository at this point in the history
Expand autodiscovery in CommandCollection to include Commands.
Also exclude files that dont have the right suffix to prevent #11326 in
the future.

Refs #11326
Refs #11137
  • Loading branch information
markstory committed Oct 17, 2017
1 parent 56a1811 commit 1eb3dcb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/Console/CommandScanner.php
Expand Up @@ -36,30 +36,46 @@ class CommandScanner
public function scanAll()
{
$shellList = [];

$appNamespace = Configure::read('App.namespace');
$shellList['app'] = $this->scanDir(

$coreShells = $this->scanDir(
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Shell' . DIRECTORY_SEPARATOR,
'Cake\Shell\\',
'',
['command_list']
);
$coreCommands = $this->scanDir(
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Command' . DIRECTORY_SEPARATOR,
'Cake\Command\\',
'',
['command_list']
);
$shellList['CORE'] = array_merge($coreShells, $coreCommands);

$appShells = $this->scanDir(
App::path('Shell')[0],
$appNamespace . '\Shell\\',
'',
['app']
);

$shellList['CORE'] = $this->scanDir(
dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Shell' . DIRECTORY_SEPARATOR,
'Cake\Shell\\',
$appCommands = $this->scanDir(
App::path('Command')[0],
$appNamespace . '\Command\\',
'',
['command_list']
['app']
);
$shellList['app'] = array_merge($appShells, $appCommands);

$plugins = [];
foreach (Plugin::loaded() as $plugin) {
$plugins[$plugin] = $this->scanDir(
Plugin::classPath($plugin) . 'Shell',
str_replace('/', '\\', $plugin) . '\Shell\\',
Inflector::underscore($plugin) . '.',
[]
);
$path = Plugin::classPath($plugin);
$namespace = str_replace('/', '\\', $plugin);
$prefix = Inflector::underscore($plugin) . '.';

$commands = $this->scanDir($path . 'Command', $namespace . '\Command\\', $prefix, []);
$shells = $this->scanDir($path . 'Shell', $namespace . '\Shell\\', $prefix, []);

$plugins[$plugin] = array_merge($shells, $commands);
}
$shellList['plugins'] = $plugins;

Expand All @@ -84,14 +100,18 @@ protected function scanDir($path, $namespace, $prefix, array $hide)
return [];
}

$classPattern = '/(Shell|Command)$/';
$shells = [];
foreach ($contents[1] as $file) {
if (substr($file, -4) !== '.php') {
continue;
}

$shell = substr($file, 0, -4);
$name = Inflector::underscore(str_replace('Shell', '', $shell));
if (!preg_match($classPattern, $shell)) {
continue;
}

$name = Inflector::underscore(preg_replace($classPattern, '', $shell));
if (in_array($name, $hide, true)) {
continue;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase/Console/CommandCollectionTest.php
Expand Up @@ -203,10 +203,12 @@ public function testAutoDiscoverApp()
$collection = new CommandCollection();
$collection->addMany($collection->autoDiscover());

$this->assertTrue($collection->has('demo'));
$this->assertTrue($collection->has('i18m'));
$this->assertTrue($collection->has('sample'));
$this->assertTrue($collection->has('testing_dispatch'));

$this->assertSame('TestApp\Command\DemoCommand', $collection->get('demo'));
$this->assertSame('TestApp\Shell\I18mShell', $collection->get('i18m'));
$this->assertSame('TestApp\Shell\SampleShell', $collection->get('sample'));
}
Expand All @@ -221,6 +223,7 @@ public function testAutoDiscoverCore()
$collection = new CommandCollection();
$collection->addMany($collection->autoDiscover());

$this->assertTrue($collection->has('version'));
$this->assertTrue($collection->has('routes'));
$this->assertTrue($collection->has('i18n'));
$this->assertTrue($collection->has('orm_cache'));
Expand All @@ -231,6 +234,7 @@ public function testAutoDiscoverCore()
// These have to be strings as ::class uses the local namespace.
$this->assertSame('Cake\Shell\RoutesShell', $collection->get('routes'));
$this->assertSame('Cake\Shell\I18nShell', $collection->get('i18n'));
$this->assertSame('Cake\Command\VersionCommand', $collection->get('version'));
}

/**
Expand Down

0 comments on commit 1eb3dcb

Please sign in to comment.