|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RectorLaravel\Commands; |
| 6 | + |
| 7 | +use Nette\Utils\FileSystem; |
| 8 | +use Rector\Console\Command\CustomRuleCommand; |
| 9 | +use Symfony\Component\Finder\Finder; |
| 10 | +use Symfony\Component\Finder\SplFileInfo; |
| 11 | + |
| 12 | +/** |
| 13 | + * Modified version of |
| 14 | + * |
| 15 | + * @see CustomRuleCommand |
| 16 | + */ |
| 17 | +final class MakeRuleCommand |
| 18 | +{ |
| 19 | + public function execute(string $ruleName): int |
| 20 | + { |
| 21 | + $rectorName = $this->getRuleName($ruleName); |
| 22 | + $rulesNamespace = 'RectorLaravel\\Rector'; |
| 23 | + $testsNamespace = 'RectorLaravel\\Tests\\Rector'; |
| 24 | + |
| 25 | + // find all files in templates directory |
| 26 | + $finder = Finder::create() |
| 27 | + ->files() |
| 28 | + ->in(__DIR__ . '/../templates/new-rule') |
| 29 | + ->notName('__NAME__Test.php'); |
| 30 | + |
| 31 | + $finder->append([ |
| 32 | + new SplFileInfo( |
| 33 | + __DIR__ . '/../templates/new-rule/tests/Rector/__NAME__/__NAME__Test.php', |
| 34 | + 'tests/Rector/__NAME__', |
| 35 | + 'tests/Rector/__NAME__/__NAME__Test.php', |
| 36 | + ), |
| 37 | + ]); |
| 38 | + |
| 39 | + $currentDirectory = getcwd(); |
| 40 | + |
| 41 | + $generatedFilePaths = []; |
| 42 | + |
| 43 | + $fileInfos = iterator_to_array($finder->getIterator()); |
| 44 | + |
| 45 | + foreach ($fileInfos as $fileInfo) { |
| 46 | + $newContent = $this->replaceNameVariable($rectorName, $fileInfo->getContents()); |
| 47 | + $newContent = $this->replaceNamespaceVariable($rulesNamespace, $newContent); |
| 48 | + $newContent = $this->replaceTestsNamespaceVariable($testsNamespace, $newContent); |
| 49 | + $newFilePath = $this->replaceNameVariable($rectorName, $fileInfo->getRelativePathname()); |
| 50 | + |
| 51 | + FileSystem::write($currentDirectory . '/' . $newFilePath, $newContent, null); |
| 52 | + |
| 53 | + $generatedFilePaths[] = $newFilePath; |
| 54 | + } |
| 55 | + |
| 56 | + echo PHP_EOL . 'Generated files:' . PHP_EOL . PHP_EOL . "\t"; |
| 57 | + echo implode(PHP_EOL . "\t", $generatedFilePaths) . PHP_EOL; |
| 58 | + |
| 59 | + return 0; |
| 60 | + } |
| 61 | + |
| 62 | + private function getRuleName(string $ruleName): string |
| 63 | + { |
| 64 | + if (! str_ends_with($ruleName, 'Rector')) { |
| 65 | + $ruleName .= 'Rector'; |
| 66 | + } |
| 67 | + |
| 68 | + return ucfirst($ruleName); |
| 69 | + } |
| 70 | + |
| 71 | + private function replaceNameVariable(string $rectorName, string $contents): string |
| 72 | + { |
| 73 | + return str_replace('__NAME__', $rectorName, $contents); |
| 74 | + } |
| 75 | + |
| 76 | + private function replaceNamespaceVariable(string $namespace, string $contents): string |
| 77 | + { |
| 78 | + return str_replace('__NAMESPACE__', $namespace, $contents); |
| 79 | + } |
| 80 | + |
| 81 | + private function replaceTestsNamespaceVariable(string $testsNamespace, string $contents): string |
| 82 | + { |
| 83 | + return str_replace('__TESTS_NAMESPACE__', $testsNamespace, $contents); |
| 84 | + } |
| 85 | +} |
0 commit comments