Skip to content

Instantly share code, notes, and snippets.

@bklein01
Created May 29, 2023 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bklein01/f5bcf353951e3d0a1b1ed0163d4e08e2 to your computer and use it in GitHub Desktop.
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
<?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