Skip to content

Commit

Permalink
Merge pull request #31 from kschroeder/develop
Browse files Browse the repository at this point in the history
Oh geez, two things.  Added configuration:list-keys and made configur…
  • Loading branch information
kschroeder committed Feb 17, 2017
2 parents ee596d6 + 629a9f7 commit b4560df
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 111 deletions.
31 changes: 20 additions & 11 deletions lib/Config/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,30 @@ public function getRegisteredConfigurationFiles()
*/

public function build($context = Config::CONTEXT_DEFAULT, ConfigInterface $config = null)
{

if (!$config instanceof Config) {
$config = new Config('<config />');
}

$structure = $this->getMergedStructure();

if (!$structure instanceof \SimpleXMLElement) {
throw new InvalidConfigurationException('No configuration files provided');
}

$this->buildConfigurationObject($structure, $config, $context);

return $config;
}

public function getMergedStructure()
{
$files = $this->getRegisteredConfigurationFiles();
if (!count($files)) {
throw new MissingConfigurationException('No configuration files have been provided. Please add via registerConfigurationFile()');
}

if (!$config instanceof Config) {
$config = new Config('<config />');
}
$structure = null;
foreach ($files as $file) {
if (!$file instanceof AdapterInterface) {
Expand All @@ -93,16 +108,10 @@ public function build($context = Config::CONTEXT_DEFAULT, ConfigInterface $confi
$this->mergeStructure($structure, $simpleXml);
}
}

if (!$structure instanceof \SimpleXMLElement) {
throw new InvalidConfigurationException('No configuration files provided');
}

$this->buildConfigurationObject($structure, $config, $context);

return $config;
return $structure;
}


/**
* @param \SimpleXMLElement $structure The object representing the merged configuration structure
* @param \SimpleXmlElement $config An empty config object to be populated
Expand Down
12 changes: 12 additions & 0 deletions lib/Config/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
interface BuilderInterface
{

/**
* @param string $context
* @param ConfigInterface|null $config
* @return Config
*/

public function build($context = Config::CONTEXT_DEFAULT, ConfigInterface $config = null);

/**
* @return \SimpleXMLElement
*/

public function getMergedStructure();

}
14 changes: 1 addition & 13 deletions lib/Console/Command/ConfigurationBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class ConfigurationBuild extends Command
{
use ConfigurationFactoryTrait;

const COMMAND = 'magium:configuration:build';

Expand All @@ -30,19 +31,6 @@ protected function configure()
$this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context (ignore to build all contexts)');
}

public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
{
$this->configurationFactory = $factory;
}

protected function getConfigurationFactory()
{
if (!$this->configurationFactory instanceof MagiumConfigurationFactoryInterface) {
$this->configurationFactory = new MagiumConfigurationFactory();
}
return $this->configurationFactory;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$factory = $this->getConfigurationFactory();
Expand Down
23 changes: 23 additions & 0 deletions lib/Console/Command/ConfigurationFactoryTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Magium\Configuration\Console\Command;

use Magium\Configuration\MagiumConfigurationFactory;
use Magium\Configuration\MagiumConfigurationFactoryInterface;

trait ConfigurationFactoryTrait
{

public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
{
$this->factory = $factory;
}

protected function getConfigurationFactory()
{
if (!$this->factory instanceof MagiumConfigurationFactoryInterface) {
$this->factory = new MagiumConfigurationFactory();
}
return $this->factory;
}
}
15 changes: 2 additions & 13 deletions lib/Console/Command/ConfigurationGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
class ConfigurationGet extends Command
{

use ConfigurationFactoryTrait;

const COMMAND = 'magium:configuration:get';

protected $factory;
Expand All @@ -31,19 +33,6 @@ protected function configure()
$this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', Config::CONTEXT_DEFAULT);
}

public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
{
$this->factory = $factory;
}

protected function getConfigurationFactory()
{
if (!$this->factory instanceof MagiumConfigurationFactoryInterface) {
$this->factory = new MagiumConfigurationFactory();
}
return $this->factory;
}

protected function getValueFlag(ConfigInterface $config, $path)
{
$value = $config->getValueFlag($path);
Expand Down
35 changes: 0 additions & 35 deletions lib/Console/Command/ConfigurationList.php

This file was deleted.

69 changes: 69 additions & 0 deletions lib/Console/Command/ConfigurationListKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Magium\Configuration\Console\Command;

use Magium\Configuration\Config\Config;
use Magium\Configuration\MagiumConfigurationFactory;
use Magium\Configuration\MagiumConfigurationFactoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConfigurationListKeys extends Command
{

const COMMAND = 'magium:configuration:list-keys';

const INITIAL_MESSAGE = 'Valid configuration keys';

protected $factory;

protected function configure()
{
$this
->setName(self::COMMAND)
->setDescription('List configuration settings')
->setHelp("This command lists all of the configuration setting options")
;
}

public function getConfigurationFactory()
{
if (!$this->factory instanceof MagiumConfigurationFactoryInterface) {
$this->factory = new MagiumConfigurationFactory();
}
return $this->factory;
}

public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
{
$this->factory = $factory;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$factory = $this->getConfigurationFactory();
$builder = $factory->getBuilder();
$merged = $builder->getMergedStructure();
$merged->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration');
$elements = $merged->xpath('//s:element');
$output->writeln(self::INITIAL_MESSAGE);
foreach ($elements as $element) {
$elementId = (string)$element['id'];
$parent = $element->xpath('..');
$groupId = (string)$parent[0]['id'];
$parent = $parent[0]->xpath('..');
$sectionId = (string)$parent[0]['id'];
$description = '';
if (isset($element->description)) {
$description = sprintf(' (%s)', (string)$element->description);
}

$out = sprintf('%s/%s/%s%s', $sectionId, $groupId, $elementId, $description);
$output->writeln($out);

}
}

}
27 changes: 15 additions & 12 deletions lib/Console/Command/ConfigurationSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class ConfigurationSet extends Command
{
use ConfigurationFactoryTrait;

const COMMAND = 'magium:configuration:set';

Expand All @@ -32,18 +33,6 @@ protected function configure()
$this->addArgument('context', InputArgument::OPTIONAL, 'Configuration Context', Config::CONTEXT_DEFAULT);
}

public function setConfigurationFactory(MagiumConfigurationFactoryInterface $factory)
{
$this->factory = $factory;
}

protected function getConfigurationFactory()
{
if (!$this->factory instanceof MagiumConfigurationFactoryInterface) {
$this->factory = new MagiumConfigurationFactory();
}
return $this->factory;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
Expand All @@ -52,6 +41,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
$path = $input->getArgument('path');
$value = $input->getArgument('value');
$context = $input->getArgument('context');

$structure = $factory->getBuilder()->getMergedStructure();
$structure->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration');
$paths = explode('/', $path);
$xpath = '/';
foreach ($paths as $pathName) {
$xpath .= sprintf('/s:*[@id="%s"]', $pathName);
}

$results = $structure->xpath($xpath);
if (!$results) {
throw new UnconfiguredPathException(sprintf('Path (%s) is not configured. Do you need to create a configuration file?', $path));
}

$builderFactory->getPersistence()->setValue($path, $value, $context);

$out = sprintf("Set %s to %s (context: %s)", $path, $value, $context);
Expand Down
4 changes: 3 additions & 1 deletion lib/Console/Command/ContextList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class ContextList extends Command
{
use ConfigurationFactoryTrait;

const TAB = ' ';
const COMMAND = 'magium:configuration:list-contexts';

Expand All @@ -27,7 +29,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Context List');
$output->writeln(['Following is list of contexts shown by inheritance', '']);
$factory = new MagiumConfigurationFactory();
$factory = $this->getConfigurationFactory();
$contextFile = $factory->getContextFile();
$context = $contextFile->toXml();
$output->writeln($this->formatNode('default', 'Default'));
Expand Down
4 changes: 3 additions & 1 deletion lib/Console/Command/CreateTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class CreateTable extends Command
{

use ConfigurationFactoryTrait;

const COMMAND = 'magium:configuration:create-table';

protected function configure()
Expand All @@ -29,7 +31,7 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Creating configuration table...');
$factory = new MagiumConfigurationFactory();
$factory = $this->getConfigurationFactory();
$persistence = $factory->getBuilderFactory()->getPersistence();
try {
$persistence->create();
Expand Down
5 changes: 5 additions & 0 deletions lib/Console/Command/UnconfiguredPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Magium\Configuration\Console\Command;

class UnconfiguredPathException extends \Exception {}
3 changes: 2 additions & 1 deletion lib/Console/Symfony/CommandList.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magium\Configuration\Console\Command\ConfigurationBuild;
use Magium\Configuration\Console\Command\ConfigurationGet;
use Magium\Configuration\Console\Command\ConfigurationList;
use Magium\Configuration\Console\Command\ConfigurationListKeys;
use Magium\Configuration\Console\Command\ConfigurationSet;
use Magium\Configuration\Console\Command\ContextList;
use Magium\Configuration\Console\Command\CreateTable;
Expand All @@ -20,7 +21,7 @@ public function addCommands(Application $application)
$application->add(new ConfigurationBuild());
$application->add(new ConfigurationGet());
$application->add(new ConfigurationSet());
$application->add(new ConfigurationList());
$application->add(new ConfigurationListKeys());
$application->add(new ContextList());
$application->add(new CreateTable());
$application->setDefaultCommand(DefaultCommand::COMMAND);
Expand Down

0 comments on commit b4560df

Please sign in to comment.