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'] |
|
|
|
|
34
|
|
|
) { |
35
|
|
|
$this->disableGarbageCollection(); |
36
|
|
|
$this->setTimezone(); |
37
|
|
|
$this->removePhpMemoryLimit(); |
38
|
|
|
|
39
|
|
|
$container = $this->createContainer($values, $dependencyInjectionDefinitions); |
40
|
|
|
$this->console = $container->get(ConsoleApplication::class); |
|
|
|
|
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') |
|
|
|
|
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
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.