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 pathBootstrap.php
More file actions
162 lines (146 loc) · 5.72 KB
/
Copy pathBootstrap.php
File metadata and controls
162 lines (146 loc) · 5.72 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
<?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 Composer\Autoload\ClassLoader;
/**
* This class provides a bootstrap for all application who wish to interface with phpDocumentor.
*
* The Bootstrapper is responsible for setting up the autoloader, profiling options and application, including
* dependency injection container.
*
* The simplest usage would be:
*
* $app = Bootstap::createInstance()->initialize();
*
* This will setup the autoloader and application, including Service Container, and return an instance of the
* application ready to be ran using the `run` command.
*
* If you need more control you can do some of the steps manually:
*
* $bootstrap = Bootstap::createInstance();
* $autoloader = $bootstrap->createAutoloader();
* $app = new Application($autoloader)
*/
class Bootstrap
{
/**
* Helper static function to get an instance of this class.
*
* Usually used to do a one-line initialization, such as:
*
* \phpDocumentor\Bootstrap::createInstance()->initialize();
*
* @return Bootstrap
*/
public static function createInstance()
{
return new self();
}
/**
* Convenience method that does the complete initialization for phpDocumentor.
*
* @return Application
*/
public function initialize()
{
$vendorPath = $this->findVendorPath();
$autoloader = $this->createAutoloader($vendorPath);
return new Application($autoloader, array('composer.vendor_path' => $vendorPath));
}
/**
* Sets up XHProf so that we can profile phpDocumentor using XHGUI.
*
* @return self
*/
public function registerProfiler()
{
// check whether xhprof is loaded
$profile = (bool) (getenv('PHPDOC_PROFILE') === 'on');
$xhguiPath = getenv('XHGUI_PATH');
if ($profile && $xhguiPath && extension_loaded('xhprof')) {
echo 'PROFILING ENABLED' . PHP_EOL;
include($xhguiPath . '/external/header.php');
}
return $this;
}
/**
* Initializes and returns the autoloader.
*
* @param string|null $vendorDir A path (either absolute or relative to the current working directory) leading to
* the vendor folder where composer installed the dependencies.
*
* @throws \RuntimeException if no autoloader could be found.
*
* @return ClassLoader
*/
public function createAutoloader($vendorDir = null)
{
if (! $vendorDir) {
$vendorDir = __DIR__ . '/../../vendor';
}
$autoloader_location = $vendorDir . '/autoload.php';
if (! file_exists($autoloader_location) || ! is_readable($autoloader_location)) {
throw new \RuntimeException(
'phpDocumentor expected to find an autoloader at "' . $autoloader_location . '" but it was not there. '
. 'Usually this is because the "composer install" command has not been ran yet. If this is not the '
. 'case, please open an issue at http://github.com/phpDocumentor/phpDocumentor2 detailing what '
. 'installation method you used, which path is mentioned in this error message and any other relevant '
. 'information.'
);
}
return require $autoloader_location;
}
/**
* Attempts to find the location of the vendor folder.
*
* This method tries to check for a composer.json in a directory 5 levels below the folder of this Bootstrap file.
* This is the expected location if phpDocumentor is installed using composer because the current directory for
* this file is expected to be 'vendor/phpdocumentor/phpdocumentor/src/phpDocumentor'.
*
* If a composer.json is found we will try to extract the vendor folder name using the 'vendor-dir' configuration
* option of composer or assume it is vendor if that option is not set.
*
*
* If no custom composer.json can be found, then we assume that the vendor folder is that of phpDocumentor itself,
* which is `../../vendor` starting from this folder.
*
* If neither locations exist, then this method returns null because no vendor path could be found.
*
* @param $baseDir parameter for test purposes only.
* @return string|null
*/
public function findVendorPath($baseDir = __DIR__)
{
// default installation
$vendorDir = $baseDir . '/../../vendor';
// Composerised installation, vendor/phpdocumentor/phpdocumentor/src/phpDocumentor is __DIR__
$rootFolderWhenInstalledWithComposer = $baseDir . '/../../../../../';
$composerConfigurationPath = $rootFolderWhenInstalledWithComposer .'composer.json';
if (file_exists($composerConfigurationPath)) {
$vendorDir = $rootFolderWhenInstalledWithComposer
. $this->getCustomVendorPathFromComposer($composerConfigurationPath);
}
return file_exists($vendorDir) ? $vendorDir : null;
}
/**
* Retrieves the custom vendor-dir from the given composer.json or returns 'vendor'.
*
* @param string $composerConfigurationPath the path pointing to the composer.json
*
* @return string
*/
protected function getCustomVendorPathFromComposer($composerConfigurationPath)
{
$composerFile = file_get_contents($composerConfigurationPath);
$composerJson = json_decode($composerFile, true);
return isset($composerJson['config']['vendor-dir']) ? $composerJson['config']['vendor-dir'] : 'vendor';
}
}