Skip to content

Supprt for ANY psr7 implementation #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 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
72 changes: 39 additions & 33 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
language: php

sudo: false

cache:
directories:
- $HOME/.composer/cache/files
- $HOME/symfony-bridge/.phpunit

matrix:
include:
- php: 5.3
dist: 'precise'
- php: 5.4
- php: 5.5
- php: 5.6
- php: 5.3
dist: 'precise'
env: COMPOSER_OPTIONS="--prefer-lowest --prefer-stable" SYMFONY_DEPRECATIONS_HELPER=weak
- php: 5.6
env: COMPOSER_OPTIONS="" SYMFONY_DEPRECATIONS_HELPER=weak
- php: 7.0
- php: hhvm
allow_failures:
- php: hhvm
fast_finish: true

env:
global:
- deps=no
- SYMFONY_DEPRECATIONS_HELPER=strict
- PHPUNIT_FLAGS="-v"
- SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit"
- COMPOSER_OPTIONS="--prefer-stable"

matrix:
fast_finish: true
include:
# Minimum supported dependencies with the latest and oldest PHP version
- php: 7.2
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"
- php: 7.1
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors"

# Test the latest stable release
- php: 7.1
- php: 7.2
env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text"

# Test LTS versions. This makes sure we do not use Symfony packages with version greater
# than 2 or 3 respectively. Read more at https://github.com/symfony/lts
- php: 7.2
env: DEPENDENCIES="symfony/lts:^3"

# Latest commit to master
- php: 7.2
env: STABILITY="dev"

allow_failures:
# Dev-master is allowed to fail.
- env: STABILITY="dev"

before_install:
- if [[ "$TRAVIS_PHP_VERSION" != "nightly" ]] && [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini; fi;
- composer self-update
- if [[ "$TRAVIS_PHP_VERSION" != "nightly" ]] && [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]] && [ $(php -r "echo PHP_MINOR_VERSION;") -le 4 ]; then echo "extension = apc.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi;
- if [[ "$TRAVIS_PHP_VERSION" != "nightly" ]] && [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then php -i; fi;
# Set the COMPOSER_ROOT_VERSION to the right version according to the branch being built
- if [ "$TRAVIS_BRANCH" = "master" ]; then export COMPOSER_ROOT_VERSION=dev-master; else export COMPOSER_ROOT_VERSION="$TRAVIS_BRANCH".x-dev; fi;
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi
- if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi;
- if ! [ -v "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi;

install:
- if [ "$TRAVIS_PHP_VERSION" != "5.3" ]; then composer require --no-update zendframework/zend-diactoros; fi;
- composer update --prefer-source $COMPOSER_OPTIONS
- vendor/bin/simple-phpunit install
# To be removed when this issue will be resolved: https://github.com/composer/composer/issues/5355
- if [[ "$COMPOSER_FLAGS" == *"--prefer-lowest"* ]]; then composer update --prefer-dist --no-interaction --prefer-stable --quiet; fi
- composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction
- ./vendor/bin/simple-phpunit install

script:
- vendor/bin/simple-phpunit
- composer validate --strict --no-check-lock
# simple-phpunit is the PHPUnit wrapper provided by the PHPUnit Bridge component and
# it helps with testing legacy code and deprecations (composer require symfony/phpunit-bridge)
- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
13 changes: 2 additions & 11 deletions Factory/DiactorosFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\PsrHttpMessage\Factory;

use Psr\Http\Message\UploadedFileInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
Expand Down Expand Up @@ -112,7 +113,7 @@ private function createUploadedFile(UploadedFile $symfonyUploadedFile)
{
return new DiactorosUploadedFile(
$symfonyUploadedFile->getRealPath(),
$symfonyUploadedFile->getClientSize(),
(int) $symfonyUploadedFile->getSize(),
$symfonyUploadedFile->getError(),
$symfonyUploadedFile->getClientOriginalName(),
$symfonyUploadedFile->getClientMimeType()
Expand Down Expand Up @@ -143,16 +144,6 @@ public function createResponse(Response $symfonyResponse)
}

$headers = $symfonyResponse->headers->all();

$cookies = $symfonyResponse->headers->getCookies();
if (!empty($cookies)) {
$headers['Set-Cookie'] = array();

foreach ($cookies as $cookie) {
$headers['Set-Cookie'][] = $cookie->__toString();
}
}

$response = new DiactorosResponse(
$stream,
$symfonyResponse->getStatusCode(),
Expand Down
12 changes: 12 additions & 0 deletions Factory/HttpFoundationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -104,6 +105,17 @@ private function createUploadedFile(UploadedFileInterface $psrUploadedFile)
$clientFileName = $psrUploadedFile->getClientFilename();
}

if (class_exists(HeaderUtils::class)) {
// Symfony 4.1+
return new UploadedFile(
$temporaryPath,
null === $clientFileName ? '' : $clientFileName,
$psrUploadedFile->getClientMediaType(),
$psrUploadedFile->getError(),
true
);
}

return new UploadedFile(
$temporaryPath,
null === $clientFileName ? '' : $clientFileName,
Expand Down
139 changes: 139 additions & 0 deletions Factory/Psr7Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

declare(strict_types=1);

namespace Symfony\Bridge\PsrHttpMessage\Factory;

use Nyholm\Psr7Server\ServerRequestCreator;
use Nyholm\Psr7Server\ServerRequestCreatorInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UriFactoryInterface;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
* Converts Symfony requests and responses to PSR-7 objects with help of PSR-17 HTTP factories.
*
* @author Tobias Nyholm <[email protected]>
*/
class Psr7Factory implements HttpMessageFactoryInterface
{
private $serverRequestCreator;

private $responseFactory;

private $streamFactory;

public function __construct(
ServerRequestCreatorInterface $serverRequestCreator,
ResponseFactoryInterface $responseFactory,
StreamFactoryInterface $streamFactory
) {
$this->serverRequestCreator = $serverRequestCreator;
$this->responseFactory = $responseFactory;
$this->streamFactory = $streamFactory;
}

public function createRequest(Request $symfonyRequest)
{
$server = $symfonyRequest->server->all();
if (empty($server['REQUEST_METHOD'])) {
$server['REQUEST_METHOD'] = $symfonyRequest->getMethod();
}

$request = $this->serverRequestCreator->fromArrays(
$server,
$symfonyRequest->headers->all(),
$symfonyRequest->cookies->all(),
$symfonyRequest->query->all(),
$symfonyRequest->request->all(),
$this->getFiles($symfonyRequest->files->all()),
$symfonyRequest->getContent(true)
);


foreach ($symfonyRequest->attributes->all() as $key => $value) {
$request = $request->withAttribute($key, $value);
}

return $request;
}

/**
* Converts Symfony uploaded files array to the PSR one.
*
* @param array $uploadedFiles
*
* @return array
*/
private function getFiles(array $uploadedFiles)
{
$files = array();

foreach ($uploadedFiles as $key => $value) {
if (null === $value) {
$files[$key] = [
'tmp_name' => tempnam(sys_get_temp_dir(), uniqid('file', true)),
'size' => 0,
'error' => UPLOAD_ERR_NO_FILE,
'name' => '',
'type' => '',
];
} elseif ($value instanceof UploadedFile) {
$files[$key] = [
'tmp_name' => $value->getRealPath(),
'size' => $value->getSize(),
'error' => $value->getError(),
'name' => $value->getClientOriginalName(),
'type' => $value->getClientMimeType(),
];
} else {
$files[$key] = $this->getFiles($value);
}
}

return $files;
}

public function createResponse(Response $symfonyResponse)
{
if ($symfonyResponse instanceof BinaryFileResponse) {
$stream = $this->streamFactory->createStreamFromFile($symfonyResponse->getFile()->getPathname(), 'r');
} else {
$stream = $this->streamFactory->createStream('');
if ($symfonyResponse instanceof StreamedResponse) {
ob_start(function ($buffer) use ($stream) {
$stream->write($buffer);

return false;
});

$symfonyResponse->sendContent();
ob_end_clean();
} else {
$stream->write($symfonyResponse->getContent());
}
}

$headers = $symfonyResponse->headers->all();
$response = $this->responseFactory->createResponse($symfonyResponse->getStatusCode());
$response = $response->withBody($stream);
$response = $response->withProtocolVersion($symfonyResponse->getProtocolVersion());
foreach ($headers as $headerName => $value) {
$response = $response->withHeader($headerName, $value);
}

return $response;

}

}
Loading