src/Controller/DumpController.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\CodeblockType;
  4. use App\Form\VardumpType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. class DumpController extends AbstractController
  10. {
  11.     #[Route('/var_dump'name'var_dump')]
  12.     public function index(Request $request): Response
  13.     {
  14.         $parameters = [
  15.             'controller_name' => 'DumpController',
  16.         ];
  17.         $form $this->createForm(VardumpType::class);
  18.         $form->handleRequest($request);
  19.         if($form->isSubmitted() && $form->isValid())
  20.         {
  21.             $varDump $form->getData();
  22.             $str $varDump['code'];
  23.             if($form->get('beautify')->isClicked())
  24.                 $str $this->beautify($str);
  25.             $parameters['codeResult'] = $str;
  26.         }
  27.         $parameters['vardumpForm'] = $form->createView();
  28.         return $this->render('dump/var_dump.html.twig'$parameters);
  29.     }
  30.     function beautify($c$label=''$return false) {
  31.         $c preg_replace("/\r\n|\r/""\n"$c);
  32.         $c str_replace("]=>\n"'] = '$c);
  33.         $c preg_replace('/= {2,}/''= '$c);
  34.         $c preg_replace("/\[\"(.*?)\"\] = /i""[$1] = "$c);
  35.         $c preg_replace('/  /'"    "$c);
  36.         $c preg_replace("/\"\"(.*?)\"/i""\"$1\""$c);
  37.         $c preg_replace("/(int|float)\(([0-9\.]+)\)/i""$1() <span class=\"number\">$2</span>"$c);
  38.         // Syntax Highlighting of Strings. This seems cryptic, but it will also allow non-terminated strings to get parsed.
  39.         $c preg_replace("/(\[[\w ]+\] = string\([0-9]+\) )\"(.*?)/sim""$1<span class=\"string\">\""$c);
  40.         $c preg_replace("/(\"\n{1,})( {0,}\})/sim""$1</span>$2"$c);
  41.         $c preg_replace("/(\"\n{1,})( {0,}\[)/sim""$1</span>$2"$c);
  42.         $c preg_replace("/(string\([0-9]+\) )\"(.*?)\"\n/sim""$1<span class=\"string\">\"$2\"</span>\n"$c);
  43.         $regex = array(
  44.             // Numberrs
  45.             'numbers' => array('/(^|] = )(array|float|int|string|resource|object\(.*\)|\&amp;object\(.*\))\(([0-9\.]+)\)/i''$1$2(<span class="number">$3</span>)'),
  46.             // Keywords
  47.             'null' => array('/(^|] = )(null)/i''$1<span class="keyword">$2</span>'),
  48.             'bool' => array('/(bool)\((true|false)\)/i''$1(<span class="keyword">$2</span>)'),
  49.             // Types
  50.             'types' => array('/(of type )\((.*)\)/i''$1(<span class="type">$2</span>)'),
  51.             // Objects
  52.             'object' => array('/(object|\&amp;object)\(([\w]+)\)/i''$1(<span class="object">$2</span>)'),
  53.             // Function
  54.             'function' => array('/(^|] = )(array|string|int|float|bool|resource|object|\&amp;object)\(/i''$1<span class="function">$2</span>('),
  55.         );
  56.         foreach ($regex as $x) {
  57.             $c preg_replace($x[0], $x[1], $c);
  58.         }
  59.         $style '
  60.     /* outside div - it will float and match the screen */
  61.     
  62.     /* inside div */
  63.     .dumpr {
  64.         color:#000000;
  65.         background-color: #fcfcfc;
  66.         border: 1px solid #d9d9d9;
  67.     }
  68.     /* syntax highlighting */
  69.     .dumpr span.string {color: #c40000;}
  70.     .dumpr span.number {color: #ff0000;}
  71.     .dumpr span.keyword {color: #007200;}
  72.     .dumpr span.function {color: #0000c4;}
  73.     .dumpr span.object {color: #ac00ac;}
  74.     .dumpr span.type {color: #0072c4;}
  75.     ';
  76.         $style preg_replace("/ {2,}/"""$style);
  77.         $style preg_replace("/\t|\r\n|\r|\n/"""$style);
  78.         $style preg_replace("/\/\*.*?\*\//i"''$style);
  79.         $style str_replace('}''} '$style);
  80.         $style str_replace(' {''{'$style);
  81.         $style trim($style);
  82.         $c trim($c);
  83.         $c preg_replace("/\n<\/span>/""</span>\n"$c);
  84.         return '<style>'.$style.'</style><div class="dumpr">'.nl2br($c).'</div>';
  85.     }
  86. }