Last active
September 15, 2022 19:25
-
-
Save ajdinmore/c2d552e839bae5b0e946a1ecd050fd86 to your computer and use it in GitHub Desktop.
Symfony Dev Controller Template
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 /** @noinspection PhpMultipleClassesDeclarationsInOneFile */ | |
declare(strict_types=1); | |
namespace App\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Routing\Annotation\Route; | |
#[Route('/_dev')] | |
class DevController extends AbstractController | |
{ | |
use DevControllerTrait; | |
#[Route('/')] | |
public function action(Request $request): Response | |
{ | |
return $this->dump(...get_defined_vars()); | |
} | |
} | |
trait DevControllerTrait { | |
private function dump(mixed ...$data): Response | |
{ | |
$dump = array_is_list($data) ? '{{ dump(datum) }}' : '<p>{{ index }}</p>{{ dump(datum) }}'; | |
/** @noinspection PhpUnhandledExceptionInspection */ | |
return $this->html( | |
$this->container->get('twig') | |
->createTemplate("{% for index, datum in data %}$dump{% endfor %}") | |
->render(['data' => $data]), | |
); | |
} | |
private function text(string $content): Response | |
{ | |
return new Response($content, Response::HTTP_OK, ['Content-Type' => 'text/plain']); | |
} | |
private function html(string $content, string $title = self::class): Response | |
{ | |
return new Response( | |
sprintf( | |
'<html lang="en"><head><title>%s</title></head><body style="background:#111;color:#ccc;font-family:sans-serif">%s</body></html>', | |
$title, | |
$content, | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment