Completed
Pull Request — master (#50)
by Kévin
02:53
created

Configuration::getConfigTreeBuilder()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 44
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 11
Bugs 2 Features 3
Metric Value
c 11
b 2
f 3
dl 0
loc 44
rs 8.8571
cc 3
eloc 36
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) Kévin Dunglas <[email protected]>
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Dunglas\ActionBundle\DependencyInjection;
11
12
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
13
use Symfony\Component\Config\Definition\ConfigurationInterface;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
/**
18
 * {@inheritdoc}
19
 *
20
 * @author Kévin Dunglas <[email protected]>
21
 */
22
class Configuration implements ConfigurationInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function getConfigTreeBuilder()
28
    {
29
        $treeBuilder = new TreeBuilder();
30
        $treeBuilder->root('dunglas_action')
31
            ->fixXmlConfig('directory', 'directories')
32
            ->children()
33
                ->arrayNode('directories')
34
                    ->info('List of directories relative to the kernel root directory containing classes.')
35
                    ->prototype('scalar')->end()
36
                    ->defaultValue([
37
                        '../src/*Bundle/Controller',
38
                        '../src/*Bundle/Action',
39
                        '../src/*Bundle/Command',
40
                        '../src/*Bundle/EventSubscriber',
41
                    ])
42
                ->end()
43
                ->arrayNode('tags')
44
                    ->info('List of tags to add when implementing the corresponding class.')
45
                    ->useAttributeAsKey('class')
46
                    ->prototype('array')
47
                        // Converts 'console.command' to ['console.command']
48
                        ->beforeNormalization()->ifString()->then(function ($v) { return [$v]; })->end()
49
                        ->prototype('array')
50
                            // Converts 'console.command' to ['console.command', []]
51
                            ->beforeNormalization()->ifString()->then(function ($v) { return [$v, []]; })->end()
52
                            ->validate()
53
                                ->ifTrue(function ($v) {
54
                                    return count($v) !== 2 || !is_string($v[0]) || !is_array($v[1]);
55
                                })
56
                                ->thenInvalid('Invalid tag format. They must be as following: [\'my_tag.name\', [\'attribute\' => \'value\']]')
57
                            ->end()
58
                            ->prototype('variable')->end()
59
                        ->end()
60
                    ->end()
61
                    ->defaultValue([
62
                        Command::class => [['console.command', []]],
63
                        EventSubscriberInterface::class => [['kernel.event_subscriber', []]],
64
                    ])
65
                ->end()
66
            ->end()
67
        ->end();
68
69
        return $treeBuilder;
70
    }
71
}
72