Skip to content

Commit d9844c4

Browse files
authored
Merge pull request #38 from vanilla/fix/command-subclass
Fix bug when adding methods from subclasses
2 parents 07c361f + 93b3e7d commit d9844c4

5 files changed

Lines changed: 37 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ class App extends Garden\Cli\Application\CliApplication {
208208
$this->addFactory(\PDO::class, [\Garden\Cli\Utility\DbUtils::class, 'createMySQL']);
209209
}
210210

211-
protected function configureContainer(): void {
212-
parent::configureContainer();
211+
protected function configureContainer(Container $container): void {
212+
parent::configureContainer($container);
213213

214214
// Configure the container here.
215215
}

src/Application/CliApplication.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ protected function configureCli(): void {
7272
*/
7373
final public function getContainer(): Container {
7474
if ($this->container === null) {
75-
$this->container = $this->createContainer();
76-
$this->configureContainer();
75+
$container = $this->createContainer();
76+
$this->configureContainer($container);
77+
$this->container = $container;
7778
$this->container->setInstance(Container::class, $this->container);
7879
}
7980
return $this->container;
@@ -93,8 +94,10 @@ protected function createContainer(): Container {
9394

9495
/**
9596
* Configure the Container for usage.
97+
*
98+
* @param Container $container
9699
*/
97-
protected function configureContainer(): void {
100+
protected function configureContainer(Container $container): void {
98101
}
99102

100103
/**
@@ -233,7 +236,7 @@ public function addMethod(string $className, string $methodName, array $options
233236
$this
234237
->command($options[self::OPT_COMMAND])
235238
->description($description)
236-
->meta(self::META_ACTION, $method->getDeclaringClass()->getName() . '::' . $method->getName());
239+
->meta(self::META_ACTION, $class->getName() . '::' . $method->getName());
237240
$this->addParams($method);
238241

239242
if ($options[self::OPT_SETTERS]) {
@@ -258,7 +261,7 @@ public function addMethod(string $className, string $methodName, array $options
258261
* @param array $options Options to control the behavior of the mapping.
259262
* @return $this
260263
*/
261-
public function addCommandClass(string $className, string $methodName, array $options = []): self {
264+
public function addCommandClass(string $className, string $methodName = 'run', array $options = []): self {
262265
$options += [
263266
self::OPT_COMMAND => null,
264267
self::OPT_SETTERS => true,

tests/App/CliApplicationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Garden\Cli\Tests\AbstractCliTest;
1313
use Garden\Cli\Tests\Fixtures\Application;
1414
use Garden\Cli\Tests\Fixtures\Db;
15+
use Garden\Cli\Tests\Fixtures\RealCommand;
1516
use Garden\Cli\Tests\Fixtures\TestApplication;
1617
use Garden\Cli\Tests\Fixtures\TestCommands;
1718

@@ -137,6 +138,13 @@ public function testAddCommandClass(): void {
137138
$this->assertTrue($schema->hasOpt('bar'));
138139
}
139140

141+
public function testAddCommandSubclass(): void {
142+
$this->app->addCommandClass(RealCommand::class, 'noParams');
143+
$schema = $this->app->getSchema('real');
144+
145+
$this->assertSame(RealCommand::class.'::noParams', $schema->getMeta(CliApplication::META_ACTION));
146+
}
147+
140148
/**
141149
* Test a basic dispatch.
142150
*/

tests/Fixtures/RealCommand.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* @author Todd Burry <todd@vanillaforums.com>
4+
* @copyright 2009-2020 Vanilla Forums Inc.
5+
* @license MIT
6+
*/
7+
8+
namespace Garden\Cli\Tests\Fixtures;
9+
10+
11+
/**
12+
* Do something real.
13+
*/
14+
class RealCommand extends TestCommands {
15+
16+
}

tests/Fixtures/TestApplication.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
use Garden\Container\Reference;
1414

1515
class TestApplication extends CliApplication {
16-
protected function configureContainer(): void {
17-
parent::configureContainer();
16+
protected function configureContainer(Container $container): void {
17+
parent::configureContainer($container);
1818

19-
$this->getContainer()
19+
$container
2020
->rule(TestCommands::class)
2121
->setShared(true)
2222
->addCall('setDb', [new Reference(Db::class)]);

0 commit comments

Comments
 (0)