Skip to content

Commit

Permalink
init version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Jun 29, 2023
1 parent 1d0a3ad commit 70e6580
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 89 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MIT License

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"bmt/plural-converter": "1.0",
"effectra/http-server-handler":"1.0",
"effectra/http-server-handler":"2.0",
"effectra/http-message": "1.0"
},
"minimum-stability": "stable"
Expand Down
163 changes: 163 additions & 0 deletions src/Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php


declare(strict_types=1);

namespace Effectra\Router;

use Effectra\Router\Exception\InvalidCallbackException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* Class Controller
*
* Represents a controller that handles requests and generates responses.
*/
class Controller
{
protected ServerRequestInterface $request;
protected ResponseInterface $response;
protected $callback;
protected array $args;

/**
* Controller constructor.
*
* @param ServerRequestInterface $request The server request.
* @param ResponseInterface $response The response.
* @param array $args The arguments.
* @param null|callable $callback The callback function to handle the request.
*
* @return void
*/
public function __construct(ServerRequestInterface $request, ResponseInterface $response, array $args, ?callable $callback = null)
{
$this->request = $request;
$this->response = $response;
$this->args = $args;
$this->callback = $callback;
}

/**
* Get the server request.
*
* @return ServerRequestInterface The server request.
*/
public function getRequest(): ServerRequestInterface
{
return $this->request;
}

/**
* Set the server request.
*
* @param ServerRequestInterface $request The server request.
*
* @return void
*/
public function setRequest(ServerRequestInterface $request): void
{
$this->request = $request;
}

/**
* Get the response.
*
* @return ResponseInterface The response.
*/
public function getResponse(): ResponseInterface
{
return $this->response;
}

/**
* Set the response.
*
* @param ResponseInterface $response The response.
*
* @return void
*/
public function setResponse(ResponseInterface $response): void
{
$this->response = $response;
}

/**
* Get the arguments.
*
* @return array The arguments.
*/
public function getArgs(): array
{
return $this->args;
}

/**
* Set the arguments.
*
* @param array $args The arguments.
*
* @return void
*/
public function setArgs(array $args): void
{
$this->args = $args;
}

/**
* Convert the controller to an array.
*
* @return array The controller as an array.
*/
public function toArray(): array
{
return [
$this->request,
$this->response,
(object) $this->args
];
}

/**
* Set the callback function to handle the request.
*
* @param callable $callback The callback function.
*
* @return void
*/
public function setCallback(callable $callback)
{
$this->callback = $callback;
}

/**
* Get the callback function.
*
* @return callable|null The callback function or null if not set.
*/
public function getCallback(): callable|null
{
return $this->callback;
}

/**
* Handle the request and return the response.
*
* @throws InvalidCallbackException If the callback is not set.
*
* @return ResponseInterface The response.
*/
public function handle(): ResponseInterface
{
$this->args = array_merge($this->args, $this->request->getQueryParams());

if (!$this->callback) {
throw new InvalidCallbackException("Error Processing Response");
}

$response = call_user_func_array($this->callback, $this->toArray());

return $response;
}
}
Loading

0 comments on commit 70e6580

Please sign in to comment.