Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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