1
|
|
|
<?php |
2
|
|
|
namespace Composed; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @author Josh Di Fabio <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
abstract class AbstractPackage |
8
|
|
|
{ |
9
|
|
|
private $dirPath; |
10
|
|
|
private $config; |
11
|
|
|
private $root; |
12
|
|
|
private $lockFile; |
13
|
|
|
private $directDependencies; |
14
|
|
|
private $dependencies; |
15
|
|
|
|
16
|
|
|
protected function __construct(RootPackage $root, string $dirPath, JsonObject $config) |
17
|
|
|
{ |
18
|
|
|
$this->root = $root; |
19
|
|
|
$this->dirPath = $dirPath; |
20
|
|
|
$this->config = $config; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return null|string |
25
|
|
|
*/ |
26
|
|
|
public function getName(bool $includeVendorName = true) |
27
|
|
|
{ |
28
|
|
|
$name = $this->config->get(['name']); |
29
|
|
|
|
30
|
|
|
if ($includeVendorName) { |
31
|
|
|
return $name; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return ltrim(strstr($name, '/'), '/'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return null|string |
39
|
|
|
*/ |
40
|
|
|
public function getVendorName() |
41
|
|
|
{ |
42
|
|
|
if (null === $name = $this->getName()) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return strstr($name, '/', true); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isRoot() : bool |
50
|
|
|
{ |
51
|
|
|
return $this->root === $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function getConfig($keys = [], $default = null) |
58
|
|
|
{ |
59
|
|
|
return $this->config->get($keys, $default); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getPath(string $relativePath = '') : string |
63
|
|
|
{ |
64
|
|
|
return $this->dirPath . (strlen($relativePath) ? DIRECTORY_SEPARATOR . $relativePath : ''); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return null|LockFile |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function getLockFile() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if (null === $this->lockFile) { |
73
|
|
|
$filePath = $this->getPath('composer.lock'); |
74
|
|
|
if (file_exists($filePath)) { |
75
|
|
|
$this->lockFile = LockFile::fromFilePath($this->root, $filePath); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->lockFile; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function directlyRequires(string $packageName) : bool |
83
|
|
|
{ |
84
|
|
|
return null !== $this->getDirectDependencies()->getPackage($packageName); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function requires(string $packageName) : bool |
88
|
|
|
{ |
89
|
|
|
return null !== $this->getDependencies()->getPackage($packageName); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getDirectDependencies() : PackageCollection |
93
|
|
|
{ |
94
|
|
|
if (null === $this->directDependencies) { |
95
|
|
|
$packageNames = array_keys($this->getConfig('require', $default = [])); |
|
|
|
|
96
|
|
|
$packages = @array_map( |
97
|
|
|
function ($packageName) { |
98
|
|
|
return $this->root->getPackages()->getPackage($packageName); |
99
|
|
|
}, |
100
|
|
|
$packageNames |
101
|
|
|
); |
102
|
|
|
$packages = array_combine($packageNames, $packages); |
103
|
|
|
$packages = array_filter($packages); |
104
|
|
|
$this->directDependencies = new PackageCollection($packages); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->directDependencies; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getDependencies() : PackageCollection |
111
|
|
|
{ |
112
|
|
|
if (null === $this->dependencies) { |
113
|
|
|
$packages = []; |
114
|
|
|
/** @var Package $package */ |
115
|
|
|
foreach ($this->getDirectDependencies() as $name => $package) { |
116
|
|
|
$packages[$name] = $package; |
117
|
|
|
$packages = array_merge($packages, $package->getDependencies()->toArray()); |
118
|
|
|
} |
119
|
|
|
$this->dependencies = new PackageCollection($packages); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->dependencies; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.