Skip to content

Commit ce963a3

Browse files
committed
Refactor directory
1 parent b42aad6 commit ce963a3

File tree

10 files changed

+485
-1
lines changed

10 files changed

+485
-1
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
},
3232
"autoload": {
3333
"psr-4": {
34-
"Simples\\Http\\Error\\": "src/Error/",
3534
"Simples\\Http\\": "src/"
3635
}
3736
}
File renamed without changes.
File renamed without changes.

src/Kernel/App.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Simples\Http\Kernel;
4+
5+
use ErrorException;
6+
use Simples\Kernel\App as Kernel;
7+
use Simples\Http\Request;
8+
use Simples\Kernel\Container;
9+
use Simples\Persistence\Transaction;
10+
use Throwable;
11+
12+
/**
13+
* Class App
14+
* @package Simples\Http\Kernel
15+
*/
16+
class App
17+
{
18+
/**
19+
* @SuppressWarnings("BooleanArgumentFlag")
20+
* Used to catch http requests and handle response to their
21+
*
22+
* @param bool $output (true) Define if the method will generate one output with the response
23+
* @return mixed The match response for requested resource
24+
* @throws ErrorException Generated when is not possible commit the changes
25+
*/
26+
public static function handler(bool $output = true)
27+
{
28+
$fail = null;
29+
$response = null;
30+
31+
$http = new Http(self::request());
32+
try {
33+
$response = $http->handler();
34+
if ($response->isSuccess()) {
35+
if (!Transaction::commit()) {
36+
throw new ErrorException("Transaction can't commit the changes");
37+
}
38+
}
39+
} catch (Throwable $throw) {
40+
$fail = $throw;
41+
}
42+
43+
if ($fail) {
44+
$response = $http->fallback($fail);
45+
}
46+
47+
if ($output) {
48+
$http->output($response);
49+
}
50+
51+
return $response;
52+
}
53+
54+
/**
55+
* Singleton to Request to keep only one instance for each request
56+
*
57+
* @return Request Request object populated by server data
58+
*/
59+
public static function request()
60+
{
61+
$container = Container::box();
62+
if (!$container->has('request')) {
63+
$request = new Request(Kernel::options('strict'));
64+
$container->register('request', $request->fromServer());
65+
}
66+
return $container->get('request');
67+
}
68+
69+
/**
70+
* Simple helper to generate a valid route to resources of project
71+
*
72+
* Ex.: `self::route('/download/images/picture.png')`, will print //localhost/download/images/picture.png
73+
*
74+
* @param string $uri Path to route
75+
* @return string
76+
*/
77+
public static function route($uri)
78+
{
79+
return '//' . self::request()->getUrl() . '/' . ($uri{0} === '/' ? substr($uri, 1) : $uri);
80+
}
81+
}

src/Kernel/Http.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Simples\Http\Kernel;
4+
5+
use Simples\Http\Request;
6+
use Simples\Http\Response;
7+
use Simples\Route\Match;
8+
use Simples\Route\Router;
9+
use Throwable;
10+
11+
/**
12+
* Class Http
13+
* @package Simples\Kernel
14+
*/
15+
class Http
16+
{
17+
/**
18+
* @var array
19+
*/
20+
const METHODS = ['get', 'post', 'put', 'patch', 'delete', 'options', 'head', 'find', 'purge', 'deletehard'];
21+
22+
/**
23+
* @var Request
24+
*/
25+
private $request;
26+
27+
/**
28+
* @var Match
29+
*/
30+
private $match;
31+
32+
/**
33+
* Http constructor.
34+
* @param Request $request
35+
*/
36+
public function __construct(Request $request)
37+
{
38+
$this->request = $request;
39+
}
40+
41+
/**
42+
* Load the routes of project
43+
*
44+
* @param Router $router The router what will be used
45+
* @param array $files (null) If not informe will be used "route.files"
46+
* @return Router Object with the routes loaded in
47+
*/
48+
public static function routes(Router $router, array $files = null)
49+
{
50+
$files = $files ? $files : App::config('route.files');
51+
52+
foreach ($files as $file) {
53+
$router->load(path(true, $file));
54+
}
55+
56+
return $router;
57+
}
58+
59+
/**
60+
* @return Response
61+
*/
62+
public function handler(): Response
63+
{
64+
// TODO: container
65+
$router = new Router(App::options('labels'), App::options('type'));
66+
67+
// TODO: make routes here
68+
/** @var Match $match */
69+
$this->match = static::routes($router)->match($this->request->getMethod(), $this->request->getUri());
70+
71+
$handler = new HttpHandler($this->request, $this->match);
72+
73+
return $handler->apply();
74+
}
75+
76+
/**
77+
* @param Throwable $fail
78+
* @return Response
79+
*/
80+
public function fallback(Throwable $fail): Response
81+
{
82+
if (!$this->match) {
83+
$method = '';
84+
$uri = '';
85+
$path = '';
86+
$callback = null;
87+
$parameters = [];
88+
$options = [];
89+
$this->match = new Match($method, $uri, $path, $callback, $parameters, $options);
90+
}
91+
$this->match->setCallback($fail);
92+
93+
$handler = new HttpHandler($this->request, $this->match);
94+
95+
return $handler->apply();
96+
}
97+
98+
/**
99+
* @param Response $response
100+
*/
101+
public function output(Response $response)
102+
{
103+
$headers = $response->getHeaders();
104+
foreach ($headers as $name => $value) {
105+
header(implode(':', [$name, $value]), true);
106+
}
107+
108+
http_response_code($response->getStatusCode());
109+
110+
$contents = $response->getBody()->getContents();
111+
if ($contents) {
112+
out($contents);
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)