Skip to content

Commit

Permalink
Merge 6c1ad01 into c3ef48a
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Felix Šulc committed May 7, 2018
2 parents c3ef48a + 6c1ad01 commit d56a4ae
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 30 deletions.
7 changes: 3 additions & 4 deletions .docs/README.md
Expand Up @@ -11,8 +11,7 @@

```yaml
extensions:
console: Contributte\Console\DI\ConsoleExtension

console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
```

The extension will look for all commands extending from [`Symfony\Component\Console\Command\Command`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Command/Command.php) and automatically add them to the console application.
Expand All @@ -33,7 +32,7 @@ console:
- Contributte\Console\Helper\ContainerHelper
```

In fact in console mode / SAPI mode is no http request and thus no URL address. This inconvenience you have to solve by yoursolve.
In fact in SAPI (CLI) mode there is no http request and thus no URL address. This inconvenience you have to solve by yourselve. Well, via `console.url` option.

```yaml
console:
Expand Down Expand Up @@ -88,7 +87,7 @@ How to define command names? Define `$defaultName` in command or via `console.co
```php
class FooCommand extends Command
{
public static $defaultName = 'app:foo';
protected static $defaultName = 'app:foo';
}
```

Expand Down
2 changes: 1 addition & 1 deletion src/CommandLoader/ContainerCommandLoader.php
Expand Up @@ -2,9 +2,9 @@

namespace Contributte\Console\CommandLoader;

use Contributte\Console\Exception\Runtime\CommandNotFoundException;
use Nette\DI\Container;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\Exception\CommandNotFoundException;

class ContainerCommandLoader implements CommandLoaderInterface
{
Expand Down
31 changes: 24 additions & 7 deletions src/DI/ConsoleExtension.php
Expand Up @@ -4,6 +4,7 @@

use Contributte\Console\Application;
use Contributte\Console\CommandLoader\ContainerCommandLoader;
use Contributte\Console\Exception\Logical\InvalidArgumentException;
use Contributte\Console\Helper\ContainerHelper;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
Expand Down Expand Up @@ -39,19 +40,34 @@ class ConsoleExtension extends CompilerExtension
'lazy' => FALSE,
];

/** @var bool */
private $cliMode;

/**
* @param bool $cliMode
*/
public function __construct($cliMode = FALSE)
{
if (func_num_args() <= 0) {
throw new InvalidArgumentException(sprintf('Provide CLI mode, e.q. %s(%%consoleMode%%).', self::class));
}

$this->cliMode = $cliMode;
}

/**
* Register services
*
* @return void
*/
public function loadConfiguration()
{
// Skip if it's not CLI mode
if (PHP_SAPI !== 'cli') return;

$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

// Skip if isn't CLI
if ($this->cliMode !== TRUE) return;

Validators::assertField($config, 'helpers', 'array|null');

$application = $builder->addDefinition($this->prefix('application'))
Expand Down Expand Up @@ -110,14 +126,15 @@ public function loadConfiguration()
*/
public function beforeCompile()
{
// Skip if it's not CLI mode
if (PHP_SAPI !== 'cli') return;

$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

// Skip if isn't CLI
if ($this->cliMode !== TRUE) return;

$application = $builder->getDefinition($this->prefix('application'));

// Setup URL in CLI
// Setup URL for CLI
if ($builder->hasDefinition('http.request') && $config['url'] !== NULL) {
$builder->getDefinition('http.request')
->setClass(Request::class, [new Statement(UrlScript::class, [$config['url']])]);
Expand Down
10 changes: 10 additions & 0 deletions src/Exception/Logical/InvalidArgumentException.php
@@ -0,0 +1,10 @@
<?php

namespace Contributte\Console\Exception\Logical;

use Contributte\Console\Exception\LogicalException;

class InvalidArgumentException extends LogicalException
{

}
10 changes: 10 additions & 0 deletions src/Exception/LogicalException.php
@@ -0,0 +1,10 @@
<?php

namespace Contributte\Console\Exception;

use LogicException;

class LogicalException extends LogicException
{

}
10 changes: 10 additions & 0 deletions src/Exception/Runtime/CommandNotFoundException.php
@@ -0,0 +1,10 @@
<?php

namespace Contributte\Console\Exception\Runtime;

use Contributte\Console\Exception\RuntimeException;

class CommandNotFoundException extends RuntimeException
{

}
8 changes: 8 additions & 0 deletions src/Exception/RuntimeException.php
@@ -0,0 +1,8 @@
<?php

namespace Contributte\Console\Exception;

class RuntimeException extends \RuntimeException
{

}
4 changes: 2 additions & 2 deletions tests/cases/Command/Command.HelperSet.phpt
Expand Up @@ -20,14 +20,14 @@ require_once __DIR__ . '/../../bootstrap.php';
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->addExtension('console', new ConsoleExtension(TRUE));
$compiler->loadConfig(FileMock::create('
console:
lazy: off
services:
- Tests\Fixtures\HelperSetCommand
', 'neon'));
}, [microtime(), 2]);
}, [getmypid(), 1]);

/** @var Container $container */
$container = new $class;
Expand Down
27 changes: 27 additions & 0 deletions tests/cases/CommandLoader/CommandLoader.phpt
@@ -0,0 +1,27 @@
<?php

/**
* Test: CommandLoader/CommandLoader
*/

use Contributte\Console\CommandLoader\ContainerCommandLoader;
use Contributte\Console\Exception\Runtime\CommandNotFoundException;
use Nette\DI\Container;
use Tester\Assert;
use Tester\Environment;

require_once __DIR__ . '/../../bootstrap.php';

if (!interface_exists('Symfony\Component\Console\CommandLoader\CommandLoaderInterface', TRUE)) {
Environment::skip('CommandLoaderInterface is available from symfony/console 3.4');
}

// Empty loader
test(function () {
$container = new Container();
$loader = new ContainerCommandLoader($container, []);

Assert::exception(function () use ($loader) {
$loader->get('foo');
}, CommandNotFoundException::class, 'Command "foo" does not exist.');
});
16 changes: 8 additions & 8 deletions tests/cases/DI/ConsoleExtension.HelperSet.phpt
Expand Up @@ -18,8 +18,8 @@ require_once __DIR__ . '/../../bootstrap.php';
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
}, [microtime(), 1]);
$compiler->addExtension('console', new ConsoleExtension(TRUE));
}, [getmypid(), 1]);

/** @var Container $container */
$container = new $class;
Expand All @@ -33,12 +33,12 @@ test(function () {
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->addExtension('console', new ConsoleExtension(TRUE));
$compiler->loadConfig(FileMock::create('
console:
helperSet: Tests\Fixtures\FooHelperSet
', 'neon'));
}, [microtime(), 3]);
}, [getmypid(), 2]);

/** @var Container $container */
$container = new $class;
Expand All @@ -51,15 +51,15 @@ test(function () {
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->addExtension('console', new ConsoleExtension(TRUE));
$compiler->loadConfig(FileMock::create('
console:
helperSet: @Tests\Fixtures\FooHelperSet
services:
- Tests\Fixtures\FooHelperSet
', 'neon'));
}, [microtime(), 3]);
}, [getmypid(), 3]);

/** @var Container $container */
$container = new $class;
Expand All @@ -72,13 +72,13 @@ test(function () {
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->addExtension('console', new ConsoleExtension(TRUE));
$compiler->loadConfig(FileMock::create('
console:
helpers:
- Tests\Fixtures\FooHelper
', 'neon'));
}, [microtime(), 4]);
}, [getmypid(), 4]);

/** @var Container $container */
$container = new $class;
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/DI/ConsoleExtension.lazy.phpt
Expand Up @@ -17,22 +17,22 @@ use Tests\Fixtures\FooCommand;

require_once __DIR__ . '/../../bootstrap.php';

if (!class_exists('Symfony\Component\Console\CommandLoader\CommandLoaderInterface')) {
Environment::skip('symfony/console 4.x is required');
if (!interface_exists('Symfony\Component\Console\CommandLoader\CommandLoaderInterface', TRUE)) {
Environment::skip('CommandLoaderInterface is available from symfony/console 3.4');
}

// 1 command of type FooCommand lazy-loading
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->addExtension('console', new ConsoleExtension(TRUE));
$compiler->loadConfig(FileMock::create('
console:
lazy: on
services:
foo: Tests\Fixtures\FooCommand
', 'neon'));
}, [microtime(), 3]);
}, [getmypid(), 1]);

/** @var Container $container */
$container = new $class;
Expand Down
35 changes: 31 additions & 4 deletions tests/cases/DI/ConsoleExtension.phpt
Expand Up @@ -6,6 +6,8 @@

use Contributte\Console\Application;
use Contributte\Console\DI\ConsoleExtension;
use Contributte\Console\Exception\Logical\InvalidArgumentException;
use Nette\Bridges\HttpDI\HttpExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
Expand All @@ -20,8 +22,8 @@ require_once __DIR__ . '/../../bootstrap.php';
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
}, [microtime(), 1]);
$compiler->addExtension('console', new ConsoleExtension(TRUE));
}, [getmypid(), 1]);

/** @var Container $container */
$container = new $class;
Expand All @@ -33,14 +35,14 @@ test(function () {
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->addExtension('console', new ConsoleExtension(TRUE));
$compiler->loadConfig(FileMock::create('
console:
lazy: off
services:
foo: Tests\Fixtures\FooCommand
', 'neon'));
}, [microtime(), 2]);
}, [getmypid(), 2]);

/** @var Container $container */
$container = new $class;
Expand All @@ -50,3 +52,28 @@ test(function () {
Assert::count(1, $container->findByType(Command::class));
Assert::type(FooCommand::class, $container->getByType(Command::class));
});

// Provide URL
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension(TRUE));
$compiler->addExtension('http', new HttpExtension(TRUE));
$compiler->loadConfig(FileMock::create('
console:
url: https://contributte.org/
', 'neon'));
}, [getmypid(), 3]);

/** @var Container $container */
$container = new $class;

Assert::equal('https://contributte.org/', (string) $container->getService('http.request')->getUrl());
});

// No CLI mode
test(function () {
Assert::exception(function () {
new ConsoleExtension();
}, InvalidArgumentException::class, 'Provide CLI mode, e.q. Contributte\Console\DI\ConsoleExtension(%consoleMode%).');
});

0 comments on commit d56a4ae

Please sign in to comment.