Completed
Push — develop ( 9af8d2...9dbd4b )
by Jaap
17s
created

AutoloaderLocator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 10
c 0
b 0
f 0
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
    public static function autoload()
21
    {
22
        return require static::findVendorPath(). '/autoload.php';
23
    }
24
25
    /**
26
     * Attempts to find the location of the vendor folder.
27
     *
28
     * This method tries to check for a composer.json in a directory 5 levels below the folder of this Bootstrap file.
29
     * This is the expected location if phpDocumentor is installed using composer because the current directory for
30
     * this file is expected to be 'vendor/phpdocumentor/phpdocumentor/src/phpDocumentor'.
31
     *
32
     * If a composer.json is found we will try to extract the vendor folder name using the 'vendor-dir' configuration
33
     * option of composer or assume it is vendor if that option is not set.
34
     *
35
     *
36
     * If no custom composer.json can be found, then we assume that the vendor folder is that of phpDocumentor itself,
37
     * which is `../../vendor` starting from this folder.
38
     *
39
     * If neither locations exist, then this method returns null because no vendor path could be found.
40
     *
41
     * @param string $baseDir parameter for test purposes only.
42
     * @return string
43
     */
44
    public static function findVendorPath($baseDir = __DIR__): string
45
    {
46
        // default installation
47
        $vendorDir = $baseDir . '/../../vendor';
48
        // Composerised installation, vendor/phpdocumentor/phpdocumentor/src/phpDocumentor is __DIR__
49
        $rootFolderWhenInstalledWithComposer = $baseDir . '/../../../../../';
50
        $composerConfigurationPath           = $rootFolderWhenInstalledWithComposer .'composer.json';
51
        if (file_exists($composerConfigurationPath)) {
52
            $vendorDir = $rootFolderWhenInstalledWithComposer
53
                . self::getCustomVendorPathFromComposer($composerConfigurationPath);
54
        }
55
        return $vendorDir;
56
    }
57
    /**
58
     * Retrieves the custom vendor-dir from the given composer.json or returns 'vendor'.
59
     *
60
     * @param string $composerConfigurationPath the path pointing to the composer.json
61
     *
62
     * @return string
63
     */
64
    private static function getCustomVendorPathFromComposer($composerConfigurationPath): string
65
    {
66
        $composerFile = file_get_contents($composerConfigurationPath);
67
        $composerJson = json_decode($composerFile, true);
68
        return $composerJson['config']['vendor-dir'] ?? 'vendor';
69
    }
70
}
71