1
+ <?php
2
+
3
+
4
+ namespace Safe ;
5
+
6
+
7
+ use http \Exception \RuntimeException ;
8
+ use Symfony \Component \Console \Command \Command ;
9
+ use Symfony \Component \Console \Input \InputArgument ;
10
+ use Symfony \Component \Console \Input \InputInterface ;
11
+ use Symfony \Component \Console \Output \OutputInterface ;
12
+
13
+ class DeprecateCommand extends Command
14
+ {
15
+ public const GENERATE_DIRECTORY = __DIR__ . '/../../generated/ ' ;
16
+ public const DEPRECATE_DIRECTORY = __DIR__ . '/../../deprecated/ ' ;
17
+
18
+ protected function configure (): void
19
+ {
20
+ $ this
21
+ ->setName ('deprecate ' )
22
+ ->setDescription ('Flag a module as deprecated and save it into the deprecated directory ' )
23
+ ->addArgument ('module ' , InputArgument::REQUIRED , 'the module to deprecate ' )
24
+ ;
25
+ }
26
+
27
+ protected function execute (InputInterface $ input , OutputInterface $ output )
28
+ {
29
+ $ moduleName = $ input ->getArgument ('module ' );
30
+
31
+ $ moduleFilePath = self ::GENERATE_DIRECTORY ."$ moduleName.php " ;
32
+ if (!\file_exists ($ moduleFilePath )) {
33
+ throw new \RuntimeException ("Module $ moduleName is not maintained! " );
34
+ }
35
+
36
+ $ output ->writeln ("Move $ moduleName.php to deprecated " );
37
+ $ success = \rename ($ moduleFilePath , self ::DEPRECATE_DIRECTORY ."$ moduleName.php " );
38
+ if (!$ success ) {
39
+ throw new \RuntimeException ("Could not move the file. " );
40
+ }
41
+
42
+ $ exceptionFilePath = self ::GENERATE_DIRECTORY .$ this ->getExceptionFilePath ($ moduleName );
43
+ $ moveException = false ;
44
+ if (\file_exists ($ exceptionFilePath )) {
45
+ $ moveException = true ;
46
+ $ output ->writeln ("Move exception file to deprecated " );
47
+ $ success = \rename ($ exceptionFilePath , self ::DEPRECATE_DIRECTORY .$ this ->getExceptionFilePath ($ moduleName ));
48
+ if (!$ success ) {
49
+ throw new \RuntimeException ("Could not move the file. " );
50
+ }
51
+ }
52
+
53
+ $ output ->writeln ('Editing composer.json ' );
54
+ $ this ->editComposerFile ($ moduleName , $ moveException );
55
+
56
+ $ generatedListFile = self ::GENERATE_DIRECTORY .'functionsList.php ' ;
57
+ $ deprecatedListFile = self ::DEPRECATE_DIRECTORY .'functionsList.php ' ;
58
+ $ output ->writeln ("Don't forget to edit $ generatedListFile and $ deprecatedListFile ! " );
59
+
60
+ return 0 ;
61
+ }
62
+
63
+ public static function getExceptionFilePath (string $ moduleName ): string
64
+ {
65
+ return "Exceptions/ " .ucfirst ($ moduleName )."Exception.php " ;
66
+ }
67
+
68
+
69
+ private function editComposerFile (string $ moduleName , bool $ moveException ): void
70
+ {
71
+
72
+ $ composerContent = file_get_contents (__DIR__ .'/../../composer.json ' );
73
+ if ($ composerContent === false ) {
74
+ throw new \RuntimeException ('Error while loading composer.json file for edition. ' );
75
+ }
76
+ $ composerJson = \json_decode ($ composerContent , true );
77
+ $ composerJson ['autoload ' ]['files ' ] = self ::editFileList ($ composerJson ['autoload ' ]['files ' ], $ moduleName );
78
+
79
+ $ newContent = json_encode ($ composerJson , \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES );
80
+ \file_put_contents (__DIR__ .'/../../composer.json ' , $ newContent );
81
+ }
82
+
83
+ /**
84
+ * @param string[] $fileList
85
+ * @return string[]
86
+ */
87
+ public static function editFileList (array $ fileList , string $ moduleName ): array
88
+ {
89
+ $ newList = [];
90
+ foreach ($ fileList as $ fileName ) {
91
+ if ($ fileName === "generated/ $ moduleName.php " ) {
92
+ $ newList [] = "deprecated/ $ moduleName.php " ;
93
+ //} else if($fileName === self::GENERATE_DIRECTORY.self::getExceptionFilePath($moduleName)) {
94
+ // $newList[] = self::DEPRECATE_DIRECTORY.self::getExceptionFilePath($moduleName);
95
+ } else {
96
+ $ newList [] = $ fileName ;
97
+ }
98
+ }
99
+
100
+ return $ newList ;
101
+ }
102
+
103
+ }
0 commit comments