Added Develop twig extension with support for dd() and dump() functions
parent
c519be276a
commit
ba4db7a7ff
@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Renderer\Twig\Extensions;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Symfony\Component\VarDumper\VarDumper;
|
||||
use Twig\Extension\AbstractExtension as TwigExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class Develop extends TwigExtension
|
||||
{
|
||||
/** @var Config */
|
||||
protected $config;
|
||||
|
||||
/** @var VarDumper|null */
|
||||
protected $dumper;
|
||||
|
||||
/**
|
||||
* @param Config $config
|
||||
*/
|
||||
public function __construct(Config $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TwigFunction[]
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
if ($this->config->get('environment') != 'development') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
new TwigFunction('dump', [$this, 'dump'], ['is_safe' => ['html']]),
|
||||
new TwigFunction('dd', [$this, 'dd']),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $vars
|
||||
* @return string
|
||||
*/
|
||||
public function dump(...$vars): string
|
||||
{
|
||||
ob_start();
|
||||
|
||||
foreach ($vars as $v) {
|
||||
$this->dumper ? $this->dumper->dump($v) : var_dump($v);
|
||||
}
|
||||
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $vars
|
||||
* @return string
|
||||
*/
|
||||
public function dd(...$vars): string
|
||||
{
|
||||
$this->flushBuffers();
|
||||
|
||||
echo call_user_func_array([$this, 'dump'], $vars);
|
||||
|
||||
$this->exit();
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param VarDumper $dumper
|
||||
*/
|
||||
public function setDumper($dumper)
|
||||
{
|
||||
$this->dumper = $dumper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
protected function exit()
|
||||
{
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
protected function flushBuffers()
|
||||
{
|
||||
ob_end_flush();
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Renderer\Twig\Extensions\Develop;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Symfony\Component\VarDumper\VarDumper;
|
||||
|
||||
class DevelopTest extends ExtensionTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Develop::__construct
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Develop::getFunctions
|
||||
*/
|
||||
public function testGetGlobals()
|
||||
{
|
||||
$config = new Config();
|
||||
$extension = new Develop($config);
|
||||
|
||||
$functions = $extension->getFunctions();
|
||||
$this->assertEquals($functions, []);
|
||||
|
||||
$config->set('environment', 'development');
|
||||
$functions = $extension->getFunctions();
|
||||
$this->assertExtensionExists('dump', [$extension, 'dump'], $functions);
|
||||
$this->assertExtensionExists('dd', [$extension, 'dd'], $functions);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Develop::dump
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Develop::setDumper
|
||||
*/
|
||||
public function testDump()
|
||||
{
|
||||
$config = new Config();
|
||||
$varDumper = new VarDumper();
|
||||
$varDumper->setHandler(function ($var) {
|
||||
echo $var;
|
||||
});
|
||||
|
||||
$extension = new Develop($config);
|
||||
$extension->setDumper($varDumper);
|
||||
|
||||
$return = $extension->dump('Foo', 1234);
|
||||
$this->assertEquals('Foo1234', $return);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Renderer\Twig\Extensions\Develop::dd
|
||||
*/
|
||||
public function testDD()
|
||||
{
|
||||
/** @var Develop|MockObject $extension */
|
||||
$extension = $this->getMockBuilder(Develop::class)
|
||||
->onlyMethods(['exit', 'flushBuffers', 'dump'])
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$extension->expects($this->once())
|
||||
->method('exit');
|
||||
$extension->expects($this->once())
|
||||
->method('flushBuffers');
|
||||
$extension->expects($this->once())
|
||||
->method('dump')
|
||||
->with(123, 'Abc');
|
||||
|
||||
$return = $extension->dd(123, 'Abc');
|
||||
$this->assertEquals('', $return);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue