-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathPluginInstaller.php
96 lines (80 loc) · 2.1 KB
/
PathPluginInstaller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace TypedPHP\Composer;
use Composer\Installer\LibraryInstaller;
use Composer\Package\PackageInterface;
class PathPluginInstaller extends LibraryInstaller
{
/**
* @param PackageInterface $package
*
* @return string
*/
public function getPackageBasePath(PackageInterface $package)
{
$root = $this->composer->getPackage();
if ($rootPath = $this->getRootPath($root, $package)) {
return $rootPath . "/" . $package->getName();
}
if ($packagePath = $this->getPackagePath($package)) {
return $packagePath . "/" . $package->getName();
}
return parent::getPackageBasePath($package);
}
/**
* @param PackageInterface $root
* @param PackageInterface $package
*
* @return mixed
*/
public function getRootPath(PackageInterface $root, PackageInterface $package)
{
$extra = $root->getExtra();
$name = $package->getName();
if (isset($extra["paths"]) and is_array($extra["paths"])) {
foreach ($extra["paths"] as $pattern => $path) {
if ($this->matches($name, $pattern)) {
return $path;
}
}
}
return null;
}
/**
* @param string $string
* @param string $pattern
*
* @return bool
*/
public function matches($string, $pattern)
{
if ($pattern == $string) {
return true;
}
$pattern = preg_quote($pattern, "#");
$pattern = str_replace("\\*", ".*", $pattern);
$pattern = "#^" . $pattern . "$#";
return (boolean) preg_match($pattern, $string);
}
/**
* @param PackageInterface $package
*
* @return mixed
*/
public function getPackagePath(PackageInterface $package)
{
$extra = $package->getExtra();
if (isset($extra["path"])) {
return $extra["path"];
}
return null;
}
/**
* @param string $type
*
* @return bool
*/
public function supports($type)
{
return true;
}
}