Completed
Push — develop ( 722f70...af048b )
by Jaap
15:12 queued 05:04
created

ServiceProvider   C

Complexity

Total Complexity 5

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 23

Importance

Changes 0
Metric Value
dl 0
loc 209
rs 5.5
c 0
b 0
f 0
wmc 5
lcom 0
cbo 23

2 Methods

Rating   Name   Duplication   Size   Complexity  
B register() 0 152 3
B provideTemplatingSystem() 0 38 2
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Transformer;
13
14
use Cilex\Application;
15
use Cilex\ServiceProviderInterface;
16
use phpDocumentor\Compiler\Compiler;
17
use phpDocumentor\Compiler\Linker\Linker;
18
use phpDocumentor\Compiler\Pass\Debug;
19
use phpDocumentor\Compiler\Pass\ElementsIndexBuilder;
20
use phpDocumentor\Compiler\Pass\ExampleTagsEnricher;
21
use phpDocumentor\Compiler\Pass\NamespaceTreeBuilder;
22
use phpDocumentor\Compiler\Pass\PackageTreeBuilder;
23
use phpDocumentor\Compiler\Pass\MarkerFromTagsExtractor;
24
use phpDocumentor\Compiler\Pass\ResolveInlineLinkAndSeeTags;
25
use phpDocumentor\Descriptor\ProjectDescriptorBuilder;
26
use phpDocumentor\Event\Dispatcher;
27
use phpDocumentor\Fileset\Collection;
28
use phpDocumentor\Transformer\Command\Project\TransformCommand;
29
use phpDocumentor\Transformer\Command\Template\ListCommand;
30
use phpDocumentor\Transformer\Event\PreTransformEvent;
31
use phpDocumentor\Transformer\Template\Factory;
32
use phpDocumentor\Transformer\Template\PathResolver;
33
34
/**
35
 * This provider is responsible for registering the transformer component with the given Application.
36
 */
37
class ServiceProvider extends \stdClass implements ServiceProviderInterface
0 ignored issues
show
Complexity introduced by
The class ServiceProvider has a coupling between objects value of 25. Consider to reduce the number of dependencies under 13.
Loading history...
38
{
39
    /**
40
     * Registers services on the given app.
41
     *
42
     * @param Application $app An Application instance.
43
     *
44
     * @throws Exception\MissingDependencyException if the application does not have a descriptor.builder service.
45
     * @throws Exception\MissingDependencyException if the application does not have a serializer service.
46
     */
47
    public function register(Application $app)
48
    {
49
        if (!isset($app['descriptor.builder'])) {
50
            throw new Exception\MissingDependencyException(
51
                'The builder object that is used to construct the ProjectDescriptor is missing'
52
            );
53
        }
54
        if (!isset($app['serializer'])) {
55
            throw new Exception\MissingDependencyException(
56
                'The serializer object that is used to read the template configuration with is missing'
57
            );
58
        }
59
60
        // parameters
61
        $app['linker.substitutions'] = array(
62
            'phpDocumentor\Descriptor\ProjectDescriptor'      => array('files'),
63
            'phpDocumentor\Descriptor\FileDescriptor'         => array(
64
                'tags',
65
                'classes',
66
                'interfaces',
67
                'traits',
68
                'functions',
69
                'constants'
70
            ),
71
            'phpDocumentor\Descriptor\ClassDescriptor'        => array(
72
                'tags',
73
                'parent',
74
                'interfaces',
75
                'constants',
76
                'properties',
77
                'methods',
78
                'usedTraits',
79
            ),
80
            'phpDocumentor\Descriptor\InterfaceDescriptor'       => array(
81
                'tags',
82
                'parent',
83
                'constants',
84
                'methods',
85
            ),
86
            'phpDocumentor\Descriptor\TraitDescriptor'           => array(
87
                'tags',
88
                'properties',
89
                'methods',
90
                'usedTraits',
91
            ),
92
            'phpDocumentor\Descriptor\FunctionDescriptor'        => array('tags', 'arguments'),
93
            'phpDocumentor\Descriptor\MethodDescriptor'          => array('tags', 'arguments'),
94
            'phpDocumentor\Descriptor\ArgumentDescriptor'        => array('types'),
95
            'phpDocumentor\Descriptor\PropertyDescriptor'        => array('tags', 'types'),
96
            'phpDocumentor\Descriptor\ConstantDescriptor'        => array('tags', 'types'),
97
            'phpDocumentor\Descriptor\Tag\ParamDescriptor'       => array('types'),
98
            'phpDocumentor\Descriptor\Tag\ReturnDescriptor'      => array('types'),
99
            'phpDocumentor\Descriptor\Tag\SeeDescriptor'         => array('reference'),
100
            'phpDocumentor\Descriptor\Type\CollectionDescriptor' => array('baseType', 'types', 'keyTypes'),
101
        );
102
103
        // services
104
        $app['compiler'] = $app->share(
105
            function ($container) {
106
                $compiler = new Compiler();
107
                $compiler->insert(new ElementsIndexBuilder(), ElementsIndexBuilder::COMPILER_PRIORITY);
108
                $compiler->insert(new MarkerFromTagsExtractor(), MarkerFromTagsExtractor::COMPILER_PRIORITY);
109
                $compiler->insert(
110
                    new ExampleTagsEnricher($container['parser.example.finder']),
111
                    ExampleTagsEnricher::COMPILER_PRIORITY
112
                );
113
                $compiler->insert(new PackageTreeBuilder(), PackageTreeBuilder::COMPILER_PRIORITY);
114
                $compiler->insert(new NamespaceTreeBuilder(), NamespaceTreeBuilder::COMPILER_PRIORITY);
115
                $compiler->insert(
116
                    new ResolveInlineLinkAndSeeTags($container['transformer.routing.queue']),
117
                    ResolveInlineLinkAndSeeTags::COMPILER_PRIORITY
118
                );
119
                $compiler->insert($container['linker'], Linker::COMPILER_PRIORITY);
120
                $compiler->insert($container['transformer'], Transformer::COMPILER_PRIORITY);
121
                $compiler->insert(
122
                    new Debug($container['monolog'], $container['descriptor.analyzer']),
123
                    Debug::COMPILER_PRIORITY
124
                );
125
126
                return $compiler;
127
            }
128
        );
129
130
        $app['linker'] = $app->share(
131
            function ($app) {
132
                return new Linker($app['linker.substitutions']);
133
            }
134
        );
135
136
        $app['transformer.behaviour.collection'] = $app->share(
137
            function () {
138
                return new Behaviour\Collection();
139
            }
140
        );
141
142
        $app['transformer.routing.standard'] = $app->share(
143
            function ($container) {
144
                /** @var ProjectDescriptorBuilder $projectDescriptorBuilder */
145
                $projectDescriptorBuilder = $container['descriptor.builder'];
146
147
                return new Router\StandardRouter($projectDescriptorBuilder);
148
            }
149
        );
150
        $app['transformer.routing.external'] = $app->share(
151
            function ($container) {
152
                return new Router\ExternalRouter($container['config']);
153
            }
154
        );
155
156
        $app['transformer.routing.queue'] = $app->share(
157
            function ($container) {
158
                $queue = new Router\Queue();
159
160
                // TODO: load from app configuration instead of hardcoded
161
                $queue->insert($container['transformer.routing.external'], 10500);
162
                $queue->insert($container['transformer.routing.standard'], 10000);
163
164
                return $queue;
165
            }
166
        );
167
168
        $app['transformer.writer.collection'] = $app->share(
169
            function ($container) {
170
                return new Writer\Collection($container['transformer.routing.queue']);
171
            }
172
        );
173
174
        $this->provideTemplatingSystem($app);
175
176
        $app['transformer'] = $app->share(
177
            function ($container) {
178
                $transformer = new Transformer(
179
                    $container['transformer.template.collection'],
180
                    $container['transformer.writer.collection']
181
                );
182
183
                /** @var Behaviour\Collection $behaviourCollection */
184
                $behaviourCollection = $container['transformer.behaviour.collection'];
185
                Dispatcher::getInstance()->addListener(
186
                    Transformer::EVENT_PRE_TRANSFORM,
187
                    function (PreTransformEvent $event) use ($behaviourCollection) {
188
                        $behaviourCollection->process($event->getProject());
189
                    }
190
                );
191
192
                return $transformer;
193
            }
194
        );
195
196
        $app->command(new TransformCommand($app['descriptor.builder'], $app['transformer'], $app['compiler']));
197
        $app->command(new ListCommand($app['transformer.template.factory']));
198
    }
199
200
    /**
201
     * Initializes the templating system in the container.
202
     *
203
     * @param Application $app
204
     *
205
     * @return void
206
     */
207
    protected function provideTemplatingSystem(Application $app)
208
    {
209
        $templateDir = __DIR__ . '/../../../data/templates';
210
211
        // when installed using composer the templates are in a different folder
212
        $composerTemplatePath = __DIR__ . '/../../../../templates';
213
        if (file_exists($composerTemplatePath)) {
214
            $templateDir = $composerTemplatePath;
215
        }
216
217
        // parameters
218
        $app['transformer.template.location'] = $templateDir;
219
220
        // services
221
        $app['transformer.template.path_resolver'] = $app->share(
222
            function ($container) {
223
                return new PathResolver($container['transformer.template.location']);
224
            }
225
        );
226
227
        $app['transformer.template.factory'] = $app->share(
228
            function ($container) {
229
                return new Factory(
230
                    $container['transformer.template.path_resolver'],
231
                    $container['serializer']
232
                );
233
            }
234
        );
235
236
        $app['transformer.template.collection'] = $app->share(
237
            function ($container) {
238
                return new Template\Collection(
239
                    $container['transformer.template.factory'],
240
                    $container['transformer.writer.collection']
241
                );
242
            }
243
        );
244
    }
245
}
246