Skip to content

Commit 70e6580

Browse files
init version 2.0
1 parent 1d0a3ad commit 70e6580

File tree

8 files changed

+357
-89
lines changed

8 files changed

+357
-89
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MIT License
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"psr/http-server-handler": "^1.0",
2121
"psr/http-server-middleware": "^1.0",
2222
"bmt/plural-converter": "1.0",
23-
"effectra/http-server-handler":"1.0",
23+
"effectra/http-server-handler":"2.0",
2424
"effectra/http-message": "1.0"
2525
},
2626
"minimum-stability": "stable"

src/Controller.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
4+
declare(strict_types=1);
5+
6+
namespace Effectra\Router;
7+
8+
use Effectra\Router\Exception\InvalidCallbackException;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\ServerRequestInterface;
11+
12+
/**
13+
* Class Controller
14+
*
15+
* Represents a controller that handles requests and generates responses.
16+
*/
17+
class Controller
18+
{
19+
protected ServerRequestInterface $request;
20+
protected ResponseInterface $response;
21+
protected $callback;
22+
protected array $args;
23+
24+
/**
25+
* Controller constructor.
26+
*
27+
* @param ServerRequestInterface $request The server request.
28+
* @param ResponseInterface $response The response.
29+
* @param array $args The arguments.
30+
* @param null|callable $callback The callback function to handle the request.
31+
*
32+
* @return void
33+
*/
34+
public function __construct(ServerRequestInterface $request, ResponseInterface $response, array $args, ?callable $callback = null)
35+
{
36+
$this->request = $request;
37+
$this->response = $response;
38+
$this->args = $args;
39+
$this->callback = $callback;
40+
}
41+
42+
/**
43+
* Get the server request.
44+
*
45+
* @return ServerRequestInterface The server request.
46+
*/
47+
public function getRequest(): ServerRequestInterface
48+
{
49+
return $this->request;
50+
}
51+
52+
/**
53+
* Set the server request.
54+
*
55+
* @param ServerRequestInterface $request The server request.
56+
*
57+
* @return void
58+
*/
59+
public function setRequest(ServerRequestInterface $request): void
60+
{
61+
$this->request = $request;
62+
}
63+
64+
/**
65+
* Get the response.
66+
*
67+
* @return ResponseInterface The response.
68+
*/
69+
public function getResponse(): ResponseInterface
70+
{
71+
return $this->response;
72+
}
73+
74+
/**
75+
* Set the response.
76+
*
77+
* @param ResponseInterface $response The response.
78+
*
79+
* @return void
80+
*/
81+
public function setResponse(ResponseInterface $response): void
82+
{
83+
$this->response = $response;
84+
}
85+
86+
/**
87+
* Get the arguments.
88+
*
89+
* @return array The arguments.
90+
*/
91+
public function getArgs(): array
92+
{
93+
return $this->args;
94+
}
95+
96+
/**
97+
* Set the arguments.
98+
*
99+
* @param array $args The arguments.
100+
*
101+
* @return void
102+
*/
103+
public function setArgs(array $args): void
104+
{
105+
$this->args = $args;
106+
}
107+
108+
/**
109+
* Convert the controller to an array.
110+
*
111+
* @return array The controller as an array.
112+
*/
113+
public function toArray(): array
114+
{
115+
return [
116+
$this->request,
117+
$this->response,
118+
(object) $this->args
119+
];
120+
}
121+
122+
/**
123+
* Set the callback function to handle the request.
124+
*
125+
* @param callable $callback The callback function.
126+
*
127+
* @return void
128+
*/
129+
public function setCallback(callable $callback)
130+
{
131+
$this->callback = $callback;
132+
}
133+
134+
/**
135+
* Get the callback function.
136+
*
137+
* @return callable|null The callback function or null if not set.
138+
*/
139+
public function getCallback(): callable|null
140+
{
141+
return $this->callback;
142+
}
143+
144+
/**
145+
* Handle the request and return the response.
146+
*
147+
* @throws InvalidCallbackException If the callback is not set.
148+
*
149+
* @return ResponseInterface The response.
150+
*/
151+
public function handle(): ResponseInterface
152+
{
153+
$this->args = array_merge($this->args, $this->request->getQueryParams());
154+
155+
if (!$this->callback) {
156+
throw new InvalidCallbackException("Error Processing Response");
157+
}
158+
159+
$response = call_user_func_array($this->callback, $this->toArray());
160+
161+
return $response;
162+
}
163+
}

0 commit comments

Comments
 (0)