Completed
Push — develop ( 3bf98d...ad187c )
by Jaap
06:26
created

Application::registerPlugins()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 7
nop 1
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phpDocumentor.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @copyright 2010-2016 Mike van Riel<[email protected]>
9
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10
 * @link      http://phpdoc.org
11
 */
12
13
namespace phpDocumentor\Application;
14
15
use DI\ContainerBuilder;
16
use phpDocumentor\Application\Console\Input\ArgvInput;
17
use Symfony\Component\Console\Application as ConsoleApplication;
18
19
/**
20
 * Application class for phpDocumentor.
21
 *
22
 * Can be used as bootstrap when the run method is not invoked.
23
 */
24
final class Application
25
{
26
    /**
27
     * Initializes all components used by phpDocumentor.
28
     *
29
     * @param array $values
30
     */
31
    public function __construct(
32
        array $values = [],
33
        $dependencyInjectionDefinitions = [__DIR__ . '/ContainerDefinitions.php']
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $dependencyInjectionDefinitions exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
34
    ) {
35
        $this->disableGarbageCollection();
36
        $this->setTimezone();
37
        $this->removePhpMemoryLimit();
38
39
        $container = $this->createContainer($values, $dependencyInjectionDefinitions);
40
        $this->console = $container->get(ConsoleApplication::class);
0 ignored issues
show
Bug introduced by
The property console does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
    }
42
43
    /**
44
     * Run the application and if no command is provided, use project:run.
45
     *
46
     * @return integer The exit code for this application
47
     */
48
    public function run()
49
    {
50
        $this->console->setAutoExit(false);
51
52
        return $this->console->run(new ArgvInput());
53
    }
54
55
    /**
56
     * phpDocumentor creates large nested, and sometimes recursive, data structures; by disabling garbage collection
57
     * we lose some memory efficiency but gain performance.
58
     *
59
     * The trade-off between memory efficiency and performance was made because this is not part of a long running
60
     * process. Should the application ever be used as a daemon then this decision should be revisited.
61
     *
62
     * @return void
63
     */
64
    private function disableGarbageCollection()
65
    {
66
        gc_disable();
67
    }
68
69
    /**
70
     * If the timezone is not set anywhere, set it to UTC.
71
     *
72
     * This is done to prevent any warnings being outputted in relation to using
73
     * date/time functions. What is checked is php.ini, and if the PHP version
74
     * is prior to 5.4, the TZ environment variable.
75
     *
76
     * @link http://php.net/manual/en/function.date-default-timezone-get.php for more information how PHP determines the
77
     *     default timezone.
78
     *
79
     * @return void
80
     */
81
    private function setTimezone()
82
    {
83
        if (false == ini_get('date.timezone')
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing ini_get('date.timezone') of type string to the boolean false. If you are specifically checking for an empty string, consider using the more explicit === '' instead.
Loading history...
84
            || (version_compare(phpversion(), '5.4.0', '<') && false === getenv('TZ'))
85
        ) {
86
            date_default_timezone_set('UTC');
87
        }
88
    }
89
90
    /**
91
     * Ensure that the memory limit of PHP doesn't get in the way by disabling it.
92
     *
93
     * @return void
94
     */
95
    private function removePhpMemoryLimit()
96
    {
97
        ini_set('memory_limit', -1);
98
    }
99
100
    /**
101
     * Creates the dependency injection container user by phpDocumentor and disables the
102
     * use of annotations in it.
103
     *
104
     * @param string[] $values
105
     * @param array $definitions
106
     *
107
     * @return \DI\Container
108
     */
109
    private function createContainer(array $values, array $definitions)
110
    {
111
        $builder = new ContainerBuilder();
112
        $builder->addDefinitions($values);
113
        foreach ($definitions as $definition) {
114
            $builder->addDefinitions($definition);
115
        }
116
        $builder->useAnnotations(false);
117
        $phpDiContainer = $builder->build();
118
119
        return $phpDiContainer;
120
    }
121
}
122