Skip to content

Commit

Permalink
Feature: improve helperSet + own helpers
Browse files Browse the repository at this point in the history
Ability to define helperSet as service or directly, port ContainerHelper from Kdyby\Console, include tests for helperSet + helpers.
  • Loading branch information
f3l1x committed May 21, 2017
1 parent 79b4a74 commit 9fe2639
Show file tree
Hide file tree
Showing 9 changed files with 350 additions and 7 deletions.
31 changes: 31 additions & 0 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ console:
autoExit: true / false
url: https://contributte.com
helperSet: @customHelperSet
helpers:
- 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.
Expand All @@ -37,6 +39,35 @@ console:
url: https://contributte.org
```

You could also define you custom `helperSet` just in case. There are 2 possible approaches. You can register your
`App\Model\MyCustomHelperSet` as services under `services` section or provide it directly to extesion config `helperSet`.

Already defined service:

```yaml
services:
customHelperSet: App\Model\MyCustomHelperSet

console:
helperSet: @customHelperSet
```

Directly defined helperSet:

```yaml
console:
helperSet: App\Model\MyCustomHelperSet
```

By default helperSet contains 5 helpers, 4 defined in `Symfony\Component\Console\Application` by default and 1 defined
by extension itself. In case of need you're able to add more helpers.

```yaml
console:
helpers:
- App\Model\MyReallyGreatHelper
```

## Command

```php
Expand Down
28 changes: 27 additions & 1 deletion src/DI/ConsoleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace Contributte\Console\DI;

use Contributte\Console\Application;
use Contributte\Console\Helper\ContainerHelper;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Statement;
use Nette\Http\Request;
use Nette\Http\UrlScript;
use Nette\Utils\Strings;
use Nette\Utils\Validators;
use Symfony\Component\Console\Command\Command;

/**
Expand All @@ -23,6 +27,9 @@ class ConsoleExtension extends CompilerExtension
'catchExceptions' => NULL,
'autoExit' => NULL,
'helperSet' => NULL,
'helpers' => [
ContainerHelper::class,
],
];

/**
Expand All @@ -38,6 +45,8 @@ public function loadConfiguration()
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);

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

$application = $builder->addDefinition($this->prefix('application'))
->setClass(Application::class);

Expand All @@ -58,7 +67,24 @@ public function loadConfiguration()
}

if ($config['helperSet'] !== NULL) {
$application->addSetup('setHelperSet', [$config['helperSet']]);
if (is_string($config['helperSet']) && Strings::startsWith($config['helperSet'], '@')) {
// Add already defined service
$application->addSetup('setHelperSet', [$config['helperSet']]);
} else {
// Parse service definition
$helperSetDef = $builder->addDefinition($this->prefix('helperSet'));
Compiler::loadDefinition($helperSetDef, $config['helperSet']);
$application->addSetup('setHelperSet', [$helperSetDef]);
}
}

if (is_array($config['helpers'])) {
$helpers = 1;
foreach ($config['helpers'] as $helper) {
$helperDef = $builder->addDefinition($this->prefix('helper.' . $helpers++));
Compiler::loadDefinition($helperDef, $helper);
$application->addSetup(new Statement('$service->getHelperSet()->set(?)', [$helperDef]));
}
}
}

Expand Down
81 changes: 81 additions & 0 deletions src/Helper/ContainerHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Contributte\Console\Helper;

use Nette\DI\Container;
use Symfony\Component\Console\Helper\Helper;

/**
* @author Milan Felix Sulc <sulcmil@gmail.com>
* @author Filip Procházka <filip@prochazka.su>
*/
class ContainerHelper extends Helper
{

/** @var Container */
private $container;

/**
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}

/**
* @param string $key
* @return bool
*/
public function hasParameter($key)
{
return isset($this->container->parameters[$key]);
}

/**
* @param string $key
* @return mixed
*/
public function getParameter($key)
{
if (!$this->hasParameter($key)) {
return NULL;
}

return $this->container->parameters[$key];
}

/**
* @return array
*/
public function getParameters()
{
return $this->container->parameters;
}

/**
* @return Container
*/
public function getContainer()
{
return $this->container;
}

/**
* @param string $type
* @return object
*/
public function getByType($type)
{
return $this->container->getByType($type);
}

/**
* @return string
*/
public function getName()
{
return 'container';
}

}
44 changes: 44 additions & 0 deletions tests/cases/Command/Command.HelperSet.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* Test: Command\AbstractCommand.HelperSet
*/

use Contributte\Console\DI\ConsoleExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Helper\HelperSet;
use Tester\Assert;
use Tester\FileMock;
use Tests\Fixtures\HelperSetCommand;

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

// Test auto filling helperSet in command
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->loadConfig(FileMock::create('
services:
- Tests\Fixtures\HelperSetCommand
', 'neon'));
}, [microtime(), 2]);

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

/** @var Application $application */
$application = $container->getByType(Application::class);

/** @var HelperSetCommand $command */
$command = $container->getByType(HelperSetCommand::class);

$application->setDefaultCommand($command->getName(), TRUE);
$application->setAutoExit(FALSE);
$application->run();

Assert::type(HelperSet::class, $command->getHelperSet());
});
90 changes: 90 additions & 0 deletions tests/cases/DI/ConsoleExtension.HelperSet.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* Test: DI\ConsoleExtension.HelperSet
*/

use Contributte\Console\DI\ConsoleExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Symfony\Component\Console\Application;
use Tester\Assert;
use Tester\FileMock;

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

// Default helperSet
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
}, [microtime(), 1]);

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

// 4 default helpers
// 1 container helper
Assert::count(5, $container->getByType(Application::class)->getHelperSet()->getIterator());
});

// Own helperSet
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->loadConfig(FileMock::create('
console:
helperSet: Tests\Fixtures\FooHelperSet
', 'neon'));
}, [microtime(), 3]);

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

// 1 container helper
Assert::count(1, $container->getByType(Application::class)->getHelperSet()->getIterator());
});

// Own helperSet as service
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->loadConfig(FileMock::create('
console:
helperSet: @Tests\Fixtures\FooHelperSet
services:
- Tests\Fixtures\FooHelperSet
', 'neon'));
}, [microtime(), 3]);

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

// 1 container helper
Assert::count(1, $container->getByType(Application::class)->getHelperSet()->getIterator());
});

// Own helper
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
$compiler->addExtension('console', new ConsoleExtension());
$compiler->loadConfig(FileMock::create('
console:
helpers:
- Tests\Fixtures\FooHelper
', 'neon'));
}, [microtime(), 4]);

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

// 4 default helpers
// 1 container helper
// 1 foo helper
Assert::count(6, $container->getByType(Application::class)->getHelperSet()->getIterator());
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Assert;
use Tester\FileMock;
use Tests\Fixtures\FooCommand;

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

// No commands
test(function () {
$loader = new ContainerLoader(TEMP_DIR, TRUE);
$class = $loader->load(function (Compiler $compiler) {
Expand All @@ -26,15 +28,15 @@ test(function () {
Assert::count(0, $container->findByType(AbstractCommand::class));
});


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

/** @var Container $container */
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/FooHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Fixtures;

use Symfony\Component\Console\Helper\Helper;

/**
* @author Milan Felix Sulc <sulcmil@gmail.com>
*/
class FooHelper extends Helper
{

/**
* @return string The canonical name
*/
public function getName()
{
return 'foo';
}

}
13 changes: 13 additions & 0 deletions tests/fixtures/FooHelperSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tests\Fixtures;

use Symfony\Component\Console\Helper\HelperSet;

/**
* @author Milan Felix Sulc <sulcmil@gmail.com>
*/
class FooHelperSet extends HelperSet
{

}

0 comments on commit 9fe2639

Please sign in to comment.