66use  Composer \DependencyResolver \Operation \UpdateOperation ;
77use  Composer \EventDispatcher \EventSubscriberInterface ;
88use  Composer \Installer \PackageEvent ;
9+ use  Composer \Installer \PackageEvents ;
910use  Composer \IO \IOInterface ;
1011use  Composer \Plugin \PluginInterface ;
1112use  Composer \Script \Event ;
1213use  Composer \Script \ScriptEvents ;
14+ use  Composer \Semver \Comparator ;
1315
1416class  Installer implements  PluginInterface, EventSubscriberInterface
1517{
@@ -22,9 +24,20 @@ public function activate(Composer $composer, IOInterface $io)
2224 {
2325 $ this  ->io  = $ io ;
2426 }
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+ 
2636 protected  function  isOperationOnC3 (PackageEvent   $ event )
2737 {
38+  if  (static ::composerV2 ()) {
39+  return  true ;
40+  }
2841 $ name  = '' ;
2942
3043 if  ($ event ->getOperation () instanceof  InstallOperation) {
@@ -40,34 +53,50 @@ protected function isOperationOnC3(PackageEvent $event)
4053
4154 public  static  function  getSubscribedEvents ()
4255 {
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+  }
4366 return  [
44-  ScriptEvents ::POST_PACKAGE_INSTALL  => [
67+  PackageEvents ::POST_PACKAGE_INSTALL  => [
4568 ['copyC3 ' , 0 ]
4669 ],
47-  ScriptEvents ::POST_PACKAGE_UPDATE  => [
70+  PackageEvents ::POST_PACKAGE_UPDATE  => [
4871 ['askForUpdate ' , 0 ]
4972 ],
50-  ScriptEvents ::POST_PACKAGE_UNINSTALL  => [
73+  PackageEvents ::POST_PACKAGE_UNINSTALL  => [
5174 ['deleteC3 ' , 0 ]
5275 ]
5376 ];
5477 }
5578
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- 
6279 public  function  copyC3 (PackageEvent   $ event )
6380 {
6481 if  (!$ this  ->isOperationOnC3 ($ event )) {
6582 return ;
6683 }
84+ 
85+  $ this  ->copyC3V2 (null );
86+  }
87+ 
88+  public  function  copyC3V2 (Event   $ event )
89+  {
6790 if  ($ this  ->c3NotChanged ()) {
6891 $ this  ->io ->write ("<comment>[codeception/c3]</comment> c3.php is already up-to-date " );
6992 return ;
7093 }
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+  }
71100
72101 $ this  ->io ->write ("<comment>[codeception/c3]</comment> Copying c3.php to the root of your project... " );
73102 copy (__DIR__  . DIRECTORY_SEPARATOR  . 'c3.php ' , getcwd () . DIRECTORY_SEPARATOR .'c3.php ' );
@@ -79,15 +108,17 @@ public function askForUpdate(PackageEvent $event)
79108 if  (!$ this  ->isOperationOnC3 ($ event ) || $ this  ->c3NotChanged ()) {
80109 return ;
81110 }
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-  }
88111 $ this  ->copyC3 ($ event );
89112 }
90113
114+  public  function  askForUpdateV2 (Event   $ event )
115+  {
116+  if  ($ this  ->c3NotChanged ()) {
117+  return ;
118+  }
119+  $ this  ->copyC3V2 ($ event );
120+  }
121+ 
91122 private  function  c3NotChanged ()
92123 {
93124 return  file_exists (getcwd () . DIRECTORY_SEPARATOR  . 'c3.php ' ) &&
@@ -99,9 +130,19 @@ public function deleteC3(PackageEvent $event)
99130 if  (!$ this  ->isOperationOnC3 ($ event )) {
100131 return ;
101132 }
133+  $ this  ->deleteFile ();
134+  }
135+ 
136+  private  function  deleteFile () {
102137 if  (file_exists (getcwd () . DIRECTORY_SEPARATOR  . 'c3.php ' )) {
103138 $ this  ->io ->write ("<comment>[codeception/c3]</comment> Deleting c3.php from the root of your project... " );
104139 unlink (getcwd () . DIRECTORY_SEPARATOR  . 'c3.php ' );
105140 }
106141 }
142+ 
143+  private  static  function  composerV2 ()
144+  {
145+  return  Comparator::greaterThanOrEqualTo (PluginInterface::PLUGIN_API_VERSION , '2.0.0 ' );
146+  }
147+ 
107148}
0 commit comments