19
19
use Composer \DependencyResolver \Operation \UninstallOperation ;
20
20
use Composer \DependencyResolver \Operation \UpdateOperation ;
21
21
use Composer \DependencyResolver \Pool ;
22
+ use Composer \DependencyResolver \Transaction ;
22
23
use Composer \Downloader \FileDownloader ;
23
24
use Composer \EventDispatcher \EventSubscriberInterface ;
24
25
use Composer \Factory ;
35
36
use Composer \Package \BasePackage ;
36
37
use Composer \Package \Comparer \Comparer ;
37
38
use Composer \Package \Locker ;
39
+ use Composer \Package \Package ;
38
40
use Composer \Package \PackageInterface ;
39
41
use Composer \Plugin \PluginEvents ;
40
42
use Composer \Plugin \PluginInterface ;
45
47
use Composer \Repository \RepositoryManager ;
46
48
use Composer \Script \Event ;
47
49
use Composer \Script \ScriptEvents ;
50
+ use Composer \Semver \VersionParser ;
48
51
use Symfony \Component \Console \Input \ArgvInput ;
49
52
use Symfony \Component \Filesystem \Filesystem ;
50
53
use Symfony \Flex \Event \UpdateEvent ;
@@ -341,11 +344,34 @@ public function configureProject(Event $event)
341
344
342
345
public function record (PackageEvent $ event )
343
346
{
344
- if ($ this ->shouldRecordOperation ($ event )) {
347
+ if ($ this ->shouldRecordOperation ($ event-> getOperation (), $ event -> isDevMode (), $ event -> getComposer () )) {
345
348
$ this ->operations [] = $ event ->getOperation ();
346
349
}
347
350
}
348
351
352
+ public function recordOperations (InstallerEvent $ event )
353
+ {
354
+ if (!$ event ->isExecutingOperations ()) {
355
+ return ;
356
+ }
357
+
358
+ $ versionParser = new VersionParser ();
359
+ $ packages = [];
360
+ foreach ($ this ->lock ->all () as $ name => $ info ) {
361
+ $ packages [] = new Package ($ name , $ versionParser ->normalize ($ info ['version ' ]), $ info ['version ' ]);
362
+ }
363
+
364
+ $ transation = \Closure::bind (function () use ($ packages , $ event ) {
365
+ return new Transaction ($ packages , $ event ->getTransaction ()->resultPackageMap );
366
+ }, null , Transaction::class)();
367
+
368
+ foreach ($ transation ->getOperations () as $ operation ) {
369
+ if ($ this ->shouldRecordOperation ($ operation , $ event ->isDevMode (), $ event ->getComposer ())) {
370
+ $ this ->operations [] = $ operation ;
371
+ }
372
+ }
373
+ }
374
+
349
375
public function update (Event $ event , $ operations = [])
350
376
{
351
377
if ($ operations ) {
@@ -854,18 +880,21 @@ private function formatOrigin(Recipe $recipe): string
854
880
return sprintf ('<info>%s</> (<comment>>=%s</>): From %s ' , $ matches [1 ], $ matches [2 ], 'auto-generated recipe ' === $ matches [3 ] ? '<comment> ' .$ matches [3 ].'</> ' : $ matches [3 ]);
855
881
}
856
882
857
- private function shouldRecordOperation (PackageEvent $ event ): bool
883
+ private function shouldRecordOperation (OperationInterface $ operation , bool $ isDevMode , Composer $ composer = null ): bool
858
884
{
859
- $ operation = $ event ->getOperation ();
885
+ if ($ this ->dryRun ) {
886
+ return false ;
887
+ }
888
+
860
889
if ($ operation instanceof UpdateOperation) {
861
890
$ package = $ operation ->getTargetPackage ();
862
891
} else {
863
892
$ package = $ operation ->getPackage ();
864
893
}
865
894
866
895
// when Composer runs with --no-dev, ignore uninstall operations on packages from require-dev
867
- if (!$ event -> isDevMode () && $ operation instanceof UninstallOperation) {
868
- foreach ($ event -> getComposer ( )->getLocker ()->getLockData ()['packages-dev ' ] as $ p ) {
896
+ if (!$ isDevMode && $ operation instanceof UninstallOperation) {
897
+ foreach (( $ composer ?? $ this -> composer )->getLocker ()->getLockData ()['packages-dev ' ] as $ p ) {
869
898
if ($ package ->getName () === $ p ['name ' ]) {
870
899
return false ;
871
900
}
@@ -993,9 +1022,6 @@ public static function getSubscribedEvents(): array
993
1022
}
994
1023
995
1024
$ events = [
996
- PackageEvents::POST_PACKAGE_INSTALL => 'record ' ,
997
- PackageEvents::POST_PACKAGE_UPDATE => [['record ' ], ['enableThanksReminder ' ]],
998
- PackageEvents::POST_PACKAGE_UNINSTALL => 'record ' ,
999
1025
ScriptEvents::POST_CREATE_PROJECT_CMD => 'configureProject ' ,
1000
1026
ScriptEvents::POST_INSTALL_CMD => 'install ' ,
1001
1027
ScriptEvents::PRE_UPDATE_CMD => 'configureInstaller ' ,
@@ -1005,14 +1031,21 @@ public static function getSubscribedEvents(): array
1005
1031
1006
1032
if (version_compare ('2.0.0 ' , PluginInterface::PLUGIN_API_VERSION , '> ' )) {
1007
1033
$ events += [
1034
+ PackageEvents::POST_PACKAGE_INSTALL => 'record ' ,
1035
+ PackageEvents::POST_PACKAGE_UPDATE => [['record ' ], ['enableThanksReminder ' ]],
1036
+ PackageEvents::POST_PACKAGE_UNINSTALL => 'record ' ,
1008
1037
InstallerEvents::PRE_DEPENDENCIES_SOLVING => [['populateProvidersCacheDir ' , \PHP_INT_MAX ]],
1009
1038
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir ' , \PHP_INT_MAX ]],
1010
1039
PackageEvents::PRE_PACKAGE_INSTALL => [['populateFilesCacheDir ' , ~\PHP_INT_MAX ]],
1011
1040
PackageEvents::PRE_PACKAGE_UPDATE => [['populateFilesCacheDir ' , ~\PHP_INT_MAX ]],
1012
1041
PluginEvents::PRE_FILE_DOWNLOAD => 'onFileDownload ' ,
1013
1042
];
1014
1043
} else {
1015
- $ events += [PluginEvents::PRE_POOL_CREATE => 'truncatePackages ' ];
1044
+ $ events += [
1045
+ PackageEvents::POST_PACKAGE_UPDATE => 'enableThanksReminder ' ,
1046
+ InstallerEvents::PRE_OPERATIONS_EXEC => 'recordOperations ' ,
1047
+ PluginEvents::PRE_POOL_CREATE => 'truncatePackages ' ,
1048
+ ];
1016
1049
}
1017
1050
1018
1051
return $ events ;
0 commit comments