Proyectos de Subversion Moodle

Rev

| 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;
5
use Symfony\Component\Filesystem\Filesystem;
6
 
7
class Composer
8
{
9
    public static function removeUnusedServices(
10
        Event      $event,
11
        Filesystem $filesystem = null
12
    )
13
    {
14
        $composer = $event->getComposer();
15
        $extra = $composer->getPackage()->getExtra();
16
        $listedServices = isset($extra['aws/aws-sdk-php'])
17
            ? $extra['aws/aws-sdk-php']
18
            : [];
19
 
20
        if ($listedServices) {
21
            $serviceMapping = self::buildServiceMapping();
22
            self::verifyListedServices($serviceMapping, $listedServices);
23
            $filesystem = $filesystem ?: new Filesystem();
24
            $vendorPath = $composer->getConfig()->get('vendor-dir');
25
            self::removeServiceDirs(
26
                $event,
27
                $filesystem,
28
                $serviceMapping,
29
                $listedServices,
30
                $vendorPath
31
            );
32
        } else {
33
            throw new \InvalidArgumentException(
34
                'There are no services listed. Did you intend to use this script?'
35
            );
36
        }
37
    }
38
 
39
    public static function buildServiceMapping()
40
    {
41
        $serviceMapping = [];
42
        $manifest = require(__DIR__ . '/../../data/manifest.json.php');
43
 
44
        foreach ($manifest as $service => $attributes) {
45
            $serviceMapping[$attributes['namespace']] = $service;
46
        }
47
 
48
        return $serviceMapping;
49
    }
50
 
51
    private static function verifyListedServices($serviceMapping, $listedServices)
52
    {
53
        foreach ($listedServices as $serviceToKeep) {
54
            if (!isset($serviceMapping[$serviceToKeep])) {
55
                throw new \InvalidArgumentException(
56
                    "'$serviceToKeep' is not a valid AWS service namespace. Please check spelling and casing."
57
                );
58
            }
59
        }
60
    }
61
 
62
    private static function removeServiceDirs(
63
        $event,
64
        $filesystem,
65
        $serviceMapping,
66
        $listedServices,
67
        $vendorPath
68
    ) {
69
        $unsafeForDeletion = ['Kms', 'S3', 'SSO', 'SSOOIDC', 'Sts'];
70
        if (in_array('DynamoDbStreams', $listedServices)) {
71
            $unsafeForDeletion[] = 'DynamoDb';
72
        }
73
 
74
        $clientPath = $vendorPath . '/aws/aws-sdk-php/src/';
75
        $modelPath = $clientPath . 'data/';
76
        $deleteCount = 0;
77
 
78
        foreach ($serviceMapping as $clientName => $modelName) {
79
            if (!in_array($clientName, $listedServices) &&
80
                !in_array($clientName, $unsafeForDeletion)
81
            ) {
82
                $clientDir = $clientPath . $clientName;
83
                $modelDir = $modelPath . $modelName;
84
 
85
                if ($filesystem->exists([$clientDir, $modelDir])) {
86
                    $filesystem->remove([$clientDir, $modelDir]);;
87
                    $deleteCount++;
88
                }
89
            }
90
        }
91
        $event->getIO()->write(
92
            "Removed $deleteCount AWS service" . ($deleteCount === 1 ? '' : 's')
93
        );
94
    }
95
}