Skip to content

Commit

Permalink
Merge branch 'master' into packages
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Mar 24, 2024
2 parents 70cf221 + 6ba5854 commit bebb384
Show file tree
Hide file tree
Showing 24 changed files with 771 additions and 665 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ All notable changes to `Weasley` will be documented in this file.
### Changed
- Conformed HTTP middlewares from `http-interop/http-middleware` to `rougin/slytherin`'s own middleware
- Improve code quality and code formatting with `phpstan`, `php-cs-fixer`
- `Controllers` directory to `Routes` directory
- `Middleware` directory to `Handlers` directory

### Fixed
- Unit tests in running `SessionIntegration`
Expand Down
36 changes: 21 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ Access the generator commands through `vendor/bin/weasley` in the terminal/comma

| Command | Description |
| ------- | ----------- |
| make:check | Creates a new check (validation) class based on [Valitron](https://github.com/vlucas/valitron) |
| make:handler | Creates a new [Slytherin Middleware](https://github.com/rougin/slytherin/wiki/Middleware) class |
| make:package | Creates a new [Slytherin Integration](https://github.com/rougin/slytherin/wiki/IntegrationInterface-Implementation) class |
| make:route | Creates a new HTTP route class |
| make:check | Creates a new check (validation) class based on [Valitron](https://github.com/vlucas/valitron). |
| make:handler | Creates a new [Slytherin Middleware](https://github.com/rougin/slytherin/wiki/Middleware) class. |
| make:package | Creates a new [Slytherin Integration](https://github.com/rougin/slytherin/wiki/IntegrationInterface-Implementation) class. |
| make:route | Creates a new HTTP route class. |

### HTTP Controllers
### HTTP Routes

| Controller | Description |
| ---------- | ----------- |
| [JsonController](https://github.com/rougin/weasley/blob/master/src/Controllers/JsonController.php) | Provides methods for RESTful APIs in [JSON](https://en.wikipedia.org/wiki/JSON) format |
| [HttpRoute](https://github.com/rougin/weasley/blob/master/src/Routes/HttpRoute.php) | A simple HTTP route class for RESTful APIs. |
| [JsonRoute](https://github.com/rougin/weasley/blob/master/src/Routes/JsonRoute.php) | Provides methods for RESTful APIs in [JSON](https://en.wikipedia.org/wiki/JSON) format. |

**NOTE**: In other PHP frameworks, this is also known as `Controllers`.

### Packages

Expand All @@ -48,17 +51,20 @@ The following classes below are using the [`IntegrationInterface`](https://githu

**NOTE**: The mentioned integrations above needs to include their required dependencies first.

### HTTP Middlewares
### HTTP Handlers

The following classes below uses the [Middleware](https://github.com/rougin/slytherin/wiki/Middleware) component of Slytherin:

| Middleware | Description |
| Handler | Description |
| ---------- | ----------- |
| [CrossOriginHeaders](https://github.com/rougin/weasley/blob/master/src/Middleware/CrossOriginHeaders.php) | Adds additional headers for [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) |
| [EmptyStringToNull](https://github.com/rougin/weasley/blob/master/src/Middleware/EmptyStringToNull.php) | Converts the empty strings from request as `null` |
| [SpoofFormMethod](https://github.com/rougin/weasley/blob/master/src/Middleware/SpoofFormMethod.php) | Replaces the HTTP verb from `_method` value |
| [JsonHeaders](https://github.com/rougin/weasley/blob/master/src/Middleware/Json.php) | Changes content response to `application/json` |
| [TrimString](https://github.com/rougin/weasley/blob/master/src/Middleware/TrimString.php) | Trims the strings from an incoming request |
| [AllowCrossOrigin](https://github.com/rougin/weasley/blob/master/src/Handlers/AllowCrossOrigin.php) | Adds additional headers for [Cross-origin resource sharing](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) (CORS). |
| [EmptyStringToNull](https://github.com/rougin/weasley/blob/master/src/Handlers/EmptyStringToNull.php) | Converts the empty strings from request as `null`. |
| [JsonContentType](https://github.com/rougin/weasley/blob/master/src/Handlers/JsonContentType.php) | Changes content response to `application/json`. |
| [MutateRequest](https://github.com/rougin/weasley/blob/master/src/Handlers/MutateRequest.php) | A middleware that can be extended to mutate/transform values from the request. |
| [SpoofHttpMethod](https://github.com/rougin/weasley/blob/master/src/Handlers/SpoofHttpMethod.php) | Replaces the HTTP verb from `_method` value. |
| [TrimStringValue](https://github.com/rougin/weasley/blob/master/src/Handlers/TrimStringValue.php) | Trims the strings from an incoming request. |

**NOTE**: All of the HTTP middlewares above are implemented in the `v0.4.1` of [PSR-15](https://github.com/http-interop/http-middleware/tree/0.4.1).
**NOTE**: In other PHP frameworks, this is also known as `Middlewares`.

## Changelog

Expand Down Expand Up @@ -90,4 +96,4 @@ The MIT License (MIT). Please see [LICENSE][link-license] for more information.
[link-coverage]: https://app.codecov.io/gh/rougin/weasley
[link-downloads]: https://packagist.org/packages/rougin/weasley
[link-license]: https://github.com/rougin/weasley/blob/master/LICENSE.md
[link-packagist]: https://packagist.org/packages/rougin/weasley
[link-packagist]: https://packagist.org/packages/rougin/weasley
2 changes: 1 addition & 1 deletion src/Commands/MakeControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Rougin\Weasley\Scripts\CreateRoute;

/**
* @deprecated since v0.7, use "Scripts/CreateRoute" instead.
* @deprecated since ~0.7, use "Scripts/CreateRoute" instead.
*
* Make Controller Command
*
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MakeIntegrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Rougin\Weasley\Scripts\CreatePackage;

/**
* @deprecated since v0.7, use "Scripts/CreatePackage" instead.
* @deprecated since ~0.7, use "Scripts/CreatePackage" instead.
*
* Make Integration Command
*
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MakeMiddlewareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Rougin\Weasley\Scripts\CreateHandler;

/**
* @deprecated since v0.7, use "Scripts/CreateHandler" instead.
* @deprecated since ~0.7, use "Scripts/CreateHandler" instead.
*
* Make Middleware Command
*
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/MakeValidatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Rougin\Weasley\Scripts\CreateCheck;

/**
* @deprecated since v0.7, use "Scripts/CreateCheck" instead.
* @deprecated since ~0.7, use "Scripts/CreateCheck" instead.
*
* Make Validator Command
*
Expand Down
80 changes: 4 additions & 76 deletions src/Controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,16 @@

namespace Rougin\Weasley\Controllers;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Weasley\Transformer\JsonTransformer;
use Rougin\Weasley\Routes\HttpRoute;

/**
* @deprecated since ~0.7, use "HttpRoute" instead.
*
* Base Controller
*
* @package Weasley
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class BaseController
class BaseController extends HttpRoute
{
/**
* @var \Psr\Http\Message\ServerRequestInterface
*/
protected $request;

/**
* @var \Psr\Http\Message\ResponseInterface
*/
protected $response;

/**
* Initializes the controller instance.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @param \Psr\Http\Message\ResponseInterface $response
*/
public function __construct(ServerRequestInterface $request, ResponseInterface $response)
{
$parsed = $request->getParsedBody();

is_null($parsed) && $parsed = array();

$this->request = $request->withParsedBody($parsed);

$this->response = $response;
}

/**
* Sets the server request instance.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return self
*/
public function request(ServerRequestInterface $request)
{
$this->request = $request;

return $this;
}

/**
* Returns the specified data to JSON.
* NOTE: Must be moved to JsonController in v1.0.0.
* The visibility of this method must also be "protected".
*
* @param mixed $data
* @param integer $code
* @param integer $options
* @return \Psr\Http\Message\ResponseInterface
*/
public function json($data, $code = 200, $options = 0)
{
$response = $this->response->withStatus($code);

$transformer = new JsonTransformer($response, $options);

return $transformer->transform($data);
}

/**
* Returns the specified data to JSON.
* NOTE: To be removed in v1.0.0. Use "json" method instead.
*
* @param mixed $data
* @param integer $code
* @param integer $options
* @return \Psr\Http\Message\ResponseInterface
*/
public function toJson($data, $code = 200, $options = 0)
{
return $this->json($data, $code, $options);
}
}
Loading

0 comments on commit bebb384

Please sign in to comment.