Skip to content

Commit

Permalink
Merge pull request #135 from laminas/3.3.x-merge-up-into-3.4.x_KNFjVQX2
Browse files Browse the repository at this point in the history
Merge release 3.3.5 into 3.4.x
  • Loading branch information
Xerkus authored Sep 20, 2022
2 parents 46a3be5 + 43fa8b0 commit 27b530e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 21 deletions.
32 changes: 16 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 19 additions & 5 deletions src/Controller/LazyControllerAbstractFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
use Laminas\Log\FormatterPluginManager as LogFormatterManager;
use Laminas\Log\ProcessorPluginManager as LogProcessorManager;
use Laminas\Log\WriterPluginManager as LogWriterManager;
use Laminas\Mvc\Exception\DomainException;
use Laminas\Serializer\AdapterPluginManager as SerializerAdapterManager;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
use Laminas\Stdlib\DispatchableInterface;
use Laminas\Validator\ValidatorPluginManager;
use ReflectionClass;
use ReflectionNamedType;
use ReflectionParameter;

/**
Expand Down Expand Up @@ -147,8 +149,20 @@ private function resolveParameter(ContainerInterface $container, $requestedName)
* resolved to a service in the container.
*/
return function (ReflectionParameter $parameter) use ($container, $requestedName) {
if ($parameter->getType()
&& $parameter->getType()->getName() === 'array') {

$parameterType = $parameter->getType();
if ($parameterType === null) {
return null;
}
if (! $parameterType instanceof ReflectionNamedType) {
throw new DomainException(sprintf(
'Unable to create controller "%s"; unable to resolve parameter "%s" with union type hint',
$requestedName,
$parameter->getName()
));
}

if ($parameterType->getName() === 'array') {
if ($parameter->getName() === 'config'
&& $container->has('config')
) {
Expand All @@ -157,11 +171,11 @@ private function resolveParameter(ContainerInterface $container, $requestedName)
return [];
}

if (! $parameter->getType() || $parameter->getType()->isBuiltin()) {
return;
if ($parameterType->isBuiltin()) {
return null;
}

$type = $parameter->getType()->getName();
$type = $parameterType->getName();
$type = isset($this->aliases[$type]) ? $this->aliases[$type] : $type;

if (! $container->has($type)) {
Expand Down
18 changes: 18 additions & 0 deletions test/Controller/LazyControllerAbstractFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Interop\Container\ContainerInterface;
use Laminas\Mvc\Controller\LazyControllerAbstractFactory;
use Laminas\Mvc\Exception\DomainException;
use Laminas\ServiceManager\Exception\ServiceNotFoundException;
use Laminas\Validator\ValidatorPluginManager;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -75,6 +76,23 @@ public function testFactoryRaisesExceptionWhenUnableToResolveATypeHintedService(
$factory($this->container->reveal(), TestAsset\ControllerWithTypeHintedConstructorParameter::class);
}

/**
* @requires PHP >= 8.0
*/
public function testFactoryRaisesExceptionWhenResolvingUnionTypeHintedService(): void
{
$this->container->has(TestAsset\SampleInterface::class)->willReturn(false);
$factory = new LazyControllerAbstractFactory();
$this->expectException(DomainException::class);
$this->expectExceptionMessage(
sprintf(
'Unable to create controller "%s"; unable to resolve parameter "sample" with union type hint',
TestAsset\ControllerWithUnionTypeHintedConstructorParameter::class
)
);
$factory($this->container->reveal(), TestAsset\ControllerWithUnionTypeHintedConstructorParameter::class);
}

public function testFactoryPassesNullForScalarParameters()
{
$factory = new LazyControllerAbstractFactory();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace LaminasTest\Mvc\Controller\TestAsset;

use Laminas\Mvc\Controller\AbstractActionController;

class ControllerWithUnionTypeHintedConstructorParameter extends AbstractActionController
{
public $sample;

public function __construct(SampleInterface|AnotherSampleInterface $sample)
{
$this->sample = $sample;
}
}

0 comments on commit 27b530e

Please sign in to comment.