Skip to content

Commit

Permalink
Merge branch '6.2' into 6.3
Browse files Browse the repository at this point in the history
* 6.2:
  [Runtime][ErrorHandler] Don't mess with ini_set('assert.warning')
  [DependencyInjection] Fix fetching lazy non-shared services multiple times
  • Loading branch information
nicolas-grekas committed Jul 16, 2023
2 parents 359fbc1 + c2d9407 commit 431d321
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,8 @@ protected static function {$methodName}(\$container$lazyInitialization)

if ($asFile) {
$code .= "self::do(...);\n\n";
} elseif ($definition->isPublic()) {
$code .= sprintf("fn () => self::%s(\$container);\n\n", $methodName);
} else {
$code .= sprintf("self::%s(...);\n\n", $methodName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,42 @@ public function testInlinedDefinitionReferencingServiceContainer()
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services13.php', $dumper->dump(), '->dump() dumps inline definitions which reference service_container');
}

public function testNonSharedLazy()
{
$container = new ContainerBuilder();

$container
->register('foo', Foo::class)
->setFile(realpath(self::$fixturesPath.'/includes/foo_lazy.php'))
->setShared(false)
->setLazy(true)
->setPublic(true);

$container->compile();
$dumper = new PhpDumper($container);
$dump = $dumper->dump([
'class' => 'Symfony_DI_PhpDumper_Service_Non_Shared_Lazy',
'file' => __DIR__,
'inline_factories' => false,
'inline_class_loader' => false,
]);
$this->assertStringEqualsFile(
self::$fixturesPath.'/php/services_non_shared_lazy_public.php',
'\\' === \DIRECTORY_SEPARATOR ? str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump) : $dump
);
eval('?>'.$dump);

$container = new \Symfony_DI_PhpDumper_Service_Non_Shared_Lazy();

$foo1 = $container->get('foo');
$this->assertTrue($foo1->resetLazyObject());

$foo2 = $container->get('foo');
$this->assertTrue($foo2->resetLazyObject());

$this->assertNotSame($foo1, $foo2);
}

/**
* @testWith [false]
* [true]
Expand All @@ -775,7 +811,7 @@ public function testNonSharedLazyDefinitionReferences(bool $asGhostObject)
{
$container = new ContainerBuilder();
$container->register('foo', 'stdClass')->setShared(false)->setLazy(true);
$container->register('bar', 'stdClass')->addArgument(new Reference('foo', ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, false))->setPublic(true);
$container->register('bar', 'stdClass')->addArgument(new Reference('foo', ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE))->setPublic(true);
$container->compile();

$dumper = new PhpDumper($container);
Expand Down Expand Up @@ -1563,6 +1599,37 @@ public function testLazyWither()
$this->assertTrue($wither->resetLazyObject());
}

public function testLazyWitherNonShared()
{
$container = new ContainerBuilder();
$container->register(Foo::class);

$container
->register('wither', Wither::class)
->setShared(false)
->setLazy(true)
->setPublic(true)
->setAutowired(true);

$container->compile();
$dumper = new PhpDumper($container);
$dump = $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared']);
$this->assertStringEqualsFile(self::$fixturesPath.'/php/services_wither_lazy_non_shared.php', $dump);
eval('?>'.$dump);

$container = new \Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared();

$wither1 = $container->get('wither');
$this->assertInstanceOf(Foo::class, $wither1->foo);
$this->assertTrue($wither1->resetLazyObject());

$wither2 = $container->get('wither');
$this->assertInstanceOf(Foo::class, $wither2->foo);
$this->assertTrue($wither2->resetLazyObject());

$this->assertNotSame($wither1, $wither2);
}

public function testWitherWithStaticReturnType()
{
$container = new ContainerBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
*/
class Symfony_DI_PhpDumper_Service_Non_Shared_Lazy extends Container
{
protected $parameters = [];

public function __construct()
{
$this->services = $this->privates = [];
$this->methodMap = [
'foo' => 'getFooService',
];

$this->aliases = [];
}

public function compile(): void
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled(): bool
{
return true;
}

protected function createProxy($class, \Closure $factory)
{
return $factory();
}

/**
* Gets the public 'foo' service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\Foo
*/
protected static function getFooService($container, $lazyLoad = true)
{
$container->factories['foo'] ??= fn () => self::getFooService($container);

if (true === $lazyLoad) {
return $container->createProxy('FooGhost80f7cfc', static fn () => \FooGhost80f7cfc::createLazyGhost(static fn ($proxy) => self::getFooService($container, $proxy)));
}

static $include = true;

if ($include) {
include_once __DIR__.'/Fixtures/includes/foo_lazy.php';

$include = false;
}

return $lazyLoad;
}
}

class FooGhost80f7cfc extends \Symfony\Component\DependencyInjection\Tests\Compiler\Foo implements \Symfony\Component\VarExporter\LazyObjectInterface
{
use \Symfony\Component\VarExporter\LazyGhostTrait;

private const LAZY_OBJECT_PROPERTY_SCOPES = [];
}

// Help opcache.preload discover always-needed symbols
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

/**
* @internal This class has been auto-generated by the Symfony Dependency Injection Component.
*/
class Symfony_DI_PhpDumper_Service_Wither_Lazy_Non_Shared extends Container
{
protected $parameters = [];

public function __construct()
{
$this->services = $this->privates = [];
$this->methodMap = [
'wither' => 'getWitherService',
];

$this->aliases = [];
}

public function compile(): void
{
throw new LogicException('You cannot compile a dumped container that was already compiled.');
}

public function isCompiled(): bool
{
return true;
}

public function getRemovedIds(): array
{
return [
'Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo' => true,
];
}

protected function createProxy($class, \Closure $factory)
{
return $factory();
}

/**
* Gets the public 'wither' autowired service.
*
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\Wither
*/
protected static function getWitherService($container, $lazyLoad = true)
{
$container->factories['wither'] ??= fn () => self::getWitherService($container);

if (true === $lazyLoad) {
return $container->createProxy('WitherProxyDd381be', static fn () => \WitherProxyDd381be::createLazyProxy(static fn () => self::getWitherService($container, false)));
}

$instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\Wither();

$a = ($container->privates['Symfony\\Component\\DependencyInjection\\Tests\\Compiler\\Foo'] ??= new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo());

$instance = $instance->withFoo1($a);
$instance = $instance->withFoo2($a);
$instance->setFoo($a);

return $instance;
}
}

class WitherProxyDd381be extends \Symfony\Component\DependencyInjection\Tests\Compiler\Wither implements \Symfony\Component\VarExporter\LazyObjectInterface
{
use \Symfony\Component\VarExporter\LazyProxyTrait;

private const LAZY_OBJECT_PROPERTY_SCOPES = [
'foo' => [parent::class, 'foo', null],
];
}

// Help opcache.preload discover always-needed symbols
class_exists(\Symfony\Component\VarExporter\Internal\Hydrator::class);
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectRegistry::class);
class_exists(\Symfony\Component\VarExporter\Internal\LazyObjectState::class);
1 change: 0 additions & 1 deletion src/Symfony/Component/ErrorHandler/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static function enable(): ErrorHandler

@ini_set('zend.assertions', 1);
ini_set('assert.active', 1);
ini_set('assert.warning', 0);
ini_set('assert.exception', 1);

DebugClassLoader::enable();
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ public function testAssertQuietEval()
$this->markTestSkipped('zend.assertions is forcibly disabled');
}

set_error_handler(function () {});
$ini = [
ini_set('zend.assertions', 1),
ini_set('assert.active', 1),
Expand All @@ -627,6 +628,7 @@ public function testAssertQuietEval()
ini_set('assert.callback', null),
ini_set('assert.exception', 0),
];
restore_error_handler();

$logger = new BufferingLogger();
$handler = new ErrorHandler($logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static function register(bool $debug): void
if (0 <= \ini_get('zend.assertions')) {
ini_set('zend.assertions', 1);
ini_set('assert.active', $debug);
ini_set('assert.warning', 0);
ini_set('assert.exception', 1);
}

Expand Down

0 comments on commit 431d321

Please sign in to comment.