Skip to content

Commit 26f8dcb

Browse files
committed
[VarDumper] added support for Imagine/Image
1 parent cf4d945 commit 26f8dcb

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

src/Symfony/Component/VarDumper/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* added `ImagineCaster` and infrastructure to dump image
8+
49
4.3.0
510
-----
611

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
$a += [
25+
Caster::PREFIX_VIRTUAL.'image' => new ImgStub($c->get('png'), 'image/png'),
26+
];
27+
28+
return $a;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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)
20+
{
21+
if (\strlen(bin2hex($data)) > 1 * 1000 * 1000) {
22+
$this->value = 'this picture is too heavy and can not be rendered';
23+
24+
return;
25+
}
26+
27+
$this->value = '';
28+
$this->attr['img-data'] = $data;
29+
$this->attr['content-type'] = $contentType;
30+
}
31+
}

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

+2
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

+24-5
Original file line numberDiff line numberDiff line change
@@ -473,15 +473,15 @@ function xpathHasClass(className) {
473473
return this.current();
474474
}
475475
this.idx = this.idx < (this.nodes.length - 1) ? this.idx + 1 : 0;
476-
476+
477477
return this.current();
478478
},
479479
previous: function () {
480480
if (this.isEmpty()) {
481481
return this.current();
482482
}
483483
this.idx = this.idx > 0 ? this.idx - 1 : (this.nodes.length - 1);
484-
484+
485485
return this.current();
486486
},
487487
isEmpty: function () {
@@ -565,11 +565,11 @@ function showCurrent(state)
565565
"sf-dump-protected",
566566
"sf-dump-private",
567567
].map(xpathHasClass).join(' or ');
568-
568+
569569
var xpathResult = doc.evaluate('.//span[' + classMatches + '][contains(translate(child::text(), ' + xpathString(searchQuery.toUpperCase()) + ', ' + xpathString(searchQuery.toLowerCase()) + '), ' + xpathString(searchQuery.toLowerCase()) + ')]', root, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
570570
571571
while (node = xpathResult.iterateNext()) state.nodes.push(node);
572-
572+
573573
showCurrent(state);
574574
}, 400);
575575
});
@@ -596,7 +596,7 @@ function showCurrent(state)
596596
*/
597597
return;
598598
}
599-
599+
600600
e.preventDefault();
601601
search.className = search.className.replace(/\bsf-dump-search-hidden\b/, '');
602602
searchInput.focus();
@@ -792,6 +792,25 @@ function showCurrent(state)
792792
return $this->dumpHeader = preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
793793
}
794794

795+
/**
796+
* {@inheritdoc}
797+
*/
798+
public function dumpString(Cursor $cursor, $str, $bin, $cut)
799+
{
800+
if (isset($cursor->attr['img-data'], $cursor->attr['content-type'])) {
801+
$this->line .= '<span>';
802+
$this->dumpKey($cursor);
803+
// we can not do fancy CSS here since we are in a <pre>
804+
$this->line .= "\n";
805+
$this->line .= str_repeat(' ', $cursor->depth + \strlen('image: '));
806+
$this->line .= sprintf('<img style="max-width:50em;max-height:50em;"src="data:%s;base64,%s" />', $cursor->attr['content-type'], base64_encode($cursor->attr['img-data']));
807+
$this->endValue($cursor);
808+
$this->line .= '</span>';
809+
} else {
810+
parent::dumpString($cursor, $str, $bin, $cut);
811+
}
812+
}
813+
795814
/**
796815
* {@inheritdoc}
797816
*/

0 commit comments

Comments
 (0)