Added Develop twig extension with support for dd() and dump() functions

main
Igor Scheller 5 years ago committed by msquare
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();
}
}

@ -8,12 +8,14 @@ use Engelsystem\Renderer\Twig\Extensions\Assets;
use Engelsystem\Renderer\Twig\Extensions\Authentication; use Engelsystem\Renderer\Twig\Extensions\Authentication;
use Engelsystem\Renderer\Twig\Extensions\Config; use Engelsystem\Renderer\Twig\Extensions\Config;
use Engelsystem\Renderer\Twig\Extensions\Csrf; use Engelsystem\Renderer\Twig\Extensions\Csrf;
use Engelsystem\Renderer\Twig\Extensions\Develop;
use Engelsystem\Renderer\Twig\Extensions\Globals; use Engelsystem\Renderer\Twig\Extensions\Globals;
use Engelsystem\Renderer\Twig\Extensions\Legacy; use Engelsystem\Renderer\Twig\Extensions\Legacy;
use Engelsystem\Renderer\Twig\Extensions\Markdown; use Engelsystem\Renderer\Twig\Extensions\Markdown;
use Engelsystem\Renderer\Twig\Extensions\Session; use Engelsystem\Renderer\Twig\Extensions\Session;
use Engelsystem\Renderer\Twig\Extensions\Translation; use Engelsystem\Renderer\Twig\Extensions\Translation;
use Engelsystem\Renderer\Twig\Extensions\Url; use Engelsystem\Renderer\Twig\Extensions\Url;
use Symfony\Component\VarDumper\VarDumper;
use Twig\Environment as Twig; use Twig\Environment as Twig;
use Twig\Extension\CoreExtension as TwigCore; use Twig\Extension\CoreExtension as TwigCore;
use Twig\Loader\LoaderInterface as TwigLoaderInterface; use Twig\Loader\LoaderInterface as TwigLoaderInterface;
@ -26,6 +28,7 @@ class TwigServiceProvider extends ServiceProvider
'authentication' => Authentication::class, 'authentication' => Authentication::class,
'config' => Config::class, 'config' => Config::class,
'csrf' => Csrf::class, 'csrf' => Csrf::class,
'develop' => Develop::class,
'globals' => Globals::class, 'globals' => Globals::class,
'session' => Session::class, 'session' => Session::class,
'legacy' => Legacy::class, 'legacy' => Legacy::class,
@ -51,6 +54,12 @@ class TwigServiceProvider extends ServiceProvider
foreach ($this->app->tagged('twig.extension') as $extension) { foreach ($this->app->tagged('twig.extension') as $extension) {
$renderer->addExtension($extension); $renderer->addExtension($extension);
} }
if (class_exists(VarDumper::class)) {
/** @var Develop $dev */
$dev = $this->app->get('twig.extension.develop');
$dev->setDumper($this->app->make(VarDumper::class));
}
} }
protected function registerTwigEngine() protected function registerTwigEngine()

@ -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);
}
}

@ -3,6 +3,7 @@
namespace Engelsystem\Test\Unit\Renderer; namespace Engelsystem\Test\Unit\Renderer;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Renderer\Twig\Extensions\Develop;
use Engelsystem\Renderer\TwigEngine; use Engelsystem\Renderer\TwigEngine;
use Engelsystem\Renderer\TwigLoader; use Engelsystem\Renderer\TwigLoader;
use Engelsystem\Renderer\TwigServiceProvider; use Engelsystem\Renderer\TwigServiceProvider;
@ -11,6 +12,7 @@ use PHPUnit\Framework\MockObject\MockObject;
use ReflectionClass as Reflection; use ReflectionClass as Reflection;
use ReflectionException; use ReflectionException;
use stdClass; use stdClass;
use Symfony\Component\VarDumper\VarDumper;
use Twig\Environment as Twig; use Twig\Environment as Twig;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\Extension\CoreExtension as TwigCore; use Twig\Extension\CoreExtension as TwigCore;
@ -70,22 +72,34 @@ class TwigServiceProviderTest extends ServiceProviderTest
$firsExtension = $this->getMockForAbstractClass(ExtensionInterface::class); $firsExtension = $this->getMockForAbstractClass(ExtensionInterface::class);
/** @var ExtensionInterface|MockObject $secondExtension */ /** @var ExtensionInterface|MockObject $secondExtension */
$secondExtension = $this->getMockForAbstractClass(ExtensionInterface::class); $secondExtension = $this->getMockForAbstractClass(ExtensionInterface::class);
/** @var Develop|MockObject $devExtension */
$devExtension = $this->createMock(Develop::class);
/** @var VarDumper|MockObject $dumper */
$dumper = $this->createMock(VarDumper::class);
$app = $this->getApp(['get', 'tagged']); $app = $this->getApp(['get', 'tagged', 'make']);
$app->expects($this->once()) $app->expects($this->exactly(2))
->method('get') ->method('get')
->with('twig.environment') ->withConsecutive(['twig.environment'], ['twig.extension.develop'])
->willReturn($twig); ->willReturnOnConsecutiveCalls($twig, $devExtension);
$app->expects($this->once()) $app->expects($this->once())
->method('tagged') ->method('tagged')
->with('twig.extension') ->with('twig.extension')
->willReturn([$firsExtension, $secondExtension]); ->willReturn([$firsExtension, $secondExtension]);
$app->expects($this->once())
->method('make')
->with(VarDumper::class)
->willReturn($dumper);
$twig->expects($this->exactly(2)) $twig->expects($this->exactly(2))
->method('addExtension') ->method('addExtension')
->withConsecutive([$firsExtension], [$secondExtension]); ->withConsecutive([$firsExtension], [$secondExtension]);
$devExtension->expects($this->once())
->method('setDumper')
->with($dumper);
$serviceProvider = new TwigServiceProvider($app); $serviceProvider = new TwigServiceProvider($app);
$serviceProvider->boot(); $serviceProvider->boot();
} }

Loading…
Cancel
Save