This repository was archived by the owner on Feb 5, 2025. It is now read-only.
forked from phpDocumentor/phpDocumentor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
310 lines (273 loc) · 9.46 KB
/
Copy pathApplication.php
File metadata and controls
310 lines (273 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* phpDocumentor
*
* PHP Version 5.3
*
* @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor;
use Cilex\Application as Cilex;
use Cilex\Provider\JmsSerializerServiceProvider;
use Cilex\Provider\MonologServiceProvider;
use Cilex\Provider\ValidatorServiceProvider;
use Composer\Autoload\ClassLoader;
use Monolog\ErrorHandler;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use phpDocumentor\Command\Helper\ConfigurationHelper;
use phpDocumentor\Command\Helper\LoggerHelper;
use phpDocumentor\Console\Input\ArgvInput;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Shell;
use Symfony\Component\Stopwatch\Stopwatch;
/**
* Application class for phpDocumentor.
*
* Can be used as bootstrap when the run method is not invoked.
*/
class Application extends Cilex
{
/** @var string $VERSION represents the version of phpDocumentor as stored in /VERSION */
public static $VERSION;
/**
* Initializes all components used by phpDocumentor.
*
* @param ClassLoader $autoloader
* @param array $values
*/
public function __construct($autoloader = null, array $values = array())
{
$this->defineIniSettings();
self::$VERSION = strpos('@package_version@', '@') === 0
? trim(file_get_contents(__DIR__ . '/../../VERSION'))
: '@package_version@';
parent::__construct('phpDocumentor', self::$VERSION, $values);
$this['kernel.timer.start'] = time();
$this['kernel.stopwatch'] = function () {
return new Stopwatch();
};
$this['autoloader'] = $autoloader;
$this->register(new JmsSerializerServiceProvider());
$this->register(new Configuration\ServiceProvider());
$this->addEventDispatcher();
$this->addLogging();
$this->register(new ValidatorServiceProvider());
$this->register(new Translator\ServiceProvider());
$this->register(new Descriptor\ServiceProvider());
$this->register(new Parser\ServiceProvider());
$this->register(new Partials\ServiceProvider());
$this->register(new Transformer\ServiceProvider());
$this->register(new Plugin\ServiceProvider());
$this->addCommandsForProjectNamespace();
if (\Phar::running()) {
$this->addCommandsForPharNamespace();
}
}
/**
* Removes all logging handlers and replaces them with handlers that can write to the given logPath and level.
*
* @param Logger $logger The logger instance that needs to be configured.
* @param integer $level The minimum level that will be written to the normal logfile; matches one of the
* constants in {@see \Monolog\Logger}.
* @param string $logPath The full path where the normal log file needs to be written.
*
* @return void
*/
public function configureLogger($logger, $level, $logPath = null)
{
/** @var Logger $monolog */
$monolog = $logger;
switch ($level) {
case 'emergency':
case 'emerg':
$level = Logger::EMERGENCY;
break;
case 'alert':
$level = Logger::ALERT;
break;
case 'critical':
case 'crit':
$level = Logger::CRITICAL;
break;
case 'error':
case 'err':
$level = Logger::ERROR;
break;
case 'warning':
case 'warn':
$level = Logger::WARNING;
break;
case 'notice':
$level = Logger::NOTICE;
break;
case 'info':
$level = Logger::INFO;
break;
case 'debug':
$level = Logger::DEBUG;
break;
}
$this['monolog.level'] = $level;
if ($logPath) {
$logPath = str_replace(
array('{APP_ROOT}', '{DATE}'),
array(realpath(__DIR__.'/../..'), $this['kernel.timer.start']),
$logPath
);
$this['monolog.logfile'] = $logPath;
}
// remove all handlers from the stack
try {
while ($monolog->popHandler()) {
}
} catch (\LogicException $e) {
// popHandler throws an exception when you try to pop the empty stack; to us this is not an
// error but an indication that the handler stack is empty.
}
if ($level === 'quiet') {
$monolog->pushHandler(new NullHandler());
return;
}
// set our new handlers
if ($logPath) {
$monolog->pushHandler(new StreamHandler($logPath, $level));
} else {
$monolog->pushHandler(new StreamHandler('php://stdout', $level));
}
}
/**
* Run the application and if no command is provided, use project:run.
*
* @param bool $interactive Whether to run in interactive mode.
*
* @return void
*/
public function run($interactive = false)
{
/** @var ConsoleApplication $app */
$app = $this['console'];
$app->setAutoExit(false);
if ($interactive) {
$app = new Shell($app);
}
$output = new Console\Output\Output();
$output->setLogger($this['monolog']);
$app->run(new ArgvInput(), $output);
}
/**
* Adjust php.ini settings.
*
* @return void
*/
protected function defineIniSettings()
{
$this->setTimezone();
ini_set('memory_limit', -1);
// this code cannot be tested because we cannot control the system settings in unit tests
// @codeCoverageIgnoreStart
if (extension_loaded('Zend OPcache') && ini_get('opcache.enable') && ini_get('opcache.enable_cli')) {
if (ini_get('opcache.save_comments')) {
ini_set('opcache.load_comments', 1);
} else {
ini_set('opcache.enable', 0);
}
}
if (extension_loaded('Zend Optimizer+') && ini_get('zend_optimizerplus.save_comments') == 0) {
throw new \RuntimeException('Please enable zend_optimizerplus.save_comments in php.ini.');
}
// @codeCoverageIgnoreEnd
}
/**
* If the timezone is not set anywhere, set it to UTC.
*
* This is done to prevent any warnings being outputted in relation to using
* date/time functions. What is checked is php.ini, and if the PHP version
* is prior to 5.4, the TZ environment variable.
*
* @link http://php.net/manual/en/function.date-default-timezone-get.php for more information how PHP determines the
* default timezone.
*
* @codeCoverageIgnore this method is very hard, if not impossible, to unit test and not critical.
*
* @return void
*/
protected function setTimezone()
{
if (false == ini_get('date.timezone')
|| (version_compare(phpversion(), '5.4.0', '<') && false === getenv('TZ'))
) {
date_default_timezone_set('UTC');
}
}
/**
* Adds a logging provider to the container of phpDocumentor.
*
* @return void
*/
protected function addLogging()
{
$this->register(
new MonologServiceProvider(),
array(
'monolog.name' => 'phpDocumentor',
'monolog.logfile' => sys_get_temp_dir() . '/phpdoc.log',
'monolog.debugfile' => sys_get_temp_dir() . '/phpdoc.debug.log',
'monolog.level' => Logger::INFO,
)
);
$app = $this;
/** @var Configuration $configuration */
$configuration = $this['config'];
$this['monolog.configure'] = $this->protect(
function ($log) use ($app, $configuration) {
$paths = $configuration->getLogging()->getPaths();
$logLevel = $configuration->getLogging()->getLevel();
$app->configureLogger($log, $logLevel, $paths['default'], $paths['errors']);
}
);
$this->extend(
'console',
function (ConsoleApplication $console) use ($configuration) {
$console->getHelperSet()->set(new LoggerHelper());
$console->getHelperSet()->set(new ConfigurationHelper($configuration));
return $console;
}
);
ErrorHandler::register($this['monolog']);
}
/**
* Adds the event dispatcher to phpDocumentor's container.
*
* @return void
*/
protected function addEventDispatcher()
{
$this['event_dispatcher'] = $this->share(
function () {
return Event\Dispatcher::getInstance();
}
);
}
/**
* Adds the command to phpDocumentor that belong to the Project namespace.
*
* @return void
*/
protected function addCommandsForProjectNamespace()
{
$this->command(new Command\Project\RunCommand());
}
/**
* Adds the command to phpDocumentor that belong to the Phar namespace.
*
* @return void
*/
protected function addCommandsForPharNamespace()
{
$this->command(new Command\Phar\UpdateCommand());
}
}