Skip to content

Commit 9d02066

Browse files
authored
Merge pull request #8898 from kenjis/fix-command-discovery
fix: precedence of command classes with the same `$name`
2 parents 61d2742 + e6b5931 commit 9d02066

File tree

3 files changed

+124
-1
lines changed

3 files changed

+124
-1
lines changed

system/CLI/Commands.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function discoverCommands()
125125
/** @var BaseCommand $class */
126126
$class = new $className($this->logger, $this);
127127

128-
if (isset($class->group)) {
128+
if (isset($class->group) && ! isset($this->commands[$class->name])) {
129129
$this->commands[$class->name] = [
130130
'class' => $className,
131131
'file' => $file,
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace App\Commands;
15+
16+
use CodeIgniter\CLI\CLI;
17+
use CodeIgniter\Commands\ListCommands as BaseListCommands;
18+
19+
class ListCommands extends BaseListCommands
20+
{
21+
/**
22+
* The group the command is lumped under
23+
* when listing commands.
24+
*
25+
* @var string
26+
*/
27+
protected $group = 'App';
28+
29+
/**
30+
* The Command's name
31+
*
32+
* @var string
33+
*/
34+
protected $name = 'list';
35+
36+
/**
37+
* the Command's short description
38+
*
39+
* @var string
40+
*/
41+
protected $description = 'This is testing to override `list` command.';
42+
43+
/**
44+
* the Command's usage
45+
*
46+
* @var string
47+
*/
48+
protected $usage = 'list';
49+
50+
/**
51+
* Displays the help for the spark cli script itself.
52+
*/
53+
public function run(array $params)
54+
{
55+
CLI::write('This is ' . self::class);
56+
57+
return EXIT_SUCCESS;
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\StreamFilterTrait;
18+
use PHPUnit\Framework\Attributes\Group;
19+
20+
/**
21+
* @internal
22+
*/
23+
#[Group('Others')]
24+
final class CommandOverrideTest extends CIUnitTestCase
25+
{
26+
use StreamFilterTrait;
27+
28+
protected function setUp(): void
29+
{
30+
$this->resetServices();
31+
32+
parent::setUp();
33+
}
34+
35+
protected function getBuffer(): string
36+
{
37+
return $this->getStreamFilterBuffer();
38+
}
39+
40+
public function testOverrideListCommands(): void
41+
{
42+
$this->copyListCommands();
43+
44+
command('list');
45+
46+
$this->assertStringContainsString('This is App\Commands\ListCommands', $this->getBuffer());
47+
$this->assertStringNotContainsString('Displays basic usage information.', $this->getBuffer());
48+
49+
$this->deleteListCommands();
50+
}
51+
52+
private function copyListCommands(): void
53+
{
54+
if (! is_dir(APPPATH . 'Commands')) {
55+
mkdir(APPPATH . 'Commands');
56+
}
57+
copy(SUPPORTPATH . '_command/ListCommands.php', APPPATH . 'Commands/ListCommands.php');
58+
}
59+
60+
private function deleteListCommands(): void
61+
{
62+
unlink(APPPATH . 'Commands/ListCommands.php');
63+
}
64+
}

0 commit comments

Comments
 (0)