6
6
use Composer \DependencyResolver \Operation \UpdateOperation ;
7
7
use Composer \EventDispatcher \EventSubscriberInterface ;
8
8
use Composer \Installer \PackageEvent ;
9
+ use Composer \Installer \PackageEvents ;
9
10
use Composer \IO \IOInterface ;
10
11
use Composer \Plugin \PluginInterface ;
11
12
use Composer \Script \Event ;
12
13
use Composer \Script \ScriptEvents ;
14
+ use Composer \Semver \Comparator ;
13
15
14
16
class Installer implements PluginInterface, EventSubscriberInterface
15
17
{
@@ -22,9 +24,20 @@ public function activate(Composer $composer, IOInterface $io)
22
24
{
23
25
$ this ->io = $ io ;
24
26
}
25
-
27
+
28
+ public function deactivate (Composer $ composer , IOInterface $ io )
29
+ {
30
+ }
31
+
32
+ public function uninstall (Composer $ composer , IOInterface $ io ) {
33
+ $ this ->deleteFile ();
34
+ }
35
+
26
36
protected function isOperationOnC3 (PackageEvent $ event )
27
37
{
38
+ if (static ::composerV2 ()) {
39
+ return true ;
40
+ }
28
41
$ name = '' ;
29
42
30
43
if ($ event ->getOperation () instanceof InstallOperation) {
@@ -40,34 +53,50 @@ protected function isOperationOnC3(PackageEvent $event)
40
53
41
54
public static function getSubscribedEvents ()
42
55
{
56
+ if (static ::composerV2 ()) {
57
+ return [
58
+ ScriptEvents::POST_INSTALL_CMD => [
59
+ ['copyC3V2 ' , 0 ]
60
+ ],
61
+ ScriptEvents::POST_UPDATE_CMD => [
62
+ ['askForUpdateV2 ' , 0 ]
63
+ ],
64
+ ];
65
+ }
43
66
return [
44
- ScriptEvents ::POST_PACKAGE_INSTALL => [
67
+ PackageEvents ::POST_PACKAGE_INSTALL => [
45
68
['copyC3 ' , 0 ]
46
69
],
47
- ScriptEvents ::POST_PACKAGE_UPDATE => [
70
+ PackageEvents ::POST_PACKAGE_UPDATE => [
48
71
['askForUpdate ' , 0 ]
49
72
],
50
- ScriptEvents ::POST_PACKAGE_UNINSTALL => [
73
+ PackageEvents ::POST_PACKAGE_UNINSTALL => [
51
74
['deleteC3 ' , 0 ]
52
75
]
53
76
];
54
77
}
55
78
56
- public static function copyC3ToRoot (Event $ event )
57
- {
58
- $ event ->getIO ()->write ("<warning>c3 is now a Composer Plugin and installs c3.php automatically.</warning> " );
59
- $ event ->getIO ()->write ("<warning>Please remove current \"post-install-cmd \" and \"post-update-cmd \" hooks from your composer.json</warning> " );
60
- }
61
-
62
79
public function copyC3 (PackageEvent $ event )
63
80
{
64
81
if (!$ this ->isOperationOnC3 ($ event )) {
65
82
return ;
66
83
}
84
+
85
+ $ this ->copyC3V2 (null );
86
+ }
87
+
88
+ public function copyC3V2 (Event $ event )
89
+ {
67
90
if ($ this ->c3NotChanged ()) {
68
91
$ this ->io ->write ("<comment>[codeception/c3]</comment> c3.php is already up-to-date " );
69
92
return ;
70
93
}
94
+ if (file_exists (getcwd () . DIRECTORY_SEPARATOR . 'c3.php ' )) {
95
+ $ replace = $ this ->io ->askConfirmation ("<warning>c3.php has changed</warning> Do you want to replace c3.php with latest version? " , false );
96
+ if (!$ replace ) {
97
+ return ;
98
+ }
99
+ }
71
100
72
101
$ this ->io ->write ("<comment>[codeception/c3]</comment> Copying c3.php to the root of your project... " );
73
102
copy (__DIR__ . DIRECTORY_SEPARATOR . 'c3.php ' , getcwd () . DIRECTORY_SEPARATOR .'c3.php ' );
@@ -79,15 +108,17 @@ public function askForUpdate(PackageEvent $event)
79
108
if (!$ this ->isOperationOnC3 ($ event ) || $ this ->c3NotChanged ()) {
80
109
return ;
81
110
}
82
- if (file_exists (getcwd () . DIRECTORY_SEPARATOR . 'c3.php ' )) {
83
- $ replace = $ this ->io ->askConfirmation ("<warning>c3.php has changed</warning> Do you want to replace c3.php with latest version? " , false );
84
- if (!$ replace ) {
85
- return ;
86
- }
87
- }
88
111
$ this ->copyC3 ($ event );
89
112
}
90
113
114
+ public function askForUpdateV2 (Event $ event )
115
+ {
116
+ if ($ this ->c3NotChanged ()) {
117
+ return ;
118
+ }
119
+ $ this ->copyC3V2 ($ event );
120
+ }
121
+
91
122
private function c3NotChanged ()
92
123
{
93
124
return file_exists (getcwd () . DIRECTORY_SEPARATOR . 'c3.php ' ) &&
@@ -99,9 +130,19 @@ public function deleteC3(PackageEvent $event)
99
130
if (!$ this ->isOperationOnC3 ($ event )) {
100
131
return ;
101
132
}
133
+ $ this ->deleteFile ();
134
+ }
135
+
136
+ private function deleteFile () {
102
137
if (file_exists (getcwd () . DIRECTORY_SEPARATOR . 'c3.php ' )) {
103
138
$ this ->io ->write ("<comment>[codeception/c3]</comment> Deleting c3.php from the root of your project... " );
104
139
unlink (getcwd () . DIRECTORY_SEPARATOR . 'c3.php ' );
105
140
}
106
141
}
142
+
143
+ private static function composerV2 ()
144
+ {
145
+ return Comparator::greaterThanOrEqualTo (PluginInterface::PLUGIN_API_VERSION , '2.0.0 ' );
146
+ }
147
+
107
148
}
0 commit comments