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

zend-diactoros is an optional dependency #580

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"psr/container": "^1.0",
"psr/http-message": "^1.0.1",
"psr/http-server-middleware": "^1.0",
"zendframework/zend-diactoros": "^1.3.10",
"zendframework/zend-expressive-router": "^3.0.0rc4",
"zendframework/zend-expressive-template": "^2.0.0alpha1",
"zendframework/zend-httphandlerrunner": "^1.0.1",
Expand All @@ -41,16 +40,19 @@
"phpstan/phpstan-strict-rules": "^0.9",
"phpunit/phpunit": "^7.0.1",
"zendframework/zend-coding-standard": "~1.0.0",
"zendframework/zend-diactoros": "^1.7.1",
"zendframework/zend-expressive-aurarouter": "^3.0.0rc3",
"zendframework/zend-expressive-fastroute": "^3.0.0rc4",
"zendframework/zend-expressive-zendrouter": "^3.0.0rc3",
"zendframework/zend-servicemanager": "^2.7.8 || ^3.3"
},
"conflict": {
"container-interop/container-interop": "<1.2.0"
"container-interop/container-interop": "<1.2.0",
"zendframework/zend-diactoros": "<1.7.1"
},
"suggest": {
"filp/whoops": "^2.1 to use the Whoops error handler",
"psr/http-message-implementation": "Please install a psr/http-message-implementation to consume Expressive; e.g., zendframework/zend-diactoros",
"zendframework/zend-auradi-config": "^1.0 to use Aura.Di dependency injection container",
"zendframework/zend-expressive-helpers": "^3.0 for its UrlHelper, ServerUrlHelper, and BodyParseMiddleware",
"zendframework/zend-expressive-tooling": "For migration and development tools; require it with the --dev flag",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

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

2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
<testsuites>
<testsuite name="zend-expressive-response-factory">
<file>./test/Container/ResponseFactoryFactoryWithoutDiactorosTest.php</file>
<file>./test/Container/ServerRequestFactoryFactoryWithoutDiactorosTest.php</file>
<file>./test/Container/StreamFactoryFactoryWithoutDiactorosTest.php</file>
</testsuite>
<testsuite name="zend-expressive">
<directory>./test</directory>
<exclude>./test/Container/ResponseFactoryFactoryWithoutDiactorosTest.php</exclude>
<exclude>./test/Container/ServerRequestFactoryFactoryWithoutDiactorosTest.php</exclude>
<exclude>./test/Container/StreamFactoryFactoryWithoutDiactorosTest.php</exclude>
</testsuite>
</testsuites>
Expand Down
13 changes: 13 additions & 0 deletions src/Container/ServerRequestFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Expressive\Container;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\ServerRequestFactory;

/**
Expand All @@ -25,6 +26,18 @@ class ServerRequestFactoryFactory
{
public function __invoke(ContainerInterface $container) : callable
{
if (! class_exists(ServerRequestFactory::class)) {
throw new Exception\InvalidServiceException(sprintf(
'The %1$s service must map to a factory capable of returning an'
. ' implementation instance. By default, we assume usage of'
. ' zend-diactoros for PSR-7, but it does not appear to be'
. ' present on your system. Please install zendframework/zend-diactoros'
. ' or provide an alternate factory for the %1$s service that'
. ' can produce an appropriate %1$s instance.',
ServerRequestInterface::class
));
}

return function () {
return ServerRequestFactory::fromGlobals();
};
Expand Down
60 changes: 60 additions & 0 deletions test/Container/ServerRequestFactoryFactoryWithoutDiactorosTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ZendTest\Expressive\Container;

use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Container\Exception\InvalidServiceException;
use Zend\Expressive\Container\ServerRequestFactoryFactory;

class ServerRequestFactoryFactoryWithoutDiactorosTest extends TestCase
{
/** @var ContainerInterface|ObjectProphecy */
private $container;

/** @var ServerRequestFactoryFactory */
private $factory;

/** @var array */
private $autoloadFunctions = [];

protected function setUp()
{
class_exists(InvalidServiceException::class);

$this->container = $this->prophesize(ContainerInterface::class)->reveal();
$this->factory = new ServerRequestFactoryFactory();

$this->autoloadFunctions = spl_autoload_functions();
foreach ($this->autoloadFunctions as $autoloader) {
spl_autoload_unregister($autoloader);
}
}

private function reloadAutoloaders()
{
foreach ($this->autoloadFunctions as $autoloader) {
spl_autoload_register($autoloader);
}
}

public function testFactoryRaisesAnExceptionIfDiactorosIsNotLoaded()
{
$this->expectException(InvalidServiceException::class);
$this->expectExceptionMessage('zendframework/zend-diactoros');

try {
($this->factory)($this->container);
} finally {
$this->reloadAutoloaders();
}
}
}