Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ class App extends Garden\Cli\Application\CliApplication {
$this->addFactory(\PDO::class, [\Garden\Cli\Utility\DbUtils::class, 'createMySQL']);
}

protected function configureContainer(): void {
parent::configureContainer();
protected function configureContainer(Container $container): void {
parent::configureContainer($container);

// Configure the container here.
}
Expand Down
15 changes: 9 additions & 6 deletions src/Application/CliApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ protected function configureCli(): void {
*/
final public function getContainer(): Container {
if ($this->container === null) {
$this->container = $this->createContainer();
$this->configureContainer();
$container = $this->createContainer();
$this->configureContainer($container);
$this->container = $container;
$this->container->setInstance(Container::class, $this->container);
}
return $this->container;
Expand All @@ -93,8 +94,10 @@ protected function createContainer(): Container {

/**
* Configure the Container for usage.
*
* @param Container $container
*/
protected function configureContainer(): void {
protected function configureContainer(Container $container): void {
}

/**
Expand Down Expand Up @@ -233,7 +236,7 @@ public function addMethod(string $className, string $methodName, array $options
$this
->command($options[self::OPT_COMMAND])
->description($description)
->meta(self::META_ACTION, $method->getDeclaringClass()->getName() . '::' . $method->getName());
->meta(self::META_ACTION, $class->getName() . '::' . $method->getName());
$this->addParams($method);

if ($options[self::OPT_SETTERS]) {
Expand All @@ -258,7 +261,7 @@ public function addMethod(string $className, string $methodName, array $options
* @param array $options Options to control the behavior of the mapping.
* @return $this
*/
public function addCommandClass(string $className, string $methodName, array $options = []): self {
public function addCommandClass(string $className, string $methodName = 'run', array $options = []): self {
$options += [
self::OPT_COMMAND => null,
self::OPT_SETTERS => true,
Expand Down Expand Up @@ -463,7 +466,7 @@ final protected function addSetters(ReflectionClass $class, callable $filter = n
foreach ($this->reflectSetters($class, $filter) as $optName => $method) {
$param = $method->getParameters()[0];
if (null === $t = $param->getType()) {
$type = '';
$type = 'string';
} else {
$type = $t instanceof \ReflectionNamedType ? $t->getName() : (string)$t;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/App/CliApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Garden\Cli\Tests\AbstractCliTest;
use Garden\Cli\Tests\Fixtures\Application;
use Garden\Cli\Tests\Fixtures\Db;
use Garden\Cli\Tests\Fixtures\RealCommand;
use Garden\Cli\Tests\Fixtures\TestApplication;
use Garden\Cli\Tests\Fixtures\TestCommands;

Expand Down Expand Up @@ -73,6 +74,17 @@ public function testAddObjectSetters(): void {
]
], $opt->jsonSerialize());

$opt = $schema->getOpt('no-type');
$this->assertArraySubsetRecursive([
'description' => '',
'required' => false,
'type' => 'string',
'meta' => [
CliApplication::META_DISPATCH_TYPE => CliApplication::TYPE_CALL,
CliApplication::META_DISPATCH_VALUE => 'setNoType',
]
], $opt->jsonSerialize());

$this->assertFalse($schema->hasOpt('db'), 'Setters with types that cannot be set via CLI should not be reflected.');
}

Expand Down Expand Up @@ -126,6 +138,13 @@ public function testAddCommandClass(): void {
$this->assertTrue($schema->hasOpt('bar'));
}

public function testAddCommandSubclass(): void {
$this->app->addCommandClass(RealCommand::class, 'noParams');
$schema = $this->app->getSchema('real');

$this->assertSame(RealCommand::class.'::noParams', $schema->getMeta(CliApplication::META_ACTION));
}

/**
* Test a basic dispatch.
*/
Expand Down
16 changes: 16 additions & 0 deletions tests/Fixtures/RealCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @author Todd Burry <todd@vanillaforums.com>
* @copyright 2009-2020 Vanilla Forums Inc.
* @license MIT
*/

namespace Garden\Cli\Tests\Fixtures;


/**
* Do something real.
*/
class RealCommand extends TestCommands {

}
6 changes: 3 additions & 3 deletions tests/Fixtures/TestApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use Garden\Container\Reference;

class TestApplication extends CliApplication {
protected function configureContainer(): void {
parent::configureContainer();
protected function configureContainer(Container $container): void {
parent::configureContainer($container);

$this->getContainer()
$container
->rule(TestCommands::class)
->setShared(true)
->addCall('setDb', [new Reference(Db::class)]);
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/TestCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public static function setBar(string $bar) {
self::call(__FUNCTION__, compact('bar'));
}

public function setNoType($a) {
self::call(__FUNCTION__, compact('a'));
}

/**
* This method has no parameters.
*/
Expand Down