Skip to content

Commit

Permalink
Removed URL building method from request and moved it in the build st…
Browse files Browse the repository at this point in the history
…ep in ApplicationFactory
  • Loading branch information
DaGhostman committed Jul 11, 2015
1 parent d91dcb3 commit a8098a1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 46 deletions.
25 changes: 23 additions & 2 deletions src/Wave/Framework/Application/ApplicationFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Wave\Framework\Application;

use Wave\Framework\Http\Entities\Url\Query;
use Wave\Framework\Interfaces\Http\UrlInterface;

class ApplicationFactory
Expand All @@ -11,11 +12,17 @@ class ApplicationFactory
private $responseClass = '\Wave\Framework\Http\Response';
private $serverClass = '\Wave\Framework\Http\Server';

private $serverVariables = [];
private $serverVariables = [
'SERVER_PORT' => 80,
'SERVER_NAME' => 'localhost',
'REQUEST_URI' => '',
'QUERY_STRING' => '',
'HTTPS' => null,
];

public function __construct(array $server)
{
$this->serverVariables = $server;
$this->serverVariables = array_merge($this->serverVariables, $server);
}

/**
Expand Down Expand Up @@ -108,6 +115,20 @@ public function build(UrlInterface $url)
));
}


if ($this->serverVariables['SERVER_PORT'] === 443 ||
(array_key_exists('HTTPS', $this->serverVariables) &&
$this->serverVariables['HTTPS'] !== 'off' &&
!empty($this->serverVariables['HTTPS']))
) {
$url = $url->setScheme('https');
}

$url = $url->setHost($this->serverVariables['SERVER_NAME'])
->setPort((int) $this->serverVariables['SERVER_PORT'])
->setPath(parse_url($this->serverVariables['REQUEST_URI'], PHP_URL_PATH))
->setQuery(new Query($this->serverVariables['QUERY_STRING']));

$request = $requestReflection->newInstance(
$this->serverVariables['REQUEST_METHOD'],
$url,
Expand Down
29 changes: 0 additions & 29 deletions src/Wave/Framework/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Wave\Framework\Http;

use Wave\Framework\Interfaces\Http\QueryInterface;
use Wave\Framework\Interfaces\Http\RequestInterface;
use Wave\Framework\Interfaces\Http\UrlInterface;

Expand Down Expand Up @@ -71,34 +70,6 @@ public function __construct($method, UrlInterface $uri, array $headers = [], $bo
}
}

/**
* Factory for the Url object.
* Although this creates a tight coupling, the URL is
* vital part of the HTTP request, it seems inappropriate if the
* construction logic is entirely placed within the Server object,
* as the server should do the heavy lifting for the
*
*
* @param array $server
* @param UrlInterface $url
* @param QueryInterface $query
* @return Url
*/
public static function buildUrl(UrlInterface $url, QueryInterface $query, array $server)
{
if ($server['SERVER_PORT'] === 443 ||
(array_key_exists('HTTPS', $server) && $server['HTTPS'] !== 'off' && !empty($server['HTTPS']))
) {
$url = $url->setScheme('https');
}

parse_str(parse_url($server['REQUEST_URI'], PHP_URL_QUERY), $params);
return $url->setHost($server['SERVER_NAME'])
->setPort((int) $server['SERVER_PORT'])
->setPath(parse_url($server['REQUEST_URI'], PHP_URL_PATH))
->setQuery($query->import($params));
}

/**
* Add a header to the current object.
* This method should have dual behaviour, based on the $append
Expand Down
15 changes: 0 additions & 15 deletions tests/Test/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,6 @@ public function testAdditionalGetters()
$this->assertSame('GET', $this->request->getMethod());
}

public function testUrlBuilding()
{
$server = [
'SERVER_PORT' => 80,
'HTTPS' => '1',
'SERVER_NAME' => 'localhost',
'REQUEST_URI' => '/index?param=value'
];
//var_dump(get_class_methods($this->url));
$url = Request::buildUrl($this->url, $this->query, $server);

$this->assertInstanceOf('\Wave\Framework\Interfaces\Http\UrlInterface', $url);
$this->assertSame('https://localhost/index?param=value', (string) $url);
}

protected function tearDown()
{
parent::tearDown();
Expand Down

0 comments on commit a8098a1

Please sign in to comment.