Completed
Push — develop ( 8fda15...dbbcf5 )
by Jaap
14s
created

phpDocumentor/Configuration/ServiceProvider.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2018 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\Configuration;
13
14
use Cilex\Application;
15
use Doctrine\Common\Annotations\AnnotationReader;
16
use Pimple\Container;
17
use Pimple\ServiceProviderInterface;
18
use Symfony\Component\Console\Application as ConsoleApplication;
19
use Symfony\Component\Console\Input\InputOption;
20
21
/**
22
 * Provides a series of services in order to handle the configuration for phpDocumentor.
23
 *
24
 * This class is responsible for registering a 'Merger' service that is used to combine several configuration
25
 * definitions into one and will add a new option `config` to all commands of phpDocumentor.
26
 *
27
 * Exposed services:
28
 *
29
 * - 'config', the configuration service containing all options and parameters for phpDocumentor.
30
 * - 'config.merger', a service used to combine the configuration template with the user configuration file (phpdoc.xml
31
 *   of phpdoc.dist.xml).
32
 *
33
 * The following variables are exposed:
34
 *
35
 * - 'config.path.template', the location of the configuration template with defaults.
36
 * - 'config.path.user', the location of the user configuration file that will be merged with the template.
37
 * - 'config.class', the class name of the root configuration object.
38
 */
39
class ServiceProvider implements ServiceProviderInterface
40
{
41
    /**
42
     * Adds the Configuration object to the DIC.
43
     *
44
     * phpDocumentor first loads the template config file (/data/phpdoc.tpl.xml)
45
     * and then the phpdoc.dist.xml, or the phpdoc.xml if it exists but not both,
46
     * from the current working directory.
47
     *
48
     * The user config file (either phpdoc.dist.xml or phpdoc.xml) is merged
49
     * with the template file.
50
     *
51
     * @param Container $app An Application instance
52
     */
53
    public function register(Container $app)
54
    {
55
        $this->addMerger($app);
0 ignored issues
show
$app of type object<Pimple\Container> is not a sub-type of object<Cilex\Application>. It seems like you assume a child class of the class Pimple\Container to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
56
57
        $app->extend(
58
            'console',
59
            function (ConsoleApplication $console) {
60
                $console->getDefinition()->addOption(
61
                    new InputOption(
62
                        'config',
63
                        'c',
64
                        InputOption::VALUE_OPTIONAL,
65
                        'Location of a custom configuration file'
66
                    )
67
                );
68
69
                return $console;
70
            }
71
        );
72
73
        $app['config.path.template'] = __DIR__ . '/Resources/phpdoc.tpl.xml';
74
        $app['config.path.user'] = getcwd()
75
            . ((file_exists(getcwd() . '/phpdoc.xml')) ? '/phpdoc.xml' : '/phpdoc.dist.xml');
76
        $app['config.class'] = 'phpDocumentor\Configuration';
77
78
        $app['config'] = function ($app) {
79
            $loader = new Loader($app['serializer'], $app['config.merger']);
80
81
            return $loader->load($app['config.path.template'], $app['config.path.user'], $app['config.class']);
82
        };
83
    }
84
85
    /**
86
     * Initializes and adds the configuration merger object as the 'config.merger' service to the container.
87
     */
88
    private function addMerger(Application $container)
89
    {
90
        $this->addMergerAnnotations($container);
91
92
        $container['config.merger'] = function () {
93
            return new Merger(new AnnotationReader());
94
        };
95
    }
96
97
    /**
98
     * Adds the annotations for the Merger component to the Serializer.
99
     *
100
     * @throws RuntimeException if the annotation handler for Jms Serializer is not added to the container as
101
     * 'serializer.annotations' service.
102
     */
103
    private function addMergerAnnotations(Application $container)
104
    {
105
        if (!isset($container['serializer.annotations'])) {
106
            throw new \RuntimeException(
107
                'The configuration service provider depends on the JmsSerializer Service Provider but the '
108
                . '"serializer.annotations" key could not be found in the container.'
109
            );
110
        }
111
112
        $annotations = $container['serializer.annotations'];
113
        $annotations[] = [
114
            'namespace' => 'phpDocumentor\Configuration\Merger\Annotation',
115
            'path' => __DIR__ . '/../../',
116
        ];
117
        $container['serializer.annotations'] = $annotations;
118
    }
119
}
120