TokensExtension::loadTokensReplacementStepTester()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author Alexei Gorobet <[email protected]>
5
 */
6
7
namespace Behat\TokensExtension\ServiceContainer;
8
9
use Behat\Behat\Context\ServiceContainer\ContextExtension;
10
use Behat\Behat\Definition\ServiceContainer\DefinitionExtension;
11
use Behat\Behat\Tester\ServiceContainer\TesterExtension;
12
use Behat\EnvironmentLoader;
13
use Behat\Testwork\Call\ServiceContainer\CallExtension;
14
use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
15
use Behat\Testwork\ServiceContainer\Extension;
16
use Behat\Testwork\ServiceContainer\ExtensionManager;
17
use Behat\Testwork\ServiceContainer\ServiceProcessor;
18
use Behat\Testwork\Translator\ServiceContainer\TranslatorExtension;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Reference;
23
24
/**
25
 * Class TokensExtension.
26
 *
27
 * @package Behat\TokensExtension\ServiceContainer
28
 */
29
class TokensExtension implements Extension
30
{
31
    /*
32
     * Available services
33
     */
34
    const REPOSITORY_ID = 'tokens_replacement.repository';
35
36
    /*
37
     * Available extension points
38
     */
39
    const STEP_TOKENS_REPLACER_TAG = 'tokens_replacer.step';
40
41
    /**
42
     * @var ServiceProcessor
43
     */
44
    private $processor;
45
46
    /**
47
     * Initializes extension.
48
     *
49
     * @param null|ServiceProcessor $processor
50
     */
51
    public function __construct(ServiceProcessor $processor = null)
52
    {
53
        $this->processor = $processor ?: new ServiceProcessor();
54
    }
55
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getConfigKey()
61
    {
62
        return 'tokens';
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function initialize(ExtensionManager $extensionManager)
69
    {
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function configure(ArrayNodeDefinition $builder)
76
    {
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function load(ContainerBuilder $container, array $config)
83
    {
84
        $this->loadTokensReplacementStepTester($container);
85
        $this->loadPluggableStepTokensReplacer($container);
86
        $this->loadDefaultStepTokensReplacers($container);
87
        $this->loadAnnotationReader($container);
88
        $this->loadRepository($container);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function process(ContainerBuilder $container)
95
    {
96
        $this->processStepTokensReplacers($container);
97
    }
98
99
    /**
100
     * Loads default step tokens replacers.
101
     *
102
     * @param ContainerBuilder $container
103
     */
104
    protected function loadDefaultStepTokensReplacers(ContainerBuilder $container)
105
    {
106
        $definition = new Definition('Behat\TokensExtension\RepositoryStepTokensReplacer', array(
107
            new Reference(self::REPOSITORY_ID),
108
            new Reference(CallExtension::CALL_CENTER_ID),
109
        ));
110
        $definition->addTag(self::STEP_TOKENS_REPLACER_TAG, array('priority' => 50));
111
        $container->setDefinition(self::STEP_TOKENS_REPLACER_TAG . '.repository', $definition);
112
    }
113
114
    /**
115
     * Loads tokens repacement context annotation reader.
116
     *
117
     * @param ContainerBuilder $container
118
     */
119
    protected function loadAnnotationReader(ContainerBuilder $container)
120
    {
121
        $definition = new Definition('Behat\TokensExtension\Context\Annotation\TokensReplacementAnnotationReader');
122
        $definition->addTag(ContextExtension::ANNOTATION_READER_TAG, array('priority' => 50));
123
        $container->setDefinition(ContextExtension::ANNOTATION_READER_TAG . '.tokens_replacement', $definition);
124
    }
125
126
    /**
127
     * Loads tokens replacements repository.
128
     *
129
     * @param ContainerBuilder $container
130
     */
131
    protected function loadRepository(ContainerBuilder $container)
132
    {
133
        $definition = new Definition('Behat\TokensExtension\TokensReplacementsRepository', array(
134
            new Reference(EnvironmentExtension::MANAGER_ID)
135
        ));
136
        $container->setDefinition(self::REPOSITORY_ID, $definition);
137
    }
138
139
    /**
140
     * Processes all available setp tokens replacers.
141
     *
142
     * @param ContainerBuilder $container
143
     */
144
    protected function processStepTokensReplacers(ContainerBuilder $container)
145
    {
146
        $references = $this->processor->findAndSortTaggedServices($container, self::STEP_TOKENS_REPLACER_TAG);
147
        $definition = $container->getDefinition(self::STEP_TOKENS_REPLACER_TAG);
148
149
        foreach ($references as $reference) {
150
            $definition->addMethodCall('registerStepTokensReplacers', array($reference));
151
        }
152
    }
153
154
    /**
155
     * Load pluggable default step tokens replacer.
156
     *
157
     * @param ContainerBuilder $container
158
     */
159
    private function loadPluggableStepTokensReplacer(ContainerBuilder $container)
160
    {
161
        $definition = new Definition('Behat\TokensExtension\PluggableStepTokensReplacer');
162
        $container->setDefinition(self::STEP_TOKENS_REPLACER_TAG, $definition);
163
    }
164
165
    /**
166
     * Load tokens replacement step tester wrapper.
167
     *
168
     * @param ContainerBuilder $container
169
     */
170
    private function loadTokensReplacementStepTester(ContainerBuilder $container)
171
    {
172
        $definition = new Definition('Behat\TokensExtension\TokensReplacementStepTester', array(
173
            new Reference(TesterExtension::STEP_TESTER_ID),
174
            new Reference(self::STEP_TOKENS_REPLACER_TAG)
175
        ));
176
        $definition->addTag(TesterExtension::STEP_TESTER_WRAPPER_TAG, array('priority' => -999999));
177
        $container->setDefinition(TesterExtension::STEP_TESTER_WRAPPER_TAG . '.token_replacement', $definition);
178
    }
179
}
180