InfoResolver::getInstallPath()   B
last analyzed

Complexity

Conditions 8
Paths 9

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 9
nop 2
dl 0
loc 36
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Package;
7
8
use Composer\Package\PackageInterface;
9
10
use Vaimo\ComposerPatches\Composer\ConfigKeys;
11
use Vaimo\ComposerPatches\Patch\Definition as Patch;
12
13
class InfoResolver
14
{
15
    const DEFAULT_PATH = '.';
16
17
    /**
18
     * @var \Composer\Installer\InstallationManager
19
     */
20
    private $installationManager;
21
22
    /**
23
     * @var string
24
     */
25
    private $vendorRoot;
26
27
    /**
28
     * @var array
29
     */
30
    private $installPathCache = array();
31
32
    /**
33
     * @param \Composer\Installer\InstallationManager $installationManager
34
     * @param string $vendorRoot
35
     */
36
    public function __construct(
37
        \Composer\Installer\InstallationManager $installationManager,
38
        $vendorRoot
39
    ) {
40
        $this->installationManager = $installationManager;
41
        $this->vendorRoot = $vendorRoot;
42
    }
43
44
    /**
45
     * @SuppressWarnings(PHPMD.StaticAccess)
46
     *
47
     * @param PackageInterface $package
48
     * @return bool|string
49
     */
50
    public function getSourcePath(PackageInterface $package)
51
    {
52
        return !$package instanceof \Composer\Package\RootPackageInterface
53
            ? $this->installationManager->getInstallPath($package)
54
            : realpath(dirname(\Composer\Factory::getComposerFile()));
55
    }
56
57
    public function resolveNamesFromPaths(array $packagesByName, array $paths)
58
    {
59
        $paths = array_unique(
60
            array_map('dirname', $paths)
61
        );
62
63
        $names = array();
64
65
        foreach ($paths as $path) {
66
            $segments = explode(DIRECTORY_SEPARATOR, $path);
67
68
            while ($chunk = array_slice($segments, 0, 2)) {
69
                array_shift($segments);
70
71
                $name = implode(DIRECTORY_SEPARATOR, $chunk);
72
73
                if (!isset($packagesByName[$name])) {
74
                    continue;
75
                }
76
77
                $names[] = $name;
78
79
                break;
80
            }
81
        }
82
83
        return $names;
84
    }
85
86
    public function getInstallPath(PackageInterface $package, $resolveMode)
87
    {
88
        $key = $package->getName() . '|' . $resolveMode;
89
90
        if (!isset($this->installPathCache[$key])) {
91
            switch ($resolveMode) {
92
                case Patch::CWD_VENDOR:
93
                    $this->installPathCache[$key] = $this->vendorRoot;
94
                    break;
95
                case Patch::CWD_PROJECT:
96
                    $this->installPathCache[$key] = getcwd();
97
                    break;
98
                case Patch::CWD_AUTOLOAD:
99
                    $autoloadConfig = $package->getAutoload();
100
101
                    $installPath = $this->getInstallPath($package, Patch::CWD_INSTALL);
102
103
                    if (!isset($autoloadConfig[ConfigKeys::PSR4_CONFIG])) {
104
                        return $installPath;
105
                    }
106
107
                    $this->installPathCache[$key] = $installPath . DIRECTORY_SEPARATOR .
108
                        reset($autoloadConfig[ConfigKeys::PSR4_CONFIG]);
109
110
                    break;
111
                case Patch::CWD_INSTALL:
112
                default:
113
                    $this->installPathCache[$key] = $package instanceof \Composer\Package\RootPackageInterface
114
                        ? $this->vendorRoot
115
                        : $this->getSourcePath($package);
116
117
                    break;
118
            }
119
        }
120
121
        return $this->installPathCache[$key];
122
    }
123
124
    public function getAutoLoadPaths(PackageInterface $package)
125
    {
126
        $autoloadConfig = $package->getAutoload();
127
128
        if (!isset($autoloadConfig[ConfigKeys::PSR4_CONFIG])) {
129
            return array();
130
        }
131
132
        $installPath = $this->getSourcePath($package);
133
134
        $sourcePaths = array_map(
135
            function ($path) use ($installPath) {
136
                return $installPath . DIRECTORY_SEPARATOR . $path;
137
            },
138
            $autoloadConfig[ConfigKeys::PSR4_CONFIG]
139
        );
140
141
        return array_values($sourcePaths);
142
    }
143
}
144