Skip to content

Commit

Permalink
Merge branch '7.1' into 7.2
Browse files Browse the repository at this point in the history
* 7.1:
  initialize RedisAdapter cursor to 0
  do not skip tests from data providers
  ensure compatibility with Twig 3.15
  [Mime] fix encoding issue with UTF-8 addresses containing doubles spaces
  fix translation file syntax
  [Notifier] Improve Telegrams markdown escaping
  [Validator] [Choice] Fix callback option if not array returned
  [DependencyInjection] Fix linting factories implemented via __callStatic
  [DependencyInjection] Fix replacing abstract arguments with bindings
  [DependencyInjection] Fix parsing nested AutowireInline attributes
  Minor fixes around parse_url() checks
  Ensure compatibility with mongodb v2
  Add missing translations for Turkish (tr)
  • Loading branch information
nicolas-grekas committed Oct 25, 2024
2 parents 550f5a5 + 1f12f9d commit 6d5c652
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Compiler/AbstractRecursivePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ protected function getReflectionMethod(Definition $definition, string $method):
return new \ReflectionMethod(static function (...$arguments) {}, '__invoke');
}

if ($r->hasMethod('__callStatic') && ($r = $r->getMethod('__callStatic')) && $r->isPublic()) {
return new \ReflectionMethod(static function (...$arguments) {}, '__invoke');
}

throw new RuntimeException(\sprintf('Invalid service "%s": method "%s()" does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
}

Expand Down
27 changes: 15 additions & 12 deletions Compiler/ResolveAutowireInlineAttributesPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class ResolveAutowireInlineAttributesPass extends AbstractRecursivePass
{
protected bool $skipScalars = true;

private int $counter;

protected function processValue(mixed $value, bool $isRoot = false): mixed
{
$value = parent::processValue($value, $isRoot);
Expand All @@ -36,6 +38,10 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
return $value;
}

if ($isRoot) {
$this->counter = 0;
}

$isChildDefinition = $value instanceof ChildDefinition;

try {
Expand Down Expand Up @@ -92,10 +98,14 @@ private function registerAutowireInlineAttributes(\ReflectionFunctionAbstract $m
}

if (\array_key_exists('$'.$parameter->name, $arguments) || (\array_key_exists($index, $arguments) && '' !== $arguments[$index])) {
$attribute = \array_key_exists('$'.$parameter->name, $arguments) ? $arguments['$'.$parameter->name] : $arguments[$index];
if (!$attribute instanceof AutowireInline) {
continue;
}
} elseif (!$attribute = $parameter->getAttributes(AutowireInline::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
continue;
}
if (!$attribute = $parameter->getAttributes(AutowireInline::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
continue;
} else {
$attribute = $attribute->newInstance();
}

$type = ProxyHelper::exportType($parameter, true);
Expand All @@ -104,25 +114,18 @@ private function registerAutowireInlineAttributes(\ReflectionFunctionAbstract $m
continue;
}

$attribute = $attribute->newInstance();
$definition = $attribute->buildDefinition($attribute->value, $type, $parameter);

$paramResolverContainer->setDefinition('.autowire_inline', $definition);
(new ResolveParameterPlaceHoldersPass(false, false))->process($paramResolverContainer);

$id = '.autowire_inline.'.ContainerBuilder::hash([$this->currentId, $method->class ?? null, $method->name, (string) $parameter]);
$id = '.autowire_inline.'.$this->currentId.'.'.++$this->counter;

$this->container->setDefinition($id, $definition);
$arguments[$isChildDefinition ? '$'.$parameter->name : $index] = new Reference($id);

if ($definition->isAutowired()) {
$currentId = $this->currentId;
try {
$this->currentId = $id;
$this->processValue($definition, true);
} finally {
$this->currentId = $currentId;
}
$this->processValue($definition);
}
}

Expand Down
9 changes: 6 additions & 3 deletions Compiler/ResolveBindingsPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
Expand Down Expand Up @@ -179,10 +180,10 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
foreach ($reflectionMethod->getParameters() as $key => $parameter) {
$names[$key] = $parameter->name;

if (\array_key_exists($key, $arguments) && '' !== $arguments[$key]) {
if (\array_key_exists($key, $arguments) && '' !== $arguments[$key] && !$arguments[$key] instanceof AbstractArgument) {
continue;
}
if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name]) {
if (\array_key_exists($parameter->name, $arguments) && '' !== $arguments[$parameter->name] && !$arguments[$parameter->name] instanceof AbstractArgument) {
continue;
}
if (
Expand Down Expand Up @@ -227,7 +228,9 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed

foreach ($names as $key => $name) {
if (\array_key_exists($name, $arguments) && (0 === $key || \array_key_exists($key - 1, $arguments))) {
$arguments[$key] = $arguments[$name];
if (!array_key_exists($key, $arguments)) {
$arguments[$key] = $arguments[$name];
}
unset($arguments[$name]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion EnvVarProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
throw new RuntimeException(\sprintf('Invalid URL in env var "%s".', $name));
}
if (!isset($params['scheme'], $params['host'])) {
throw new RuntimeException(\sprintf('Invalid URL env var "%s": schema and host expected, "%s" given.', $name, $env));
throw new RuntimeException(\sprintf('Invalid URL in env var "%s": scheme and host expected.', $name));
}
$params += [
'port' => null,
Expand Down
18 changes: 18 additions & 0 deletions Tests/Compiler/CheckTypeDeclarationsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,17 @@ public function testCallableClass()
$this->addToAssertionCount(1);
}

public function testStaticCallableClass()
{
$container = new ContainerBuilder();
$container->register('foo', StaticCallableClass::class)
->setFactory([StaticCallableClass::class, 'staticMethodCall']);

(new CheckTypeDeclarationsPass())->process($container);

$this->addToAssertionCount(1);
}

public function testIgnoreDefinitionFactoryArgument()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -1017,3 +1028,10 @@ public function __call($name, $arguments)
{
}
}

class StaticCallableClass
{
public static function __callStatic($name, $arguments)
{
}
}
18 changes: 18 additions & 0 deletions Tests/Compiler/ResolveAutowireInlineAttributesPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveNamedArgumentsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

require_once __DIR__.'/../Fixtures/includes/autowiring_classes.php';

Expand Down Expand Up @@ -66,4 +68,20 @@ public function testChildDefinition()

$this->assertSame(['$inlined'], array_keys($container->getDefinition('autowire_inline1')->getArguments()));
}

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

$container->register('nested_autowire_inline', NestedAutowireInlineAttribute::class)
->setAutowired(true);

(new ResolveAutowireInlineAttributesPass())->process($container);

$this->assertEquals([new Reference('.autowire_inline.nested_autowire_inline.1')], $container->getDefinition('nested_autowire_inline')->getArguments());
$this->assertSame(AutowireInlineAttributesBar::class, $container->getDefinition('.autowire_inline.nested_autowire_inline.1')->getClass());

$this->assertEquals([new Reference('.autowire_inline.nested_autowire_inline.2'), 'testString'], $container->getDefinition('.autowire_inline.nested_autowire_inline.1')->getArguments());
$this->assertSame(Foo::class, $container->getDefinition('.autowire_inline.nested_autowire_inline.2')->getClass());
}
}
17 changes: 15 additions & 2 deletions Tests/Compiler/ResolveBindingsPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Argument\AbstractArgument;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
Expand Down Expand Up @@ -256,11 +257,23 @@ public function testBindWithNamedArgs()
$definition->setArguments(['c' => 'C', 'hostName' => 'H']);
$definition->setBindings($bindings);

$container->register('foo', CaseSensitiveClass::class);

$pass = new ResolveBindingsPass();
$pass->process($container);

$this->assertEquals(['C', 'K', 'H'], $definition->getArguments());
}

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

$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
$definition->setArguments([new AbstractArgument(), 'apiKey' => new AbstractArgument()]);
$definition->setBindings(['$c' => new BoundArgument('C'), '$apiKey' => new BoundArgument('K')]);

$pass = new ResolveBindingsPass();
$pass->process($container);

$this->assertEquals(['C', 'K'], $definition->getArguments());
}
}
15 changes: 15 additions & 0 deletions Tests/Fixtures/includes/autowiring_classes_80.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,18 @@ public function __construct(
) {
}
}

class NestedAutowireInlineAttribute
{
public function __construct(
#[AutowireInline(
AutowireInlineAttributesBar::class,
arguments: [
new AutowireInline(Foo::class),
'testString',
],
)]
public AutowireInlineAttributesBar $inlined,
) {
}
}

0 comments on commit 6d5c652

Please sign in to comment.