Skip to content

Commit

Permalink
Merge pull request #50 from robbieaverill/bugfix/new-config-api
Browse files Browse the repository at this point in the history
FIX Update config commands for new config API in alpha5
  • Loading branch information
robbieaverill authored Mar 6, 2017
2 parents 322a58a + 224e953 commit f1cf97e
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 308 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@
"SilverLeague\\Console\\": "src/",
"SilverLeague\\Console\\Tests\\": "tests/"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
10 changes: 3 additions & 7 deletions docs/en/commands/config-dump.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
# Command: `config:dump`

Dumps all of the processed configuration properties and their values.

You can optionally filter the type to control the source of data, for example use "yaml" to only return configuration values that were defined in YAML configuration files.

You can also add the `--filter` option with a search value to narrow the results.
Dumps all of the processed configuration properties and their values. You can optionally add the `--filter` option
with a search value to narrow the results.

## Usage

```shell
$ ssconsole config:dump [<type>] [--filter TERM]
$ ssconsole config:dump [--filter TERM]
```

## Options

| Type | Name | Required | Description | Options | Default |
| --- | --- | --- | --- | --- | --- |
| Argument | `type` | No | The configuration type to return | all, yaml, static, overrides | all |
| Option | `--filter` | No | Filter the config by a search term | _string_ | _none_ |

## Example
Expand Down
99 changes: 5 additions & 94 deletions src/Command/Config/AbstractConfigCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace SilverLeague\Console\Command\Config;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\Config\Collections\ConfigCollectionInterface;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\ConfigLoader;
use SilverStripe\Core\Object;

/**
Expand All @@ -16,102 +17,12 @@
class AbstractConfigCommand extends SilverStripeCommand
{
/**
* @var array
*/
protected $yamlConfig;

/**
* @var \SilverStripe\Core\Manifest\ConfigManifest
*/
protected $configManifest;

/**
* Gets the parsed YAML configuration array from the ConfigManifest
*
* @return array
*/
public function getYamlConfig()
{
if ($this->yamlConfig === null) {
$manifest = $this->getConfigManifest();
$this->yamlConfig = $this->getPropertyValue($manifest, 'yamlConfig');
}
return $this->yamlConfig;
}

/**
* Assemble a data-set that would be returned from ConfigStaticManifest, if it were a bit more
* useful for external API access.
*
* @return array
*/
public function getStaticConfig()
{
$output = [];
foreach (ClassInfo::subclassesFor(Object::class) as $class => $filename) {
$classConfig = [];
$reflection = new \ReflectionClass($class);
foreach ($reflection->getProperties() as $property) {
/** @var ReflectionProperty $property */
if ($property->isPrivate() && $property->isStatic()) {
$property->setAccessible(true);
$classConfig[$property->getName()] = $property->getValue();
}
}
if (!empty($classConfig)) {
$output[$class] = $classConfig;
}
}
return $output;
}

/**
* Gets the ConfigManifest from the current Config instance
* Get the SilverStripe Config manifest/collection interface
*
* @return \SilverStripe\Core\Manifest\ConfigManifest
*/
public function getConfigManifest()
{
if ($this->configManifest === null) {
$manifests = $this->getPropertyValue($this->getConfig(), 'manifests');
$this->configManifest = array_shift($manifests);
}
return $this->configManifest;
}

/**
* Gets any overrides made to the manifest
*
* @return array
*/
public function getConfigOverrides()
{
$overrides = (array) $this->getPropertyValue($this->getConfig(), 'overrides');
return !empty($overrides) ? array_shift($overrides) : [];
}

/**
* Get the SilverStripe Config model
*
* @return Config
* @return ConfigCollectionInterface
*/
public function getConfig()
{
return Config::inst();
}

/**
* Gets the value of a non-public property from the given class instance
*
* @param object $class
* @param string $propertyName
* @return mixed
*/
protected function getPropertyValue($class, $propertyName)
{
$reflectionClass = new \ReflectionClass($class);
$property = $reflectionClass->getProperty($propertyName);
$property->setAccessible(true);
return $property->getValue($class);
return ConfigLoader::instance()->getManifest();
}
}
112 changes: 24 additions & 88 deletions src/Command/Config/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverLeague\Console\Command\Config;

use SilverLeague\Console\Command\Config\AbstractConfigCommand;
use SilverStripe\Core\Convert;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -16,18 +17,6 @@
*/
class DumpCommand extends AbstractConfigCommand
{
/**
* The supported configuration sources
*
* @var array
*/
protected $configTypes = ['all', 'yaml', 'static', 'overrides'];

/**
* @var string
*/
protected $configType;

/**
* @var string|null
*/
Expand All @@ -41,13 +30,11 @@ protected function configure()
$this
->setName('config:dump')
->setDescription('Dumps all of the processed configuration properties and their values')
->addArgument('type', null, implode(', ', $this->configTypes), 'all')
->addOption('filter', null, InputOption::VALUE_REQUIRED, 'Filter the results (search)');

$this->setHelp(<<<HELP
Dumps all of the processed configuration properties and their values. You can optionally filter the type to
control the source of data, for example use "yaml" to only return configuration values that were defined in
YAML configuration files. You can also add the --filter option with a search value to narrow the results.
Dumps all of the processed configuration properties and their values. You can optionally add the --filter option
with a search value to narrow the results.
HELP
);
}
Expand All @@ -60,20 +47,9 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getArgument('type') && !in_array($input->getArgument('type'), $this->configTypes)) {
throw new \InvalidArgumentException(
sprintf(
'%s is not a valid config type, options: %s',
$input->getArgument('type'),
implode(', ', $this->configTypes)
)
);
}

$this->filter = $input->getOption('filter');
$this->configType = $input->getArgument('type');

$data = $this->getParsedOutput();
$data = $this->getParsedOutput($this->getConfig()->getAll());
if ($this->filter) {
$data = $this->filterOutput($data, $this->filter);
}
Expand All @@ -86,74 +62,31 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* Get source configuration data by the optional "type"
*
* @return array
*/
protected function getSourceData()
{
switch ($this->configType) {
case 'yaml':
$output = $this->getYamlConfig();
break;
case 'static':
$output = $this->getStaticConfig();
break;
case 'overrides':
$output = $this->getConfigOverrides();
break;
case 'all':
default:
$output = $this->getMergedData();
break;
}
return $output;
}

/**
* Merge together the config manifests data in the same manner as \SilverStripe\Core\Config\Config::getUncached
*
* @return array
*/
protected function getMergedData()
{
// Statics are the lowest priority
$output = $this->getStaticConfig();

// Then YAML is added
foreach ($this->getYamlConfig() as $class => $property) {
if (!array_key_exists($class, $output)) {
$output[$class] = [];
}
$output[$class] = array_merge($property, $output[$class]);
}

// Then overrides are added last
foreach ($this->getConfigOverrides() as $class => $values) {
foreach ($values as $property => $value) {
$output[$class][$property] = $value;
}
}

return $output;
}

/**
* Creates a table-friendly output array from the input configuration sources
* Creates a table-friendly output array from the input configuration source
*
* @param array $data
* @return array
*/
protected function getParsedOutput()
protected function getParsedOutput($data)
{
$output = [];
foreach ($this->getSourceData() as $className => $classInfo) {
foreach ($data as $className => $classInfo) {
foreach ($classInfo as $property => $values) {
$row = [$className, $property];
if (is_array($values)) {
if (is_array($values) || is_object($values)) {
foreach ($values as $key => $value) {
$row[] = is_numeric($key) ? '' : $key;
$row[] = is_array($value) ? json_encode($value, JSON_PRETTY_PRINT) : $value;
$output[] = $row;
if (is_array($value)) {
$value = Convert::raw2json($value, JSON_PRETTY_PRINT);
} elseif (is_object($value)) {
$value = get_class($value);
} else {
$value = (string) $value;
}
$row[] = $value;
if (array_filter($row) != []) {
$output[] = $row;
}
// We need the class and property data for second level values if we're filtering.
if ($this->filter !== null) {
$row = [$className, $property];
Expand All @@ -165,7 +98,10 @@ protected function getParsedOutput()
$row[] = '';
$row[] = $values;
}
$output[] = $row;

if (array_filter($row) != []) {
$output[] = $row;
}
}
}
return $output;
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Config/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$result = $this->getConfig()->get(
$input->getArgument('class'),
$input->getArgument('property')
$input->getArgument('property'),
true
);
$output->writeln(var_export($result, true));
}
Expand Down
3 changes: 3 additions & 0 deletions src/Command/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function getCommandFromTask(BuildTask $task)
public function getCommandName(BuildTask $task)
{
$taskSegment = Config::inst()->get(get_class($task), 'segment');
if (empty($taskSegment)) {
$taskSegment = $task->class;
}
$segment = strtolower(preg_replace('/(?<=[a-z])([A-Z]+)/', '-$1', $taskSegment));
// We don't really need "-task" on the end of every task.
if (substr($segment, -5, 5) === '-task') {
Expand Down
3 changes: 2 additions & 1 deletion src/Command/SilverStripeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverLeague\Console\Command;

use SilverStripe\Config\Collections\ConfigCollectionInterface;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function getInjector()
/**
* Get the configuration API handler
*
* @return Config
* @return ConfigCollectionInterface
*/
public function getConfig()
{
Expand Down
3 changes: 3 additions & 0 deletions src/Framework/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function initialize()
*/
protected function findSilverStripe()
{
if (defined('SILVERSTRIPE_ROOT_DIR')) {
return true;
}
foreach ([getcwd(), CONSOLE_BASE_DIR . '/../', CONSOLE_BASE_DIR . '/silverstripe'] as $rootFolder) {
if (file_exists($rootFolder . '/framework/src/Core/Core.php')) {
define('SILVERSTRIPE_ROOT_DIR', $rootFolder);
Expand Down
Loading

0 comments on commit f1cf97e

Please sign in to comment.