SharedPackageInstallerConfig   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 215
c 0
b 0
f 0
wmc 28
lcom 6
cbo 0
rs 9.0909

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 2
A setSymlinkDirectory() 0 12 3
A setVendorDir() 0 12 3
B setSymlinkBasePath() 0 22 6
A setIsSymlinkEnabled() 0 10 3
A getPackageList() 0 4 1
A setPackageList() 0 12 3
A isSymlinkEnabled() 0 4 1
A getVendorDir() 0 4 1
A getSymlinkDir() 0 4 1
A getOriginalVendorDir() 0 8 3
A getSymlinkBasePath() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the "Composer Shared Package Plugin" package.
5
 *
6
 * https://github.com/Letudiant/composer-shared-package-plugin
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LEtudiant\Composer\Installer\Config;
13
14
use LEtudiant\Composer\Installer\SharedPackageInstaller;
15
16
/**
17
 * @author Sylvain Lorinet <[email protected]>
18
 *
19
 * @see https://github.com/Letudiant/composer-shared-package-plugin/blob/master/docs/all-available-configurations.md
20
 */
21
class SharedPackageInstallerConfig
22
{
23
    const ENV_PARAMETER_VENDOR_DIR        = 'COMPOSER_SPP_VENDOR_DIR';
24
    const ENV_PARAMETER_SYMLINK_BASE_PATH = 'COMPOSER_SPP_SYMLINK_BASE_PATH';
25
26
27
    /**
28
     * @var string
29
     */
30
    protected $originalVendorDir;
31
32
    /**
33
     * @var string
34
     */
35
    protected $symlinkDir;
36
37
    /**
38
     * @var string
39
     */
40
    protected $vendorDir;
41
42
    /**
43
     * @var string|null
44
     */
45
    protected $symlinkBasePath;
46
47
    /**
48
     * @var bool
49
     */
50
    protected $isSymlinkEnabled = true;
51
52
    /**
53
     * @var array
54
     */
55
    protected $packageList = array();
56
57
58
    /**
59
     * @param string     $originalRelativeVendorDir
60
     * @param string     $originalAbsoluteVendorDir
61
     * @param array|null $extraConfigs
62
     */
63
    public function __construct($originalRelativeVendorDir, $originalAbsoluteVendorDir, $extraConfigs)
64
    {
65
        if (!isset($extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['vendor-dir'])) {
66
            throw new \InvalidArgumentException(
67
                'The "vendor-dir" parameter for "' . SharedPackageInstaller::PACKAGE_TYPE . '" configuration '
68
                . 'should be provided in your project composer.json ("extra" key)'
69
            );
70
        }
71
72
        $this->originalVendorDir = $originalRelativeVendorDir;
73
74
        $baseDir = substr($originalAbsoluteVendorDir, 0, -strlen($this->originalVendorDir));
75
76
        $this->setVendorDir($baseDir, $extraConfigs);
77
        $this->setSymlinkDirectory($baseDir, $extraConfigs);
78
        $this->setSymlinkBasePath($extraConfigs);
79
        $this->setIsSymlinkEnabled($extraConfigs);
80
        $this->setPackageList($extraConfigs);
81
    }
82
83
    /**
84
     * @param string $baseDir
85
     * @param array  $extraConfigs
86
     */
87
    protected function setSymlinkDirectory($baseDir, array $extraConfigs)
88
    {
89
        $this->symlinkDir = $baseDir . 'vendor-shared';
90
91
        if (isset($extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['symlink-dir'])) {
92
            $this->symlinkDir = $extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['symlink-dir'];
93
94
            if ('/' != $this->symlinkDir[0]) {
95
                $this->symlinkDir = $baseDir . $this->symlinkDir;
96
            }
97
        }
98
    }
99
100
    /**
101
     * @param string $baseDir
102
     * @param array  $extraConfigs
103
     *
104
     * @throws \InvalidArgumentException
105
     */
106
    protected function setVendorDir($baseDir, array $extraConfigs)
107
    {
108
        $this->vendorDir = $extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['vendor-dir'];
109
110
        if (false !== getenv(static::ENV_PARAMETER_VENDOR_DIR)) {
111
            $this->vendorDir = getenv(static::ENV_PARAMETER_VENDOR_DIR);
112
        }
113
114
        if ('/' != $this->vendorDir[0]) {
115
            $this->vendorDir = $baseDir . $this->vendorDir;
116
        }
117
    }
118
119
    /**
120
     * Allow to override symlinks base path.
121
     * This is useful for a Virtual Machine environment, where directories can be different
122
     * on the host machine and the guest machine.
123
     *
124
     * @param array $extraConfigs
125
     */
126
    protected function setSymlinkBasePath(array $extraConfigs)
127
    {
128
        if (isset($extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['symlink-base-path'])) {
129
            $this->symlinkBasePath = $extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['symlink-base-path'];
130
131
            if (false !== getenv(static::ENV_PARAMETER_SYMLINK_BASE_PATH)) {
132
                $this->symlinkBasePath = getenv(static::ENV_PARAMETER_SYMLINK_BASE_PATH);
133
            }
134
135
            // Remove the ending slash if exists
136
            if ('/' === $this->symlinkBasePath[strlen($this->symlinkBasePath) - 1]) {
137
                $this->symlinkBasePath = substr($this->symlinkBasePath, 0, -1);
138
            }
139
        } elseif (0 < strpos($extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['vendor-dir'], '/')) {
140
            $this->symlinkBasePath = $extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['vendor-dir'];
141
        }
142
143
        // Up to the project root directory
144
        if (0 < strpos($this->symlinkBasePath, '/')) {
145
            $this->symlinkBasePath = '../../' . $this->symlinkBasePath;
146
        }
147
    }
148
149
    /**
150
     * The symlink directory creation process can be disabled.
151
     * This may mean that you work directly with the sources directory so the symlink directory is useless.
152
     *
153
     * @param array $extraConfigs
154
     */
155
    protected function setIsSymlinkEnabled(array $extraConfigs)
156
    {
157
        if (isset($extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['symlink-enabled'])) {
158
            if (!is_bool($extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['symlink-enabled'])) {
159
                throw new \UnexpectedValueException('The configuration "symlink-enabled" should be a boolean');
160
            }
161
162
            $this->isSymlinkEnabled = $extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['symlink-enabled'];
163
        }
164
    }
165
166
    /**
167
     * @return array
168
     */
169
    public function getPackageList()
170
    {
171
        return $this->packageList;
172
    }
173
174
    /**
175
     * @param array $extraConfigs
176
     */
177
    public function setPackageList(array $extraConfigs)
178
    {
179
        if (isset($extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['package-list'])) {
180
            $packageList = $extraConfigs[SharedPackageInstaller::PACKAGE_TYPE]['package-list'];
181
182
            if (!is_array($packageList)) {
183
                throw new \UnexpectedValueException('The configuration "package-list" should be a JSON object');
184
            }
185
186
            $this->packageList = $packageList;
187
        }
188
    }
189
190
    /**
191
     * @return bool
192
     */
193
    public function isSymlinkEnabled()
194
    {
195
        return $this->isSymlinkEnabled;
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public function getVendorDir()
202
    {
203
        return $this->vendorDir;
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getSymlinkDir()
210
    {
211
        return $this->symlinkDir;
212
    }
213
214
    /**
215
     * @param bool $endingSlash
216
     *
217
     * @return string
218
     */
219
    public function getOriginalVendorDir($endingSlash = false)
220
    {
221
        if ($endingSlash && null != $this->originalVendorDir) {
222
            return $this->originalVendorDir . '/';
223
        }
224
225
        return $this->originalVendorDir;
226
    }
227
228
    /**
229
     * @return string|null
230
     */
231
    public function getSymlinkBasePath()
232
    {
233
        return $this->symlinkBasePath;
234
    }
235
}
236