Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Adds initial http request building.
Browse files Browse the repository at this point in the history
  • Loading branch information
stevewest committed May 10, 2016
1 parent 0cb618a commit 2054435
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 44 deletions.
8 changes: 5 additions & 3 deletions composer.json
Expand Up @@ -13,7 +13,9 @@
"php": ">=7.0.0",
"fuelphp/dependency": "dev-master",
"league/event": "^2.1",
"fuelphp/config": "dev-master"
"league/route": "^2.0",
"fuelphp/config": "dev-master",
"zendframework/zend-diactoros": "^1.3"
},
"require-dev": {
"codeception/codeception": "^2.1",
Expand All @@ -28,8 +30,8 @@
},
"autoload-dev": {
"psr-4": {
"Basic\\": "tests/_support/components/Basic/",
"Broken\\": "tests/_support/components/Broken/"
"Basic\\": "tests/_support/components/Basic/src",
"Broken\\": "tests/_support/components/Broken/src"
}
},
"extra": {
Expand Down
2 changes: 1 addition & 1 deletion src/AbstractComponent.php
Expand Up @@ -22,6 +22,6 @@ abstract class AbstractComponent implements ComponentInterface
public function getConfigPath() : string
{
$reflection = new ReflectionClass(static::class);
return dirname($reflection->getFileName());
return dirname($reflection->getFileName()) . '/..';
}
}
11 changes: 9 additions & 2 deletions src/Application.php
Expand Up @@ -15,6 +15,8 @@
use Fuel\Config\Container as ConfigContainer;
use Fuel\Dependency\Container as DependencyContainer;
use Fuel\Foundation\Event\AppStarted;
use Fuel\Foundation\Request\RequestInterface;
use Fuel\Foundation\Response\ResponseInterface;
use League\Container\ContainerInterface;
use League\Event\Emitter;

Expand Down Expand Up @@ -66,6 +68,13 @@ public function getDependencyContainer() : ContainerInterface
}

public function run()
{
// TODO: make sure the URI is set

// TODO: send shutdown event
}

public function performRequest(RequestInterface $request) : ResponseInterface
{
// TODO: trigger request started event

Expand All @@ -76,8 +85,6 @@ public function run()
// TODO: trigger response started event

// TODO: generate and send response

// TODO: send shutdown event
}

/**
Expand Down
28 changes: 22 additions & 6 deletions src/ApplicationServicesProvider.php
Expand Up @@ -12,21 +12,24 @@

namespace Fuel\Foundation;

use Fuel\Foundation\Request\Http;
use Fuel\Foundation\Request\RequestInterface;
use Fuel\Foundation\Response\ResponseInterface;
use League\Container\ServiceProvider;
use League\Container\ServiceProvider\AbstractServiceProvider;

class ApplicationServicesProvider extends ServiceProvider
class ApplicationServicesProvider extends AbstractServiceProvider
{

protected $provides = [
'fuel.application.event',

'fuel.application.request',
'Fuel\Foundation\Request\Cli',
'Fuel\Foundation\Request\Http',

'fuel.application.response',
'Fuel\Foundation\Response\Cli',
'Fuel\Foundation\Response\Http',

'fuel.application.component_manager',
];
Expand All @@ -39,9 +42,11 @@ public function register()
$this->getContainer()->add('fuel.application.event', 'League\Event\Emitter', true);

$this->getContainer()->add('Fuel\Foundation\Request\Cli', 'Fuel\Foundation\Request\Cli', false);
$this->getContainer()->add('Fuel\Foundation\Request\Http', Http::forge(), false);
$this->getContainer()->add('fuel.application.request', $this->constructRequest(), true);

$this->getContainer()->add('Fuel\Foundation\Response\Cli', 'Fuel\Foundation\Response\Cli', false);
$this->getContainer()->add('Fuel\Foundation\Response\Http', 'Fuel\Foundation\Response\Http', false);
$this->getContainer()->add('fuel.application.response', $this->constructResponse(), true);

$this->getContainer()->add('fuel.application.component_manager', $this->constructComponentManager(), true);
Expand All @@ -57,16 +62,27 @@ protected function constructComponentManager()
*/
protected function constructRequest() : RequestInterface
{
// TODO: perform an actual check to see what kind of request we are dealing with!
return $this->getContainer()->get('Fuel\Foundation\Request\Cli');
if ($this->isCli()) {
return $this->getContainer()->get('Fuel\Foundation\Request\Cli');
}

return $this->getContainer()->get('Fuel\Foundation\Request\Http');
}

/**
* @return ResponseInterface
*/
protected function constructResponse() : ResponseInterface
{
// TODO: perform an actual check to see what kind of request we are dealing with!
return $this->getContainer()->get('Fuel\Foundation\Response\Cli');
if ($this->isCli()) {
return $this->getContainer()->get('Fuel\Foundation\Response\Cli');
}

return $this->getContainer()->get('Fuel\Foundation\Response\Http');
}

public function isCli()
{
return php_sapi_name() === 'cli';
}
}
14 changes: 7 additions & 7 deletions src/ComponentManager.php
Expand Up @@ -12,7 +12,7 @@

namespace Fuel\Foundation;

use Fuel\Config\Container;
use Fuel\Config\ContainerInterface;
use Fuel\Foundation\Exception\ComponentLoad;

/**
Expand All @@ -35,11 +35,11 @@ class ComponentManager implements ComponentManagerInterface
protected $loadedComponents = [];

/**
* @var Container
* @var ContainerInterface
*/
protected $configContainer;

public function __construct(Container $configContainer)
public function __construct(ContainerInterface $configContainer)
{
$this->configContainer = $configContainer;
}
Expand Down Expand Up @@ -70,15 +70,15 @@ public function load(string $name) : ComponentInterface
throw new ComponentLoad("FOU-001: Unable to load [$fullName]: Class not found");
}

// Load the component
$component = new $fullName();

// Check if it implements the correct interface
if ( ! in_array('Fuel\Foundation\ComponentInterface', class_implements($fullName)))
if ( ! $component instanceof ComponentInterface)
{
throw new ComponentLoad("FOU-002: Unable to load [$fullName]: Does not implement ComponentInterface");
}

// Load the component
$component = new $fullName();

$this->loadedComponents[$name] = $component;

$this->addConfigPath($component);
Expand Down
1 change: 0 additions & 1 deletion src/Request/Cli.php
Expand Up @@ -14,5 +14,4 @@

class Cli implements RequestInterface
{

}
21 changes: 21 additions & 0 deletions src/Request/Http.php
@@ -0,0 +1,21 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Foundation\Request;

use Zend\Diactoros\ServerRequest;

class Http extends ServerRequest implements RequestInterface
{
public static function forge()
{
return HttpRequestFactory::fromGlobals();
}
}
64 changes: 64 additions & 0 deletions src/Request/HttpRequestFactory.php
@@ -0,0 +1,64 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Foundation\Request;

use UnexpectedValueException;
use Zend\Diactoros\ServerRequestFactory;

/**
* Temporary request factory until fuel has a http message implementation
*
* @package Fuel\Foundation\Request
*/
class HttpRequestFactory extends ServerRequestFactory
{
public static function fromGlobals(
array $server = null,
array $query = null,
array $body = null,
array $cookies = null,
array $files = null
) {
$server = static::normalizeServer($server ?: $_SERVER);
$files = static::normalizeFiles($files ?: $_FILES);
$headers = static::marshalHeaders($server);

return new Http(
$server,
$files,
static::marshalUriFromServer($server, $headers),
static::get('REQUEST_METHOD', $server, 'GET'),
'php://input',
$headers,
$cookies ?: $_COOKIE,
$query ?: $_GET,
$body ?: $_POST,
static::marshalProtocolVersion($server)
);

}

private static function marshalProtocolVersion(array $server)
{
if (! isset($server['SERVER_PROTOCOL'])) {
return '1.1';
}

if (! preg_match('#^(HTTP/)?(?P<version>[1-9]\d*(?:\.\d)?)$#', $server['SERVER_PROTOCOL'], $matches)) {
throw new UnexpectedValueException(sprintf(
'Unrecognized protocol version (%s)',
$server['SERVER_PROTOCOL']
));
}

return $matches['version'];
}
}
Empty file modified src/Request/RequestInterface.php 100755 → 100644
Empty file.
10 changes: 10 additions & 0 deletions src/Response/Cli.php
Expand Up @@ -14,5 +14,15 @@

class Cli implements ResponseInterface
{
protected $statusCode;

public function setStatusCode($code)
{
$this->statusCode = $code;
}

public function getStatusCode()
{
return $this->statusCode;
}
}
18 changes: 18 additions & 0 deletions src/Response/Http.php
@@ -0,0 +1,18 @@
<?php
/**
* @package Fuel\Foundation
* @version 2.0
* @author Fuel Development Team
* @license MIT License
* @copyright 2010 - 2016 Fuel Development Team
* @link http://fuelphp.com
*/

namespace Fuel\Foundation\Response;

use Zend\Diactoros\Response;

class Http extends Response implements ResponseInterface
{

}
2 changes: 1 addition & 1 deletion src/Response/ResponseInterface.php 100755 → 100644
Expand Up @@ -14,5 +14,5 @@

interface ResponseInterface
{

public function getStatusCode();
}
24 changes: 1 addition & 23 deletions tests/_support/_generated/UnitTesterActions.php
@@ -1,4 +1,4 @@
<?php //[STAMP] 1715bd3253c121dfcf3d1583424e2396
<?php //[STAMP] 0f3190f33333ae0fb51c9c1f443e0fef
namespace _generated;

// This class was automatically generated by build task
Expand Down Expand Up @@ -93,17 +93,6 @@ public function assertGreaterThan($expected, $actual, $message = null) {
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @deprecated
* @see \Codeception\Module\Asserts::assertGreaterThen()
*/
public function assertGreaterThen($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThen', func_get_args()));
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
Expand All @@ -119,17 +108,6 @@ public function assertGreaterThanOrEqual($expected, $actual, $message = null) {
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
* @deprecated
* @see \Codeception\Module\Asserts::assertGreaterThenOrEqual()
*/
public function assertGreaterThenOrEqual($expected, $actual, $message = null) {
return $this->getScenario()->runStep(new \Codeception\Step\Action('assertGreaterThenOrEqual', func_get_args()));
}


/**
* [!] Method is generated. Documentation taken from corresponding module.
*
Expand Down

0 comments on commit 2054435

Please sign in to comment.