Last active
August 29, 2015 14:19
-
-
Save velikanov/09b08bec7c46250cccf8 to your computer and use it in GitHub Desktop.
Can not get the array of entities config from within the Command
This file contains hidden or 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
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$container = $this->getContainer(); | |
$doctrine = $container->get('doctrine'); | |
$monitoringEntities = $container->getParameter('monitoring.entities'); | |
foreach ($monitoringEntities as $monitoringEntity) { | |
$output->writeln($monitoringEntity); | |
// let's try to get monitoring.entities.entity1 array | |
print_r($container->getParameter($monitoringEntity)); // <=-- this throws an InvalidArgumentException exception, can't find the parameter | |
} | |
} |
This file contains hidden or 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
monitoring: | |
entities: | |
entity1: | |
day_time: | |
from: 9 | |
to: 19 | |
day_time_gap: 5 | |
night_time_gap: 15 | |
entity2: | |
day_time: | |
from: 7 | |
to: 21 | |
day_time_gap: 1 | |
night_time_gap: 5 |
This file contains hidden or 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
class Configuration implements ConfigurationInterface | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getConfigTreeBuilder() | |
{ | |
$treeBuilder = new TreeBuilder(); | |
$rootNode = $treeBuilder->root('monitoring'); | |
$rootNode | |
->children() | |
->append($this->getEntitiesNode()) | |
->end() | |
; | |
return $treeBuilder; | |
} | |
private function getEntitiesNode() | |
{ | |
$treeBuilder = new TreeBuilder(); | |
$node = $treeBuilder->root('entities'); | |
$timingsNode = $node | |
->requiresAtLeastOneElement() | |
->useAttributeAsKey('name') | |
->prototype('array') | |
; | |
$timingsNode | |
->children() | |
->arrayNode('day_time') | |
->children() | |
->integerNode('from')->defaultValue(9)->end() | |
->integerNode('to')->defaultValue(19)->end() | |
->end() | |
->end() | |
->integerNode('day_time_gap')->defaultValue(5)->end() | |
->integerNode('night_time_gap')->defaultValue(15)->end() | |
->end() | |
; | |
return $node; | |
} | |
} |
This file contains hidden or 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
public function load(array $configs, ContainerBuilder $container) | |
{ | |
$configuration = new Configuration(); | |
$config = $this->processConfiguration($configuration, $configs); | |
$timings = []; | |
foreach (array_keys($config['entities']) as $name) { | |
$timings[$name] = sprintf('monitoring.entities.%s_entity', $name); | |
} | |
$container->setParameter('monitoring.entities', $timings); | |
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); | |
$loader->load('services.xml'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment