1 | <?php |
||
2 | /** |
||
3 | * YOURLS Composer Installer |
||
4 | */ |
||
5 | |||
6 | namespace YOURLS\ComposerInstaller; |
||
7 | |||
8 | use RuntimeException; |
||
9 | use Composer\Installer\LibraryInstaller; |
||
10 | use Composer\Package\PackageInterface; |
||
11 | use Composer\Repository\InstalledRepositoryInterface; |
||
12 | |||
13 | /** |
||
14 | * Extension of Composer's LibraryInstaller that allows a custom function |
||
15 | * handle after update or install |
||
16 | * |
||
17 | * @package YOURLS\ComposerInstaller |
||
18 | * @author Ozh <[email protected]> |
||
19 | * @link https://github.com/yourls/composer-installer/ |
||
20 | * @license MIT |
||
21 | */ |
||
22 | class Installer extends LibraryInstaller |
||
23 | { |
||
24 | /** |
||
25 | * Installs specific package. |
||
26 | * |
||
27 | * @param InstalledRepositoryInterface $repo repository in which to check |
||
28 | * @param PackageInterface $package package instance |
||
29 | */ |
||
30 | 5 | public function install(InstalledRepositoryInterface $repo, PackageInterface $package) |
|
31 | { |
||
32 | // first install the package normally... |
||
33 | 5 | parent::install($repo, $package); |
|
34 | |||
35 | // ...then run custom code |
||
36 | 5 | $this->postInstall($package); |
|
37 | 5 | } |
|
38 | |||
39 | /** |
||
40 | * Updates specific package. |
||
41 | * |
||
42 | * @param InstalledRepositoryInterface $repo repository in which to check |
||
43 | * @param PackageInterface $initial already installed package version |
||
44 | * @param PackageInterface $target updated version |
||
45 | * |
||
46 | * @throws InvalidArgumentException if $initial package is not installed |
||
47 | */ |
||
48 | 3 | public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) |
|
49 | { |
||
50 | // first update the package normally... |
||
51 | 3 | parent::update($repo, $initial, $target); |
|
52 | |||
53 | // ...then run custom code |
||
54 | 3 | $this->postInstall($target); |
|
55 | 3 | } |
|
56 | |||
57 | /** |
||
58 | * Custom handler called after each package installation or update |
||
59 | * |
||
60 | * @param PackageInterface $package |
||
61 | */ |
||
62 | 3 | protected function postInstall(PackageInterface $package) |
|
63 | { |
||
64 | // remove the package's `vendor` directory to avoid duplicated autoloader and vendor code |
||
65 | 3 | $packageVendorDir = $this->getInstallPath($package) . '/vendor'; |
|
66 | 3 | if (is_dir($packageVendorDir)) { |
|
67 | 1 | $success = $this->filesystem->removeDirectory($packageVendorDir); |
|
68 | 1 | if (!$success) { |
|
69 | throw new RuntimeException('Could not completely delete ' . $path . ', aborting.'); // @codeCoverageIgnore |
||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
70 | } |
||
71 | } |
||
72 | 3 | } |
|
73 | } |
||
74 |