Skip to content

Commit 14f6d49

Browse files
committed
Finish 1.1.0
2 parents ce963a3 + d2ad460 commit 14f6d49

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

src/Controller.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Simples\Http;
44

55
use Simples\Kernel\App;
6+
use Simples\Http\Kernel\App as Http;
67
use Simples\Route\Match;
78

89
/**
@@ -115,6 +116,9 @@ abstract protected function answer($content = null, $meta = [], $code = 200) : R
115116
*/
116117
final protected function request()
117118
{
119+
if (!$this->request) {
120+
$this->request = Http::request();
121+
}
118122
return $this->request;
119123
}
120124

@@ -123,6 +127,9 @@ final protected function request()
123127
*/
124128
final protected function response()
125129
{
130+
if (!$this->response) {
131+
$this->response = Http::response();
132+
}
126133
return $this->response;
127134
}
128135

src/Kernel/App.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Simples\Http\Kernel;
44

55
use ErrorException;
6+
use Simples\Http\Response;
67
use Simples\Kernel\App as Kernel;
78
use Simples\Http\Request;
89
use Simples\Kernel\Container;
@@ -78,4 +79,18 @@ public static function route($uri)
7879
{
7980
return '//' . self::request()->getUrl() . '/' . ($uri{0} === '/' ? substr($uri, 1) : $uri);
8081
}
82+
83+
/**
84+
* Singleton to Response to keep only one instance for each request
85+
*
86+
* @return Response Response object populated by server data
87+
*/
88+
public static function response()
89+
{
90+
$container = Container::box();
91+
if (!$container->has('response')) {
92+
$container->register('response', new Response());
93+
}
94+
return $container->get('response');
95+
}
8196
}

src/Kernel/Http.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Simples\Http\Kernel;
44

5+
use Simples\Kernel\App as Kernel;
56
use Simples\Http\Request;
67
use Simples\Http\Response;
78
use Simples\Route\Match;
@@ -38,6 +39,23 @@ public function __construct(Request $request)
3839
$this->request = $request;
3940
}
4041

42+
/**
43+
* @return Response
44+
*/
45+
public function handler(): Response
46+
{
47+
// TODO: container
48+
$router = new Router(Kernel::options('labels'), Kernel::options('type'));
49+
50+
// TODO: make routes here
51+
/** @var Match $match */
52+
$this->match = static::routes($router)->match($this->request->getMethod(), $this->request->getUri());
53+
54+
$handler = new HttpHandler($this->request, $this->match);
55+
56+
return $handler->apply();
57+
}
58+
4159
/**
4260
* Load the routes of project
4361
*
@@ -47,7 +65,7 @@ public function __construct(Request $request)
4765
*/
4866
public static function routes(Router $router, array $files = null)
4967
{
50-
$files = $files ? $files : App::config('route.files');
68+
$files = $files ? $files : Kernel::config('route.files');
5169

5270
foreach ($files as $file) {
5371
$router->load(path(true, $file));
@@ -56,23 +74,6 @@ public static function routes(Router $router, array $files = null)
5674
return $router;
5775
}
5876

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-
7677
/**
7778
* @param Throwable $fail
7879
* @return Response

src/Kernel/HttpHandler.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Simples\Http\Controller;
77
use Simples\Http\Request;
88
use Simples\Http\Response;
9+
use Simples\Kernel\Container;
910
use Simples\Route\Match;
1011
use Simples\Kernel\Wrapper;
1112
use Throwable;

src/Response.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Simples\Http;
44

55
use Simples\Helper\JSON;
6-
use Simples\Kernel\App;
76

87
/**
98
* @method Response atom($data, $code = 200)
@@ -103,7 +102,6 @@ public function header($name, $value = '')
103102
public function __call($name, $arguments)
104103
{
105104
$this->write($this->toString(off($arguments, 0, null)));
106-
107105
$this->header('content-type', off(self::CONTENT_TYPES, $name, self::CONTENT_TYPE_UNKNOWN));
108106

109107
if (off($arguments, 1)) {
@@ -126,7 +124,7 @@ public function toString($data)
126124
if (in_array(gettype($data), $scalars)) {
127125
return (string)$data;
128126
}
129-
return json_encode($data, JSON_NUMERIC_CHECK);
127+
return JSON::encode($data, JSON_NUMERIC_CHECK);
130128
}
131129

132130
/**

0 commit comments

Comments
 (0)