Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Script/Composer/Composer.php

raw symbols SCIP
1 <?php
2 namespace Aws\Script\Composer;
3
4 use Composer\Script\Event;
5 use Symfony\Component\Filesystem\Exception\IOException;
6 use Symfony\Component\Filesystem\Filesystem;
7
8 class Composer
9 {
10 private static array $unsafeForDeletion = [
11 'Kms' => true,
12 'S3' => true ,
13 'SSO' => true,
14 'SSOOIDC' => true,
15 'Sts' => true,
16 'Signin' => true
17 ];
18
19 public static function removeUnusedServicesInDev(Event $event, ?Filesystem $filesystem = null)
20 {
21 self::removeUnusedServicesWithConfig($event, $filesystem, true);
22 }
23
24 public static function removeUnusedServices(Event $event, ?Filesystem $filesystem = null)
25 {
26 self::removeUnusedServicesWithConfig($event, $filesystem, false);
27 }
28
29 private static function removeUnusedServicesWithConfig(Event $event, ?Filesystem $filesystem = null, $isDev = false)
30 {
31 if ($isDev && !$event->isDevMode()){
32 return;
33 }
34
35 $composer = $event->getComposer();
36 $extra = $composer->getPackage()->getExtra();
37 $listedServices = $extra['aws/aws-sdk-php'] ?? [];
38
39 if ($listedServices) {
40 $serviceMapping = self::buildServiceMapping();
41 self::verifyListedServices($serviceMapping, $listedServices);
42 $filesystem = $filesystem ?: new Filesystem();
43 $vendorPath = $composer->getConfig()->get('vendor-dir');
44 self::removeServiceDirs(
45 $event,
46 $filesystem,
47 $serviceMapping,
48 $listedServices,
49 $vendorPath
50 );
51 } else {
52 throw new \InvalidArgumentException(
53 'There are no services listed. Did you intend to use this script?'
54 );
55 }
56 }
57
58 public static function buildServiceMapping()
59 {
60 $serviceMapping = [];
61 $manifest = require(__DIR__ . '/../../data/manifest.json.php');
62
63 foreach ($manifest as $service => $attributes) {
64 $serviceMapping[$attributes['namespace']] = $service;
65 }
66
67 return $serviceMapping;
68 }
69
70 private static function verifyListedServices($serviceMapping, $listedServices)
71 {
72 foreach ($listedServices as $serviceToKeep) {
73 if (!isset($serviceMapping[$serviceToKeep])) {
74 throw new \InvalidArgumentException(
75 "'$serviceToKeep' is not a valid AWS service namespace. Please check spelling and casing."
76 );
77 }
78 }
79 }
80
81 private static function removeServiceDirs(
82 $event,
83 $filesystem,
84 $serviceMapping,
85 $listedServices,
86 $vendorPath
87 ) {
88 $unsafeForDeletion = self::$unsafeForDeletion;
89 if (in_array('DynamoDbStreams', $listedServices)) {
90 $unsafeForDeletion['DynamoDb'] = true;
91 }
92
93 $clientPath = $vendorPath . '/aws/aws-sdk-php/src/';
94 $modelPath = $clientPath . 'data/';
95 $deleteCount = 0;
96
97 foreach ($serviceMapping as $clientName => $modelName) {
98 if (!in_array($clientName, $listedServices) &&
99 !isset($unsafeForDeletion[$clientName])
100 ) {
101 $clientDir = $clientPath . $clientName;
102 $modelDir = $modelPath . $modelName;
103
104 if ($filesystem->exists([$clientDir, $modelDir])) {
105 $attempts = 3;
106 $delay = 2;
107
108 while ($attempts) {
109 try {
110 $filesystem->remove([$clientDir, $modelDir]);
111 $deleteCount++;
112 break;
113 } catch (IOException $e) {
114 $attempts--;
115
116 if (!$attempts) {
117 throw new IOException(
118 "Removal failed after several attempts. Last error: " . $e->getMessage()
119 );
120 } else {
121 sleep($delay);
122 $event->getIO()->write(
123 "Error encountered: " . $e->getMessage() . ". Retrying..."
124 );
125 $delay += 2;
126 }
127 }
128 }
129
130 }
131 }
132 }
133 $event->getIO()->write(
134 "Removed $deleteCount AWS service" . ($deleteCount === 1 ? '' : 's')
135 );
136 }
137 }