10
10
namespace Youwe \TestingSuite \Composer ;
11
11
12
12
use Composer \Composer ;
13
+ use Composer \DependencyResolver \Operation ;
13
14
use Composer \EventDispatcher \EventSubscriberInterface ;
15
+ use Composer \Installer \PackageEvent ;
14
16
use Composer \IO \IOInterface ;
15
17
use Composer \Plugin \PluginInterface ;
18
+ use UnexpectedValueException ;
16
19
use Youwe \TestingSuite \Composer \Installer \InstallerInterface ;
17
20
18
21
/**
21
24
*/
22
25
class Plugin implements PluginInterface, EventSubscriberInterface
23
26
{
27
+ public const PACKAGE_NAME = 'youwe/testing-suite ' ;
28
+
24
29
/** @var InstallerInterface[] */
25
- private $ installers ;
30
+ private array $ installers ;
31
+
32
+ /**
33
+ * Subscribe to post update and post install command.
34
+ *
35
+ * @return array
36
+ */
37
+ public static function getSubscribedEvents (): array
38
+ {
39
+ return [
40
+ 'post-package-install ' => [ 'onPackageChange ' ],
41
+ 'post-package-update ' => [ 'onPackageChange ' ],
42
+ ];
43
+ }
26
44
27
45
/**
28
46
* Constructor.
@@ -42,7 +60,7 @@ public function __construct(InstallerInterface ...$installers)
42
60
*
43
61
* @return void
44
62
*/
45
- public function activate (Composer $ composer , IOInterface $ io )
63
+ public function activate (Composer $ composer , IOInterface $ io ): void
46
64
{
47
65
$ this ->addInstallers (
48
66
...include __DIR__ . '/installers.php ' ,
@@ -57,7 +75,7 @@ public function activate(Composer $composer, IOInterface $io)
57
75
*
58
76
* @return void
59
77
*/
60
- public function deactivate (Composer $ composer , IOInterface $ io )
78
+ public function deactivate (Composer $ composer , IOInterface $ io ): void
61
79
{
62
80
}
63
81
@@ -69,7 +87,7 @@ public function deactivate(Composer $composer, IOInterface $io)
69
87
*
70
88
* @return void
71
89
*/
72
- public function uninstall (Composer $ composer , IOInterface $ io )
90
+ public function uninstall (Composer $ composer , IOInterface $ io ): void
73
91
{
74
92
}
75
93
@@ -80,37 +98,34 @@ public function uninstall(Composer $composer, IOInterface $io)
80
98
*
81
99
* @return void
82
100
*/
83
- public function addInstallers (InstallerInterface ...$ installers )
101
+ public function addInstallers (InstallerInterface ...$ installers ): void
84
102
{
85
103
$ this ->installers = array_merge ($ this ->installers , $ installers );
86
104
}
87
105
88
106
/**
89
- * Run the installers.
107
+ * Run the installers when this package has been installed/updated
90
108
*
109
+ * @param PackageEvent $event
91
110
* @return void
92
111
*/
93
- public function install ()
112
+ public function onPackageChange ( PackageEvent $ event ): void
94
113
{
114
+ $ operation = $ event ->getOperation ();
115
+
116
+ $ packageName = match (true ) {
117
+ $ operation instanceof Operation \InstallOperation => $ operation ->getPackage ()->getName (),
118
+ $ operation instanceof Operation \UpdateOperation => $ operation ->getTargetPackage ()->getName (),
119
+ default => throw new UnexpectedValueException ('Unexpected operation type: ' . $ operation ::class),
120
+ };
121
+
122
+ if ($ packageName !== self ::PACKAGE_NAME ) {
123
+ return ;
124
+ }
125
+
126
+ $ event ->getIO ()->write ('<info>Running Youwe Testing Suite installer</info> ' );
95
127
foreach ($ this ->installers as $ installer ) {
96
128
$ installer ->install ();
97
129
}
98
130
}
99
-
100
- /**
101
- * Subscribe to post update and post install command.
102
- *
103
- * @return array
104
- */
105
- public static function getSubscribedEvents (): array
106
- {
107
- return [
108
- 'post-install-cmd ' => [
109
- 'install ' ,
110
- ],
111
- 'post-update-cmd ' => [
112
- 'install ' ,
113
- ],
114
- ];
115
- }
116
131
}
0 commit comments