Created
May 29, 2023 19:54
-
-
Save bklein01/f5bcf353951e3d0a1b1ed0163d4e08e2 to your computer and use it in GitHub Desktop.
Little Utility to convert a directory of standalone php files into a Symfony controller class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$targetDir = '../public/'; | |
$output = "<?php\n"; | |
$output .= "namespace App\Controller\n\n"; | |
$output .= "#[Route('/')]\n"; | |
$output .= "class NewClass() extends AbstractController {\n"; | |
$files = scandir($targetDir); | |
foreach ($files as $file) { | |
echo "Reading File: " . $targetDir . $file . "\n"; | |
if (is_file($targetDir . $file)) { | |
$content = file_get_contents($targetDir . $file); | |
$method = rtrim($file, '.php'); | |
$output .= "#[Route('/{$method}', name: 'newclass_{$method}', methods: ['GET', 'POST'])]\n"; | |
$output .= "public function {$method}(Request \$request, Connection \$connection): Response\n"; | |
$output .= "{\n"; | |
$output .= str_replace("?>", "", str_replace("<?php", "", $content)); | |
$output .= "}\n\n"; | |
} | |
} | |
$output .= "}"; | |
file_put_contents('NewClass.php', $output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment