PluginProbe
Gmail SMTP / trunk
Gmail SMTP vtrunk
1.2.3.21 1.2.3.20 trunk 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.9 1.2.0 1.2.3.14 1.2.3.15 1.2.3.16 1.2.3.18 1.2.3.5
gmail-smtp / google-api-php-client / src / Task / Composer.php

Composer.php in Gmail SMTP trunk, at google-api-php-client/src/Task/Composer.php

116 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 * Copyright 2020 Google LLC
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy of
7 * the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17
18 namespace Google\Task;
19
20 use Composer\Script\Event;
21 use InvalidArgumentException;
22 use Symfony\Component\Filesystem\Filesystem;
23 use Symfony\Component\Finder\Finder;
24
25 class Composer
26 {
27 /**
28 * @param Event $event Composer event passed in for any script method
29 * @param Filesystem $filesystem Optional. Used for testing.
30 */
31 public static function cleanup(
32 Event $event,
33 ?Filesystem $filesystem = null
34 ) {
35 $composer = $event->getComposer();
36 $extra = $composer->getPackage()->getExtra();
37 $servicesToKeep = $extra['google/apiclient-services'] ?? [];
38 if (empty($servicesToKeep)) {
39 return;
40 }
41 $vendorDir = $composer->getConfig()->get('vendor-dir');
42 $serviceDir = sprintf(
43 '%s/google/apiclient-services/src/Google/Service',
44 $vendorDir
45 );
46 if (!is_dir($serviceDir)) {
47 // path for google/apiclient-services >= 0.200.0
48 $serviceDir = sprintf(
49 '%s/google/apiclient-services/src',
50 $vendorDir
51 );
52 }
53 self::verifyServicesToKeep($serviceDir, $servicesToKeep);
54 $finder = self::getServicesToRemove($serviceDir, $servicesToKeep);
55 $filesystem = $filesystem ?: new Filesystem();
56 $servicesToRemoveCount = $finder->count();
57 if (0 === $servicesToRemoveCount) {
58 return;
59 }
60 $event->getIO()->write(
61 sprintf('Removing %d google services', $servicesToRemoveCount)
62 );
63 $pathsToRemove = iterator_to_array($finder);
64 foreach ($pathsToRemove as $pathToRemove) {
65 $realpath = $pathToRemove->getRealPath();
66 $filesystem->remove($realpath);
67 $filesystem->remove($realpath . '.php');
68 }
69 }
70
71 /**
72 * @throws InvalidArgumentException when the service doesn't exist
73 */
74 private static function verifyServicesToKeep(
75 $serviceDir,
76 array $servicesToKeep
77 ) {
78 $finder = (new Finder())
79 ->directories()
80 ->depth('== 0');
81
82 foreach ($servicesToKeep as $service) {
83 if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) {
84 throw new InvalidArgumentException(
85 sprintf(
86 'Invalid Google service name "%s"',
87 $service
88 )
89 );
90 }
91 try {
92 $finder->in($serviceDir . '/' . $service);
93 } catch (InvalidArgumentException $e) {
94 throw new InvalidArgumentException(
95 sprintf(
96 'Google service "%s" does not exist or was removed previously',
97 $service
98 )
99 );
100 }
101 }
102 }
103
104 private static function getServicesToRemove(
105 $serviceDir,
106 array $servicesToKeep
107 ) {
108 // find all files in the current directory
109 return (new Finder())
110 ->directories()
111 ->depth('== 0')
112 ->in($serviceDir)
113 ->exclude($servicesToKeep);
114 }
115 }
116