AutoloaderLocator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
ccs 11
cts 11
cp 1
wmc 4
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A autoload() 0 4 1
A findVendorPath() 0 13 2
A getCustomVendorPathFromComposer() 0 6 1
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
 * @author    Mike van Riel <[email protected]>
9
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpdoc.org
12
 *
13
 *
14
 */
15
16
namespace phpDocumentor;
17
18
final class AutoloaderLocator
19
{
20
    /**
21
     * @codeCoverageIgnore cannot test without side-effects
22
     */
23
    public static function autoload()
24
    {
25
        return require static::findVendorPath(). '/autoload.php';
26
    }
27
28
    /**
29
     * Attempts to find the location of the vendor folder.
30
     *
31
     * This method tries to check for a composer.json in a directory 5 levels below the folder of this Bootstrap file.
32
     * This is the expected location if phpDocumentor is installed using composer because the current directory for
33
     * this file is expected to be 'vendor/phpdocumentor/phpdocumentor/src/phpDocumentor'.
34
     *
35
     * If a composer.json is found we will try to extract the vendor folder name using the 'vendor-dir' configuration
36
     * option of composer or assume it is vendor if that option is not set.
37
     *
38
     *
39
     * If no custom composer.json can be found, then we assume that the vendor folder is that of phpDocumentor itself,
40
     * which is `../../vendor` starting from this folder.
41
     *
42
     * If neither locations exist, then this method returns null because no vendor path could be found.
43
     *
44
     * @param string $baseDir parameter for test purposes only.
45
     * @return string
46
     */
47 3
    public static function findVendorPath($baseDir = __DIR__): string
48
    {
49
        // default installation
50 3
        $vendorDir = $baseDir . '/../../vendor';
51
        // Composerised installation, vendor/phpdocumentor/phpdocumentor/src/phpDocumentor is __DIR__
52 3
        $rootFolderWhenInstalledWithComposer = $baseDir . '/../../../../../';
53 3
        $composerConfigurationPath           = $rootFolderWhenInstalledWithComposer .'composer.json';
54 3
        if (file_exists($composerConfigurationPath)) {
55
            $vendorDir = $rootFolderWhenInstalledWithComposer
56 2
                . self::getCustomVendorPathFromComposer($composerConfigurationPath);
57
        }
58 3
        return $vendorDir;
59
    }
60
    /**
61
     * Retrieves the custom vendor-dir from the given composer.json or returns 'vendor'.
62
     *
63
     * @param string $composerConfigurationPath the path pointing to the composer.json
64
     *
65
     * @return string
66
     */
67 2
    private static function getCustomVendorPathFromComposer($composerConfigurationPath): string
68
    {
69 2
        $composerFile = file_get_contents($composerConfigurationPath);
70 2
        $composerJson = json_decode($composerFile, true);
71 2
        return $composerJson['config']['vendor-dir'] ?? 'vendor';
72
    }
73
}
74