8000 [ValueExporter] added CallableToStringFormatter · symfony/symfony@3a1d99b · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a1d99b

Browse files
committed
[ValueExporter] added CallableToStringFormatter
1 parent 21c94d1 commit 3a1d99b

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

src/Symfony/Component/ValueExporter/Exporter/ValueToStringExporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function exportValue($value, $depth = 1, $expand = false)
3030
// Therefor, \Traversable instances might be treated as objects unless
3131
// implementing a {@link StringFormatterInterface} and passing it to
3232
// the exporter in order to support them.
33-
if (is_array($value)) {
33+
if (is_array($value) && !is_callable($value)) {
3434
if (empty($value)) {
3535
return 'array()';
3636
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\ValueExporter\Formatter;
13+
14+
/**
15+
* Returns a string representation of a string or array callable.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class CallableToStringFormatter implements StringFormatterInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function supports($value)
25+
{
26+
return is_callable($value) && !$value instanceof \Closure;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function formatToString($value)
33+
{
34+
if (is_string($value)) {
35+
return sprintf('(function) "%s"', $value);
36+
}
37+
38+
$caller = is_object($value) ? get_class($value) : (is_object($value[0]) ? get_class($value[0]) : $value[0]);
39+
if (is_object($value)) {
40+
return sprintf('(invokable) "%s"', $caller);
41+
}
42+
43+
$method = $value[1];
44+
if (false !== $cut = strpos($method, $caller)) {
45+
$method = substr($method, $cut);
46+
}
47+
48+
return sprintf('(callable) "%s::%s"', $caller, $method);
49+
}
50+
}

src/Symfony/Component/ValueExporter/Tests/ValueExporterTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testExportValueExpanded()
3939

4040
$exportedValue = <<<EOT
4141
array(
42-
0 => array(0 => "Symfony\Component\ValueExporter\ValueExporter", 1 => "export")
42+
0 => (callable) "Symfony\Component\ValueExporter\ValueExporter::export"
4343
)
4444
EOT;
4545

@@ -68,6 +68,13 @@ public function valueProvider()
6868
array(0 => 0, '1' => 'un', 'key' => 4.5),
6969
'array(0 => (int) 0, 1 => "un", \'key\' => (float) 4.5)',
7070
),
71+
'closure' => array(function() {}, 'object(Closure)'),
72+
'callable string' => array('strlen', '(function) "strlen"'),
73+
'callable array' => array(
74+
array($this, 'testExportValue'),
75+
'(callable) "Symfony\Component\ValueExporter\Tests\ValueExporterTest::testExportValue"',
76+
),
77+
'invokable object' => array($this, '(invokable) "Symfony\Component\ValueExporter\Tests\ValueExporterTest"'),
7178
'datetime' => array(
7279
new \DateTime('2014-06-10 07:35:40', new \DateTimeZone('UTC')),
7380
'object(DateTime) - 2014-06-10T07:35:40+0000',
@@ -79,4 +86,9 @@ public function valueProvider()
7986
'php incomplete class' => array($foo, '__PHP_Incomplete_Class(AppBundle/Foo)'),
8087
);
8188
}
89+
90+
public function __invoke()
91+
{
92+
return 'TEST';
93+
}
8294
}

src/Symfony/Component/ValueExporter/ValueExporter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\ValueExporter\Exporter\ValueExporterInterface;
1515
use Symfony\Component\ValueExporter\Exporter\ValueToStringExporter;
16+
use Symfony\Component\ValueExporter\Formatter\CallableToStringFormatter;
1617
use Symfony\Component\ValueExporter\Formatter\DateTimeToStringFormatter;
1718
use Symfony\Component\ValueExporter\Formatter\FormatterInterface;
1819
use Symfony\Component\ValueExporter\Formatter\PhpIncompleteClassToStringFormatter;
@@ -35,6 +36,7 @@ public static function export($value, $depth = 1, $expand = false)
3536
{
3637
if (null === self::$handler) {
3738
$exporter = self::$exporter ?: new ValueToStringExporter(
39+
new CallableToStringFormatter(),
3840
new DateTimeToStringFormatter(),
3941
new PhpIncompleteClassToStringFormatter()
4042
);

0 commit comments

Comments
 (0)
0