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
285 lines (250 loc) · 8.23 KB
/
Copy pathApplication.php
File metadata and controls
285 lines (250 loc) · 8.23 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
<?php
/**
* phpDocumentor
*
* PHP Version 5.3
*
* @copyright 2010-2013 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\MonologServiceProvider;
use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\SerializerBuilder;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Shell;
use Zend\Cache\Storage\Adapter\Filesystem;
use Zend\Cache\Storage\Plugin\Serializer as SerializerPlugin;
use Zend\Config\Factory;
use phpDocumentor\Console\Input\ArgvInput;
use phpDocumentor\Descriptor\ProjectAnalyzer;
use phpDocumentor\Descriptor\Validation;
use phpDocumentor\Parser;
use phpDocumentor\Plugin\Core;
/**
* Finds and activates the autoloader.
*/
require_once findAutoloader();
/**
* Application class for phpDocumentor.
*
* Can be used as bootstrap when the run method is not invoked.
*/
class Application extends Cilex
{
public static $VERSION;
/**
* Initializes all components used by phpDocumentor.
*/
public function __construct()
{
self::$VERSION = file_get_contents(__DIR__ . '/../../VERSION');
parent::__construct('phpDocumentor', self::$VERSION);
$this->addAutoloader();
$this->addLogging();
$this->setTimezone();
$this->addConfiguration();
$this->addEventDispatcher();
$this['console']->getHelperSet()->set(
new Console\Helper\ProgressHelper()
);
$this['translator.locale'] = 'en';
$this['translator'] = $this->share(
function ($app) {
$translator = new Translator();
$translator->setLocale($app['translator.locale']);
return $translator;
}
);
$this['serializer'] = $this->share(
function () {
AnnotationRegistry::registerAutoloadNamespace(
'JMS\Serializer\Annotation',
__DIR__ . '/../../vendor/jms/serializer/src'
);
return SerializerBuilder::create()->build();
}
);
$this->addDescriptorServices();
$this->register(new Parser\ServiceProvider());
$this->register(new Transformer\ServiceProvider());
// TODO: make plugin service provider calls registrable from config
$this->register(new Core\ServiceProvider());
$this->addCommandsForProjectNamespace();
}
/**
* Instantiates the autoloader and adds it to phpDocumentor's container.
*
* @return void
*/
protected function addAutoloader()
{
$this['autoloader'] = include findAutoloader();
}
/**
* 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'
)
);
}
/**
* 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.
*
* @return void
*/
public function setTimezone()
{
if (false === ini_get('date.timezone') || (version_compare(phpversion(), '5.4.0', '<')
&& false === getenv('TZ'))
) {
date_default_timezone_set('UTC');
}
}
/**
* Adds the Configuration object to the DIC.
*
* phpDocumentor first loads the template config file (/data/phpdoc.tpl.xml)
* and then the phpdoc.dist.xml, or the phpdoc.xml if it exists but not both,
* from the current working directory.
*
* The user config file (either phpdox.dist.xml or phpdoc.xml) is merged
* with the template file.
*
* @return void
*/
protected function addConfiguration()
{
$this['config'] = $this->share(
function () {
$user_config_file = (file_exists(getcwd() . DIRECTORY_SEPARATOR . 'phpdoc.xml'))
? getcwd() . DIRECTORY_SEPARATOR . 'phpdoc.xml'
: getcwd() . DIRECTORY_SEPARATOR . 'phpdoc.dist.xml';
$config_files = array(__DIR__ . '/../../data/phpdoc.tpl.xml');
if (is_readable($user_config_file)) {
$config_files[] = $user_config_file;
}
return Factory::fromFiles($config_files, true);
}
);
}
/**
* 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 services to build the descriptors.
*
* This method injects the following services into the Dependency Injection Container:
*
* * descriptor.serializer, the serializer used to generate the cache
* * descriptor.builder, the builder used to transform the Reflected information into a series of Descriptors.
*
* It is possible to override which serializer is used by overriding the parameter `descriptor.serializer.class`.
*
* @return void
*/
protected function addDescriptorServices()
{
$this['descriptor.builder.serializer'] = 'PhpSerialize';
$this['descriptor.cache'] = $this->share(
function () {
$cache = new Filesystem();
$cache->setOptions(
array(
'namespace' => 'phpdoc-cache',
'cache_dir' => sys_get_temp_dir(),
)
);
$cache->addPlugin(new SerializerPlugin());
return $cache;
}
);
$this['descriptor.builder.validator'] = $this->share(
function ($container) {
return new Validation($container['translator']);
}
);
$this['descriptor.builder'] = $this->share(
function ($container) {
$builder = new Descriptor\Builder\Reflector();
$builder->setValidation($container['descriptor.builder.validator']);
return $builder;
}
);
$this['descriptor.analyzer'] = function () {
return new ProjectAnalyzer();
};
}
/**
* Adds the command to phpDocumentor that belong to the Project namespace.
*
* @return void
*/
protected function addCommandsForProjectNamespace()
{
$this->command(new Command\Project\RunCommand());
}
/**
* 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'];
if ($interactive) {
$app = new Shell($app);
}
$output = new Console\Output\Output();
$output->setLogger($this['monolog']);
$app->run(new ArgvInput(), $output);
}
}
/**
* Tries to find the autoloader relative to ththis file and return its path.
*
* @throws \RuntimeException if the autoloader could not be found.
*
* @return string the path of the autoloader.
*/
function findAutoloader()
{
$autoloader_base_path = '/../../vendor/autoload.php';
// if the file does not exist from a base path it is included as vendor
$autoloader_location = file_exists(__DIR__ . $autoloader_base_path)
? __DIR__ . $autoloader_base_path
: __DIR__ . '/../../..' . $autoloader_base_path;
if (!file_exists($autoloader_location)) {
throw new \RuntimeException(
'Unable to find autoloader at ' . $autoloader_location
);
}
return $autoloader_location;
}