Commit 09968780 authored by Igor Wiedler's avatar Igor Wiedler

Revert the addition of RequestContextFactory

parent d0e0095b
...@@ -237,9 +237,6 @@ of them. ...@@ -237,9 +237,6 @@ of them.
* **request_context**: The request context is a simplified representation * **request_context**: The request context is a simplified representation
of the request that is used by the Router and the UrlGenerator. of the request that is used by the Router and the UrlGenerator.
* **request_context.factory**: Is used internally to create a RequestContext
from a Request.
.. note:: .. note::
All of these Silex core services are shared. All of these Silex core services are shared.
......
...@@ -27,6 +27,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher; ...@@ -27,6 +27,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\Exception\Exception as MatcherException; use Symfony\Component\Routing\Matcher\Exception\Exception as MatcherException;
use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Matcher\Exception\NotFoundException; use Symfony\Component\Routing\Matcher\Exception\NotFoundException;
...@@ -80,10 +81,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -80,10 +81,6 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
return new HttpKernel($app['dispatcher'], $app['resolver']); return new HttpKernel($app['dispatcher'], $app['resolver']);
}); });
$this['request_context.factory'] = $this->share(function () {
return new RequestContextFactory();
});
$this['request.http_port'] = 80; $this['request.http_port'] = 80;
$this['request.https_port'] = 443; $this['request.https_port'] = 443;
} }
...@@ -319,7 +316,14 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -319,7 +316,14 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{ {
$this['request'] = $event->getRequest(); $this['request'] = $event->getRequest();
$this['request_context'] = $this['request_context.factory']->create($this['request'], $this['request.http_port'], $this['request.https_port']); $this['request_context'] = new RequestContext(
$this['request']->getBaseUrl(),
$this['request']->getMethod(),
$this['request']->getHost(),
$this['request']->getScheme(),
!$this['request']->isSecure() ? $this['request']->getPort() : $this['request.http_port'],
$this['request']->isSecure() ? $this['request']->getPort() : $this['request.https_port']
);
$this['controllers']->flush(); $this['controllers']->flush();
......
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
/**
* Creates a RequestContext from a Request.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class RequestContextFactory
{
/**
* Creates the RequestContext instance.
*
* @param Request $request The input Request
* @param int $defaultHttpPort The default port for HTTP
* @param int $defaultHttpsPort The default port for HTTPS
*
* @return RequestContext the RequestContext
*/
public function create(Request $request, $defaultHttpPort = 80, $defaultHttpsPort = 443)
{
return new RequestContext(
$request->getBaseUrl(),
$request->getMethod(),
$request->getHost(),
$request->getScheme(),
!$request->isSecure() ? $request->getPort() : $defaultHttpPort,
$request->isSecure() ? $request->getPort() : $defaultHttpsPort
);
}
}
...@@ -32,8 +32,10 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -32,8 +32,10 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
$app->get('/hello/{name}', function ($name) {}) $app->get('/hello/{name}', function ($name) {})
->bind('hello'); ->bind('hello');
$app['request'] = Request::create('/'); $app->get('/', function () {});
$app['request_context'] = $app['request_context.factory']->create($app['request']);
$request = Request::create('/');
$app->handle($request);
$this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGenerator', $app['url_generator']); $this->assertInstanceOf('Symfony\Component\Routing\Generator\UrlGenerator', $app['url_generator']);
} }
...@@ -47,11 +49,14 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -47,11 +49,14 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
$app->get('/hello/{name}', function ($name) {}) $app->get('/hello/{name}', function ($name) {})
->bind('hello'); ->bind('hello');
$app['request'] = Request::create('/'); $app->get('/', function () use ($app) {
$app['request_context'] = $app['request_context.factory']->create($app['request']); return $app['url_generator']->generate('hello', array('name' => 'john'));
});
$request = Request::create('/');
$response = $app->handle($request);
$url = $app['url_generator']->generate('hello', array('name' => 'john')); $this->assertEquals('/hello/john', $response->getContent());
$this->assertEquals('/hello/john', $url);
} }
public function testAbsoluteUrlGeneration() public function testAbsoluteUrlGeneration()
...@@ -63,11 +68,14 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -63,11 +68,14 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
$app->get('/hello/{name}', function ($name) {}) $app->get('/hello/{name}', function ($name) {})
->bind('hello'); ->bind('hello');
$app['request'] = Request::create('https://localhost:81/'); $app->get('/', function () use ($app) {
$app['request_context'] = $app['request_context.factory']->create($app['request']); return $app['url_generator']->generate('hello', array('name' => 'john'), true);
});
$request = Request::create('https://localhost:81/');
$response = $app->handle($request);
$url = $app['url_generator']->generate('hello', array('name' => 'john'), true); $this->assertEquals('https://localhost:81/hello/john', $response->getContent());
$this->assertEquals('https://localhost:81/hello/john', $url);
} }
public function testUrlGenerationWithHttp() public function testUrlGenerationWithHttp()
...@@ -80,11 +88,14 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -80,11 +88,14 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
->bind('insecure_page') ->bind('insecure_page')
->requireHttp(); ->requireHttp();
$app['request'] = Request::create('https://localhost/'); $app->get('/', function () use ($app) {
$app['request_context'] = $app['request_context.factory']->create($app['request']); return $app['url_generator']->generate('insecure_page');
});
$url = $app['url_generator']->generate('insecure_page'); $request = Request::create('https://localhost/');
$this->assertEquals('http://localhost/insecure', $url); $response = $app->handle($request);
$this->assertEquals('http://localhost/insecure', $response->getContent());
} }
public function testUrlGenerationWithHttps() public function testUrlGenerationWithHttps()
...@@ -97,10 +108,13 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -97,10 +108,13 @@ class UrlGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
->bind('secure_page') ->bind('secure_page')
->requireHttps(); ->requireHttps();
$app['request'] = Request::create('http://localhost/'); $app->get('/', function () use ($app) {
$app['request_context'] = $app['request_context.factory']->create($app['request']); return $app['url_generator']->generate('secure_page');
});
$request = Request::create('http://localhost/');
$response = $app->handle($request);
$url = $app['url_generator']->generate('secure_page'); $this->assertEquals('https://localhost/secure', $response->getContent());
$this->assertEquals('https://localhost/secure', $url);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment