Completed
Push — develop ( 71fd61...bbac44 )
by Jaap
06:03 queued 02:27
created

src/phpDocumentor/Bootstrap.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor;
13
14
use Composer\Autoload\ClassLoader;
15
16
/**
17
 * This class provides a bootstrap for all application who wish to interface with phpDocumentor.
18
 *
19
 * The Bootstrapper is responsible for setting up the autoloader, profiling options and application, including
20
 * dependency injection container.
21
 *
22
 * The simplest usage would be:
23
 *
24
 *     $app = Bootstap::createInstance()->initialize();
25
 *
26
 * This will setup the autoloader and application, including Service Container, and return an instance of the
27
 * application ready to be ran using the `run` command.
28
 *
29
 * If you need more control you can do some of the steps manually:
30
 *
31
 *     $bootstrap = Bootstap::createInstance();
32
 *     $autoloader = $bootstrap->createAutoloader();
33
 *     $app = new Application($autoloader)
34
 */
35
class Bootstrap
36
{
37
    /**
38
     * Helper static function to get an instance of this class.
39
     *
40
     * Usually used to do a one-line initialization, such as:
41
     *
42
     *     \phpDocumentor\Bootstrap::createInstance()->initialize();
43
     *
44
     * @return Bootstrap
45
     */
46 6
    public static function createInstance()
47
    {
48 6
        return new self();
49
    }
50
51
    /**
52
     * Convenience method that does the complete initialization for phpDocumentor.
53
     *
54
     * @return Application
55
     */
56 1
    public function initialize()
57
    {
58 1
        $vendorPath = $this->findVendorPath();
59
60 1
        $autoloader = $this->createAutoloader($vendorPath);
61
62 1
        return new Application($autoloader, array('composer.vendor_path' => $vendorPath));
63
    }
64
65
    /**
66
     * Sets up XHProf so that we can profile phpDocumentor using XHGUI.
67
     *
68
     * @return self
69
     */
70
    public function registerProfiler()
71
    {
72
        // check whether xhprof is loaded
73
        $profile = (bool) (getenv('PHPDOC_PROFILE') === 'on');
74
        $xhguiPath = getenv('XHGUI_PATH');
75
        if ($profile && $xhguiPath && extension_loaded('xhprof')) {
76
            echo 'PROFILING ENABLED' . PHP_EOL;
77
            include($xhguiPath . '/external/header.php');
78
        }
79
80
        return $this;
81
    }
82
83
    /**
84
     * Initializes and returns the autoloader.
85
     *
86
     * @param string|null $vendorDir A path (either absolute or relative to the current working directory) leading to
87
     *     the vendor folder where composer installed the dependencies.
88
     *
89
     * @throws \RuntimeException if no autoloader could be found.
90
     *
91
     * @return ClassLoader
92
     */
93 3
    public function createAutoloader($vendorDir = null)
94
    {
95 3
        if (! $vendorDir) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $vendorDir of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
96
            $vendorDir = __DIR__ . '/../../vendor';
97
        }
98
99 3
        $autoloader_location = $vendorDir . '/autoload.php';
100 3
        if (! file_exists($autoloader_location) || ! is_readable($autoloader_location)) {
101 1
            throw new \RuntimeException(
102 1
                'phpDocumentor expected to find an autoloader at "' . $autoloader_location . '" but it was not there. '
103 1
                . 'Usually this is because the "composer install" command has not been ran yet. If this is not the '
104 1
                . 'case, please open an issue at http://github.com/phpDocumentor/phpDocumentor2 detailing what '
105 1
                . 'installation method you used, which path is mentioned in this error message and any other relevant '
106 1
                . 'information.'
107
            );
108
        }
109
110 2
        return require $autoloader_location;
111
    }
112
113
    /**
114
     * Attempts to find the location of the vendor folder.
115
     *
116
     * This method tries to check for a composer.json in a directory 5 levels below the folder of this Bootstrap file.
117
     * This is the expected location if phpDocumentor is installed using composer because the current directory for
118
     * this file is expected to be 'vendor/phpdocumentor/phpdocumentor/src/phpDocumentor'.
119
     *
120
     * If a composer.json is found we will try to extract the vendor folder name using the 'vendor-dir' configuration
121
     * option of composer or assume it is vendor if that option is not set.
122
     *
123
     *
124
     * If no custom composer.json can be found, then we assume that the vendor folder is that of phpDocumentor itself,
125
     * which is `../../vendor` starting from this folder.
126
     *
127
     * If neither locations exist, then this method returns null because no vendor path could be found.
128
     *
129
     * @param $baseDir parameter for test purposes only.
130
     * @return string|null
131
     */
132 3
    public function findVendorPath($baseDir = __DIR__)
133
    {
134
        // default installation
135 3
        $vendorDir = $baseDir . '/../../vendor';
136
137
        // Composerised installation, vendor/phpdocumentor/phpdocumentor/src/phpDocumentor is __DIR__
138 3
        $rootFolderWhenInstalledWithComposer = $baseDir . '/../../../../../';
139 3
        $composerConfigurationPath           = $rootFolderWhenInstalledWithComposer .'composer.json';
140 3
        if (file_exists($composerConfigurationPath)) {
141
            $vendorDir = $rootFolderWhenInstalledWithComposer
142 1
                . $this->getCustomVendorPathFromComposer($composerConfigurationPath);
143
        }
144
145 3
        return file_exists($vendorDir) ? $vendorDir : null;
146
    }
147
148
    /**
149
     * Retrieves the custom vendor-dir from the given composer.json or returns 'vendor'.
150
     *
151
     * @param string $composerConfigurationPath the path pointing to the composer.json
152
     *
153
     * @return string
154
     */
155 1
    protected function getCustomVendorPathFromComposer($composerConfigurationPath)
156
    {
157 1
        $composerFile = file_get_contents($composerConfigurationPath);
158 1
        $composerJson = json_decode($composerFile, true);
159
160 1
        return $composerJson['config']['vendor-dir'] ?? 'vendor';
161
    }
162
}
163