Skip to content

Commit c8d420d

Browse files
committed
[VarDumper] added support for Imagine/Image
1 parent 9216cb7 commit c8d420d

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ CHANGELOG
44
4.4.0
55
-----
66

7-
* added `VarDumperTestTrait::setUpVarDumper()` and `VarDumperTestTrait::tearDownVarDumper()`
7+
* added `VarDumperTestTrait::setUpVarDumper()` and `VarDumperTestTrait::tearDownVarDumper()`
88
to configure casters & flags to use in tests
9+
* added `ImagineCaster` and infrastructure to dump images
910

1011
4.3.0
1112
-----
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
use Imagine\Image\ImageInterface;
15+
use Symfony\Component\VarDumper\Cloner\Stub;
16+
17+
/**
18+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
19+
*/
20+
class ImagineCaster
21+
{
22+
public static function castImage(ImageInterface $c, array $a, Stub $stub, $isNested)
23+
{
24+
$imgData = $c->get('png');
25+
if (\strlen($imgData) > 1 * 1000 * 1000) {
26+
$a += [
27+
Caster::PREFIX_VIRTUAL.'image' => new ConstStub($c->getSize()),
28+
];
29+
} else {
30+
$a += [
31+
Caster::PREFIX_VIRTUAL.'image' => new ImgStub($imgData, 'image/png', $c->getSize()),
32+
];
33+
}
34+
35+
return $a;
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Caster;
13+
14+
/**
15+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
16+
*/
17+
class ImgStub extends ConstStub
18+
{
19+
public function __construct(string $data, string $contentType, string $size)
20+
{
21+
$this->value = '';
22+
$this->attr['img-data'] = $data;
23+
$this->attr['img-size'] = $size;
24+
$this->attr['content-type'] = $contentType;
25+
}
26+
}

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ abstract class AbstractCloner implements ClonerInterface
8787
'Symfony\Component\VarDumper\Cloner\AbstractCloner' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
8888
'Symfony\Component\ErrorHandler\Exception\SilencedErrorContext' => ['Symfony\Component\VarDumper\Caster\ExceptionCaster', 'castSilencedErrorContext'],
8989

90+
'Imagine\Image\ImageInterface' => ['Symfony\Component\VarDumper\Caster\ImagineCaster', 'castImage'],
91+
9092
'ProxyManager\Proxy\ProxyInterface' => ['Symfony\Component\VarDumper\Caster\ProxyManagerCaster', 'castProxy'],
9193
'PHPUnit_Framework_MockObject_MockObject' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],
9294
'Prophecy\Prophecy\ProphecySubjectInterface' => ['Symfony\Component\VarDumper\Caster\StubCaster', 'cutInternals'],

src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,13 @@ function showCurrent(state)
682682
outline: none;
683683
color: inherit;
684684
}
685+
pre.sf-dump img {
686+
max-width: 50em;
687+
max-height: 50em;
688+
margin: .5em 0 0 0;
689+
padding: 0;
690+
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAAAAAA6mKC9AAAAHUlEQVQY02O8zAABilCaiQEN0EeA8QuUcX9g3QEAAjcC5piyhyEAAAAASUVORK5CYII=) #D3D3D3;
691+
}
685692
pre.sf-dump .sf-dump-ellipsis {
686693
display: inline-block;
687694
overflow: visible;
@@ -792,6 +799,23 @@ function showCurrent(state)
792799
return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
793800
}
794801

802+
/**
803+
* {@inheritdoc}
804+
*/
805+
public function dumpString(Cursor $cursor, $str, $bin, $cut)
806+
{
807+
if ('' === $str && isset($cursor->attr['img-data'], $cursor->attr['content-type'])) {
808+
$this->dumpKey($cursor);
809+
$this->line .= $this->style('default', $cursor->attr['img-size'] ?? '', []).' <samp>';
810+
$this->endValue($cursor);
811+
$this->line .= $this->indentPad;
812+
$this->line .= sprintf('<img src="data:%s;base64,%s" /></samp>', $cursor->attr['content-type'], base64_encode($cursor->attr['img-data']));
813+
$this->endValue($cursor);
814+
} else {
815+
parent::dumpString($cursor, $str, $bin, $cut);
816+
}
817+
}
818+
795819
/**
796820
* {@inheritdoc}
797821
*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy