Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 9daeae6

Browse files
committed
Make the ServerRequestFactory service compatible with all containers
We cannot return just `ServerRequestFactory::fromGlobals` or `[ServerRequestFactory::class, 'fromGlobals']` as not all containers allow vanilla PHP callable services. Instead, we wrap it in an anonymous function here, which is allowed by all containers tested at this time.
1 parent 3c365f1 commit 9daeae6

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Container/ServerRequestFactoryFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@
1212
use Psr\Container\ContainerInterface;
1313
use Zend\Diactoros\ServerRequestFactory;
1414

15+
/**
16+
* Return a factory for generating a server request.
17+
*
18+
* We cannot return just `ServerRequestFactory::fromGlobals` or
19+
* `[ServerRequestFactory::class, 'fromGlobals']` as not all containers
20+
* allow vanilla PHP callable services. Instead, we wrap it in an
21+
* anonymous function here, which is allowed by all containers tested
22+
* at this time.
23+
*/
1524
class ServerRequestFactoryFactory
1625
{
1726
public function __invoke(ContainerInterface $container) : callable
1827
{
19-
return [ServerRequestFactory::class, 'fromGlobals'];
28+
return function () {
29+
return ServerRequestFactory::fromGlobals();
30+
};
2031
}
2132
}

test/Container/ServerRequestFactoryFactoryTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace ZendTest\Expressive\Container;
1111

12+
use Closure;
1213
use PHPUnit\Framework\TestCase;
1314
use Psr\Container\ContainerInterface;
1415
use Zend\Diactoros\ServerRequestFactory;
@@ -29,10 +30,18 @@ public function testFactoryReturnsCallable()
2930
}
3031

3132
/**
33+
* Some containers do not allow returning generic PHP callables, and will
34+
* error when one is returned; one example is Auryn. As such, the factory
35+
* cannot simply return a callable referencing the
36+
* ServerRequestFactory::fromGlobals method, but must be decorated as a
37+
* closure.
38+
*
3239
* @depends testFactoryReturnsCallable
3340
*/
34-
public function testFactoryUsesDiactorosFromGlobals(callable $factory)
41+
public function testFactoryIsAClosure(callable $factory)
3542
{
36-
$this->assertSame([ServerRequestFactory::class, 'fromGlobals'], $factory);
43+
$this->assertNotSame([ServerRequestFactory::class, 'fromGlobals'], $factory);
44+
$this->assertNotSame(ServerRequestFactory::class . '::fromGlobals', $factory);
45+
$this->assertInstanceOf(Closure::class, $factory);
3746
}
3847
}

0 commit comments

Comments
 (0)