Passed
Pull Request — master (#30)
by Baptiste
03:36 queued 01:24
created

Behapi::load()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 18
nc 1
nop 2
1
<?php
2
namespace Behapi;
3
4
use Behat\Testwork\ServiceContainer\Extension;
5
use Behat\Testwork\ServiceContainer\ExtensionManager;
6
use Behat\Testwork\Cli\ServiceContainer\CliExtension;
7
8
use Behat\Behat\HelperContainer\ServiceContainer\HelperContainerExtension;
9
10
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
11
12
use Symfony\Component\DependencyInjection\Reference;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
15
use Behapi\Tools\Debug;
16
use Behapi\Tools\HttpHistory as History;
17
18
use Behapi\Cli\DebugController;
19
use Behapi\EventListener\DebugHttp;
20
use Behapi\EventListener\HttpHistory;
21
22
/**
23
 * Extension which feeds the dependencies of behapi's features
24
 *
25
 * @author Baptiste Clavié <[email protected]>
26
 */
27
class Behapi implements Extension
28
{
29
    /** {@inheritDoc} */
30
    public function getConfigKey()
31
    {
32
        return 'behapi';
33
    }
34
35
    /** {@inheritDoc} */
36
    public function configure(ArrayNodeDefinition $builder)
37
    {
38
        $builder
39
            ->children()
40
                ->scalarNode('base_url')
41
                    ->isRequired()
42
                    ->cannotBeEmpty()
43
                ->end()
44
45
                ->arrayNode('debug')
46
                    ->addDefaultsIfNotSet()
47
                    ->children()
48
                        ->scalarNode('formatter')
49
                            ->defaultValue('pretty')
50
                        ->end()
51
52
                        ->arrayNode('headers')
53
                            ->info('Headers to print in DebugHttp listener')
54
                            ->addDefaultsIfNotSet()
55
                            ->children()
56
                                ->arrayNode('request')
57
                                    ->info('Request headers to print in DebugHttp listener')
58
                                    ->defaultValue(['Content-Type'])
59
                                    ->prototype('scalar')->end()
60
                                ->end()
61
62
                                ->arrayNode('response')
63
                                    ->info('Response headers to print in DebugHttp listener')
64
                                    ->defaultValue(['Content-Type'])
65
                                    ->prototype('scalar')->end()
66
                                ->end()
67
                            ->end()
68
                        ->end()
69
                    ->end()
70
                ->end()
71
72
                ->arrayNode('twig')
73
                    ->addDefaultsIfNotSet()
74
                    ->children()
75
                        ->scalarNode('cache')
76
                            ->defaultNull()
77
                            ->beforeNormalization()
78
                                ->ifEmpty()
79
                                ->thenUnset()
80
                            ->end()
81
                            ->validate()
82
                            ->ifTrue(function ($v) { return !is_dir($v); })
83
                                ->thenInvalid('Directory does not exist')
84
                            ->end()
85
                        ->end()
86
                        ->enumNode('autoescape')
87
                            ->values([false, 'html', 'js', 'name'])
88
                            ->defaultFalse()
89
                        ->end()
90
                    ->end()
91
                ->end()
92
            ->end()
93
        ->end();
94
95
    }
96
97
    /** {@inheritDoc} */
98
    public function initialize(ExtensionManager $extensionManager)
99
    {
100
    }
101
102
    /** {@inheritDoc} */
103
    public function load(ContainerBuilder $container, array $config)
104
    {
105
        $container->register('behapi.debug', Debug::class)
106
            ->addArgument($config['debug']['headers']['request'])
107
            ->addArgument($config['debug']['headers']['response'])
108
        ;
109
110
        $container->register('behapi.history', History::class);
111
112
        $container->register('behapi.controller.debug', DebugController::class)
113
            ->addArgument(new Reference('output.manager'))
114
            ->addArgument(new Reference('behapi.debug'))
115
            ->addArgument($config['debug']['formatter'])
116
            ->addTag(CliExtension::CONTROLLER_TAG, ['priority' => 10])
117
        ;
118
119
        $container->register('behapi.subscriber.debug', DebugHttp::class)
120
            ->addArgument(new Reference('behapi.debug'))
121
            ->addArgument(new Reference('behapi.history'))
122
            ->addTag('event_dispatcher.subscriber')
123
        ;
124
125
        $container->register('behapi.subscriber.http_history', HttpHistory::class)
126
            ->addArgument(new Reference('behapi.history'))
127
            ->addTag('event_dispatcher.subscriber')
128
        ;
129
130
        $this->loadContainer($container, $config);
131
    }
132
133
    /** {@inheritDoc} */
134
    public function process(ContainerBuilder $container)
135
    {
136
    }
137
138
    private function loadContainer(ContainerBuilder $container, array $config): void
139
    {
140
        $definition = $container->register('behapi.container', Container::class);
141
142
        $definition
143
            ->addArgument(new Reference('behapi.history'))
144
            ->addArgument(new Reference('behapi.debug'))
145
            ->addArgument($config['base_url'])
146
            ->addArgument($config['twig'])
147
        ;
148
149
        $definition->addTag(HelperContainerExtension::HELPER_CONTAINER_TAG);
150
151
        if (method_exists($definition, 'setShared')) { // Symfony 2.8
152
            $definition->setShared(false);
153
        } else {
154
            $definition->setScope(ContainerBuilder::SCOPE_PROTOTYPE);
155
        }
156
    }
157
}
158