-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: improve helperSet + own helpers
Ability to define helperSet as service or directly, port ContainerHelper from Kdyby\Console, include tests for helperSet + helpers.
- Loading branch information
Showing
9 changed files
with
350 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
|
||
} |
Oops, something went wrong.