Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/VarExporter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add support for exporting named closures

7.3
---

Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/VarExporter/Internal/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
goto handle_value;
}

if ($value instanceof \Closure && !($r = new \ReflectionFunction($value))->isAnonymous()) {
$callable = [$r->getClosureThis() ?? $r->getClosureCalledClass()?->name, $r->name];
$r = $callable[0] ? new \ReflectionMethod(...$callable) : null;
$value = new NamedClosure(self::prepare($callable, $objectsPool, $refsPool, $objectsCount, $valueIsStatic), $r);

goto handle_value;
}

$class = $value::class;
$reflector = Registry::$reflectors[$class] ??= Registry::getClassReflector($class);
$properties = [];
Expand Down Expand Up @@ -217,6 +225,19 @@ public static function export($value, $indent = '')
}
$subIndent = $indent.' ';

if ($value instanceof NamedClosure) {
if ($value->method?->isPublic() ?? true) {
return match (true) {
null === $value->callable[0] => '\\'.$value->callable[1],
\is_string($value->callable[0]) => '\\'.$value->callable[0].'::'.$value->callable[1],
\is_object($value->callable[0]) => self::export($value->callable[0], $subIndent).'->'.$value->callable[1],
}.'(...)';
}

return 'new \ReflectionMethod(\\'.$value->method->class.'::class, '.self::export($value->callable[1]).')'
.'->getClosure('.(\is_object($value->callable[0]) ? self::export($value->callable[0]) : '').')';
}

if (\is_string($value)) {
$code = \sprintf("'%s'", addcslashes($value, "'\\"));

Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/VarExporter/Internal/NamedClosure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Internal;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class NamedClosure
{
public function __construct(
public readonly array $callable,
public readonly ?\ReflectionMethod $method = null,
) {
}
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/VarExporter/Tests/Fixtures/PrivateFCC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Tests\Fixtures;

#[\Attribute(\Attribute::TARGET_CLASS)]
#[PrivateFCC(self::testMethod(...))]
class PrivateFCC
{
private static function testMethod()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return \Symfony\Component\VarExporter\Internal\Hydrator::hydrate(
$o = [
clone (\Symfony\Component\VarExporter\Internal\Registry::$prototypes['Symfony\\Component\\VarExporter\\Tests\\TestClass'] ?? \Symfony\Component\VarExporter\Internal\Registry::p('Symfony\\Component\\VarExporter\\Tests\\TestClass')),
],
null,
[],
$o[0]->testMethod(...),
[]
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return \Symfony\Component\VarExporter\Tests\TestClass::testStaticMethod(...);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return new \ReflectionMethod(\Symfony\Component\VarExporter\Tests\Fixtures\PrivateFCC::class, 'testMethod')->getClosure();
27 changes: 25 additions & 2 deletions src/Symfony/Component/VarExporter/Tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\VarExporter\Tests\Fixtures\FooSerializable;
use Symfony\Component\VarExporter\Tests\Fixtures\FooUnitEnum;
use Symfony\Component\VarExporter\Tests\Fixtures\MySerializable;
use Symfony\Component\VarExporter\Tests\Fixtures\PrivateFCC;
use Symfony\Component\VarExporter\VarExporter;

class VarExporterTest extends TestCase
Expand Down Expand Up @@ -80,7 +81,7 @@ public static function provideFailingSerialization()
public function testExport(string $testName, $value, bool $staticValueExpected = false)
{
$dumpedValue = $this->getDump($value);
$isStaticValue = true;
$isStaticValue = null;
$marshalledValue = VarExporter::export($value, $isStaticValue);

$this->assertSame($staticValueExpected, $isStaticValue);
Expand All @@ -99,7 +100,7 @@ public function testExport(string $testName, $value, bool $staticValueExpected =
}
$marshalledValue = include $fixtureFile;

if (!$isStaticValue) {
if (!$isStaticValue || 'named-closure-static' === $testName || 'private-fcc' === $testName) {
if ($value instanceof MyWakeup) {
$value->bis = null;
}
Expand Down Expand Up @@ -234,11 +235,20 @@ public static function provideExport()
yield ['unit-enum', [FooUnitEnum::Bar], true];
yield ['readonly', new FooReadonly('k', 'v')];

yield ['named-closure-method', (new TestClass())->testMethod(...)];
yield ['named-closure-static', TestClass::testStaticMethod(...), true];

if (\PHP_VERSION_ID < 80400) {
return;
}

yield ['backed-property', new BackedProperty('name')];

if (\PHP_VERSION_ID < 80500) {
return;
}

yield ['private-fcc', (new \ReflectionClass(PrivateFCC::class))->getAttributes(PrivateFCC::class)[0]->getArguments()[0], true];
}

public function testUnicodeDirectionality()
Expand Down Expand Up @@ -299,6 +309,19 @@ private function __construct($prop)
}
}

class TestClass
{
public function testMethod()
{
return 'test';
}

public static function testStaticMethod()
{
return 'test';
}
}

class MyPrivateValue
{
protected $prot;
Expand Down
Loading