Skip to content

Commit b5bb338

Browse files
committed
basic deprecate command
1 parent f781b93 commit b5bb338

File tree

5 files changed

+142
-0
lines changed

5 files changed

+142
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"psr-4": {
77
"Safe\\": [
88
"lib/",
9+
"deprecated/",
910
"generated/"
1011
]
1112
},

deprecated/functionsList.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
return [
4+
5+
];

generator/safe.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
use Safe\GenerateCommand;
88
use Safe\ScanObjectsCommand;
9+
use Safe\DeprecateCommand;
910
use Symfony\Component\Console\Application;
1011

1112
$application = new Application();
1213
$application->addCommands([new GenerateCommand()]);
1314
$application->addCommands([new ScanObjectsCommand()]);
15+
$application->addCommands([new DeprecateCommand()]);
1416

1517
$application->run();

generator/src/DeprecateCommand.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
4+
namespace Safe;
5+
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
class DeprecateCommandTest extends TestCase
10+
{
11+
public function testExceptionName()
12+
{
13+
$this->assertEquals('Exceptions/ApcException.php', DeprecateCommand::getExceptionFilePath('apc'));
14+
}
15+
16+
public function testFileListEdition()
17+
{
18+
$oldList = [
19+
"generated/apache.php",
20+
"generated/apc.php",
21+
];
22+
23+
$newList = DeprecateCommand::editFileList($oldList, 'apc');
24+
25+
$this->assertEquals([
26+
"generated/apache.php",
27+
"deprecated/apc.php",
28+
], $newList);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)