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