1 | <?php |
||
2 | /** |
||
3 | * YOURLS Composer Installer |
||
4 | */ |
||
5 | |||
6 | namespace YOURLS\ComposerInstaller; |
||
7 | |||
8 | use Composer\Package\PackageInterface; |
||
9 | |||
10 | /** |
||
11 | * This class does the actual job of the plugin installer (check if package |
||
12 | * supports the installer and if so set its install path) |
||
13 | * |
||
14 | * @package YOURLS\ComposerInstaller |
||
15 | * @author Ozh <[email protected]> |
||
16 | * @link https://github.com/yourls/composer-installer/ |
||
17 | * @license MIT |
||
18 | */ |
||
19 | class PluginInstaller extends Installer |
||
20 | { |
||
21 | /** |
||
22 | * Decides if this installer supports the given package type |
||
23 | * |
||
24 | * @param string $packageType |
||
25 | * @return bool true if type is 'yourls-plugin', false otherwise |
||
26 | */ |
||
27 | 2 | public function supports($packageType): bool |
|
28 | { |
||
29 | 2 | return $packageType === 'yourls-plugin'; |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * Returns the installation path of a package |
||
34 | * |
||
35 | * @param PackageInterface $package Package |
||
36 | * @return string path |
||
37 | */ |
||
38 | 8 | public function getInstallPath(PackageInterface $package): string |
|
39 | { |
||
40 | // Put package in 'vendor/' directory as usual if PluginInstaller is not required |
||
41 | 8 | if ($this->requiresPluginInstaller($package) !== true) { |
|
42 | 3 | return $this->fixWinPath(parent::getInstallPath($package)); |
|
43 | } |
||
44 | |||
45 | // Get the extra configuration of the top-level package |
||
46 | 5 | if ($rootPackage = $this->composer->getPackage()) { |
|
47 | $extra = $rootPackage->getExtra(); |
||
48 | } else { |
||
49 | 5 | $extra = []; |
|
50 | } |
||
51 | |||
52 | // use base path from configuration if specified, otherwise use YOURLS default |
||
53 | 5 | $basePath = $extra['yourls-plugin-path'] ?? 'user/plugins'; |
|
54 | |||
55 | // Get plugin name from its package name |
||
56 | 5 | $prettyName = $package->getPrettyName(); |
|
57 | 5 | $pluginExtra = $package->getExtra(); |
|
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
58 | |||
59 | 5 | if (strpos($prettyName, '/') !== false) { |
|
60 | // use name after the slash |
||
61 | 4 | $name = explode('/', $prettyName)[1]; |
|
62 | } else { |
||
63 | // case when a package is just 'name', not 'vendor/name' (can this happen?) |
||
64 | 1 | $name = $prettyName; |
|
65 | } |
||
66 | |||
67 | // build destination path from base path and plugin name |
||
68 | 5 | return $this->fixWinPath($basePath . '/' . $name); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Custom handler that will be called after package install or update |
||
73 | * |
||
74 | * @param PackageInterface $package Package |
||
75 | */ |
||
76 | 5 | protected function postInstall(PackageInterface $package) |
|
77 | { |
||
78 | // only continue if PluginInstaller is supported |
||
79 | 5 | if ($this->requiresPluginInstaller($package) !== true) { |
|
80 | 2 | return; |
|
81 | } |
||
82 | |||
83 | 3 | parent::postInstall($package); |
|
84 | 3 | } |
|
85 | |||
86 | /** |
||
87 | * Checks if the package has explicitly required this installer |
||
88 | * |
||
89 | * If not (package is not a YOURLS plugin, or plugin does not support this installer yet) |
||
90 | * the installer will fall back to the behavior of the LibraryInstaller |
||
91 | * |
||
92 | * @param PackageInterface $package |
||
93 | * @return bool |
||
94 | */ |
||
95 | 8 | protected function requiresPluginInstaller(PackageInterface $package): bool |
|
96 | { |
||
97 | 8 | foreach ($package->getRequires() as $link) { |
|
98 | 5 | if ($link->getTarget() === 'yourls/composer-installer') { |
|
99 | 5 | return true; |
|
100 | } |
||
101 | } |
||
102 | |||
103 | // no required package is the installer |
||
104 | 3 | return false; |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * Fix path in Windows |
||
109 | * |
||
110 | * @param string $path Path |
||
111 | * @return string |
||
112 | */ |
||
113 | 8 | protected function fixWinPath(string $path): string |
|
114 | { |
||
115 | 8 | return str_replace('\\', '/', $path); |
|
116 | } |
||
117 | |||
118 | } |
||
119 |