Skip to content

Commit

Permalink
added naming option
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksandr Yulin committed Sep 29, 2019
1 parent 8a4b2e5 commit b6fa90c
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 36 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
"container console runner",
"psr-11"
],
"config": {
"optimize-autoloader": true
},
"require": {
"php": "^7.3",
"psr/container": "^1.0",
"symfony/console": "^4.3.4",
"streamcommon/excess-configuration": "^1.0.8",
"zendframework/zend-stdlib": "^3.2.1"
},
"require-dev": {
Expand Down
67 changes: 57 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions src/CommandLoader/Factory/CommandLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ public function __invoke(
$componentOptions = $container->get(ComponentOptions::class);
$commandMap = [];
foreach ($componentOptions->getCommands() as $command) {
if (is_string($command) === false) {
throw new ContainerException(
sprintf(
'Expected string, got %s',
is_object($command) ? get_class($command) : gettype($command)
)
);
}
$commandMap[$command] = $command;
$commandMap[$command->getName()] = $command->getClass();
}
return new ContainerCommandLoader($container, $commandMap);
}
Expand Down
17 changes: 11 additions & 6 deletions src/ComponentOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Streamcommon\Console;

use Streamcommon\Excess\Configuration\ClassName\NamedValue;
use Zend\Stdlib\AbstractOptions;

/**
Expand All @@ -25,9 +26,9 @@ class ComponentOptions extends AbstractOptions
protected $name = 'UNKNOWN';
/** @var string */
protected $version = 'UNKNOWN';
/** @var array */
/** @var NamedValue[] */
protected $commands = [];
/** @var array */
/** @var NamedValue[] */
protected $helpers = [];
/** @var bool */
protected $catchExceptions = true;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function setVersion(string $version): ComponentOptions
/**
* Get commands
*
* @return array
* @return NamedValue[]
*/
public function getCommands(): array
{
Expand All @@ -94,14 +95,16 @@ public function getCommands(): array
*/
public function setCommands(array $commands): ComponentOptions
{
$this->commands = $commands;
$this->commands = array_map(function ($item) {
return (($item instanceof NamedValue) == true) ? $item : new NamedValue($item);
}, $commands);
return $this;
}

/**
* Get helpers
*
* @return array
* @return NamedValue[]
*/
public function getHelpers(): array
{
Expand All @@ -116,7 +119,9 @@ public function getHelpers(): array
*/
public function setHelpers(array $helpers): ComponentOptions
{
$this->helpers = $helpers;
$this->helpers = array_map(function ($item) {
return (($item instanceof NamedValue) == true) ? $item : new NamedValue($item);
}, $helpers);
return $this;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public function getConsoleRunner(): array
'name' => 'UNKNOWN',
'version' => 'UNKNOWN',
'commands' => [
//list commands
//named list commands ['name' => 'name', 'class' => 'classname']
],
'helpers' => [
// list helpers
// named list helpers ['name' => 'name', 'class' => 'classname']
],
'catch_exceptions' => true,
];
Expand Down
10 changes: 1 addition & 9 deletions src/Helper/Factory/HelperSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ public function __invoke(
$componentOptions = $container->get(ComponentOptions::class);
$helperMap = [];
foreach ($componentOptions->getHelpers() as $helper) {
if (is_string($helper) === false) {
throw new ContainerException(
sprintf(
'Expected string, got %s',
is_object($helper) ? get_class($helper) : gettype($helper)
)
);
}
$helperMap[$helper] = $helper;
$helperMap[$helper->getName()] = $helper->getClass();
}
return new ContainerHelperSet($container, $helperMap);
}
Expand Down

0 comments on commit b6fa90c

Please sign in to comment.