Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit 9469fc1

Browse files
authored
Merge pull request #43 from modulusphp/feature/response-handler
Feature/response handler
2 parents 09deef9 + 998c15d commit 9469fc1

4 files changed

Lines changed: 73 additions & 25 deletions

File tree

Response.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Modulus\Framework;
4+
5+
use Modulus\Http\Rest;
6+
use Modulus\Http\Redirect;
7+
use Modulus\Framework\Upstart;
8+
9+
class Response
10+
{
11+
/**
12+
* Make application response
13+
*
14+
* @param Upstart $app
15+
*/
16+
public static function make(Upstart $app)
17+
{
18+
/**
19+
* Get application response
20+
*/
21+
$response = $app->getResponse();
22+
23+
/**
24+
* Create a rest response
25+
*/
26+
if (
27+
is_string($response) ||
28+
is_int($response) ||
29+
is_float($response) ||
30+
is_double($response) ||
31+
is_array($response)
32+
) return is_array($response) ? Rest::response()->json($response)->send() : Rest::response($response)->send();
33+
34+
if (is_bool($response)) return Rest::response($response ? 'true' : 'false')->send();
35+
36+
if ($response instanceof Rest) return $response->send();
37+
38+
/**
39+
* Create a redirect
40+
*/
41+
if ($response instanceof Redirect) return $response->send();
42+
43+
/**
44+
* Avoid "Segmentation fault (core dumped)"
45+
*/
46+
echo '';
47+
}
48+
}

Upstart.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,20 @@ class Upstart
3939
*/
4040
protected $request;
4141

42+
/**
43+
* $response
44+
*
45+
* @var mixed
46+
*/
47+
protected $response;
48+
4249
/**
4350
* Start application
4451
*
45-
* @return void
52+
* @param bool|null $isConsole
53+
* @return Upstart $this
4654
*/
47-
public function boot(?bool $isConsole = false) : void
55+
public function boot(?bool $isConsole = false) : Upstart
4856
{
4957
/**
5058
* Add cors to the request
@@ -55,7 +63,7 @@ public function boot(?bool $isConsole = false) : void
5563
* Don't load framework components, if
5664
* the application has already started.
5765
*/
58-
if (Upstart::$isReady) return;
66+
if (Upstart::$isReady) return $this;
5967

6068
/**
6169
* Load application components
@@ -69,6 +77,8 @@ public function boot(?bool $isConsole = false) : void
6977
* it has started.
7078
*/
7179
Upstart::$isReady = true;
80+
81+
return $this;
7282
}
7383

7484
/**
@@ -148,4 +158,14 @@ public function getRequest()
148158
{
149159
return $this->request;
150160
}
161+
162+
/**
163+
* Get application response
164+
*
165+
* @return mixed
166+
*/
167+
public function getResponse()
168+
{
169+
return $this->response;
170+
}
151171
}

Upstart/SwishEvents.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
namespace Modulus\Framework\Upstart;
44

5-
use Modulus\Http\Rest;
65
use Modulus\Http\Status;
76
use Modulus\Http\Request;
87
use Modulus\Utility\View;
9-
use Modulus\Http\Redirect;
108
use Modulus\Utility\Events;
119
use AtlantisPHP\Swish\Route;
1210
use Modulus\Utility\Reflect;
@@ -112,24 +110,6 @@ private function swishView()
112110
*/
113111
private function handleResponse($response)
114112
{
115-
/**
116-
* Create a rest response
117-
*/
118-
if (
119-
is_string($response) ||
120-
is_int($response) ||
121-
is_float($response) ||
122-
is_double($response) ||
123-
is_array($response)
124-
) return is_array($response) ? Rest::response()->json($response)->send() : Rest::response($response)->send();
125-
126-
if (is_bool($response)) return Rest::response($response ? 'true' : 'false')->send();
127-
128-
if ($response instanceof Rest) return $response->send();
129-
130-
/**
131-
* Create a redirect
132-
*/
133-
if ($response instanceof Redirect) return $response->send();
113+
$this->response = $response;
134114
}
135115
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "modulusphp/framework",
33
"description": "Framework component for Modulus",
4-
"version": "1.9.8.11",
4+
"version": "1.9.9.0",
55
"license": "MIT",
66
"type": "package",
77
"authors": [{

0 commit comments

Comments
 (0)