Skip to content

Commit

Permalink
Fix issue if ServerRequestInterface is an argument
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 26, 2023
1 parent 8dea6e3 commit 1adbc80
Show file tree
Hide file tree
Showing 20 changed files with 364 additions and 167 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes to `Slytherin` will be documented in this file.
### Fixed
- Type hinting of all classes using `PHPStan`
- Checking of available instances in `Container::value`
- Changed `ServerRequestInterface` in middleware does not reflect if the said interface is defined as an argument

## [0.9.6](https://github.com/rougin/slytherin/compare/v0.9.5...v0.9.6) - 2023-11-16

Expand Down
6 changes: 6 additions & 0 deletions src/Application/FinalCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class FinalCallback
{
const REQUEST = 'Psr\Http\Message\ServerRequestInterface';

const RESPONSE = 'Psr\Http\Message\ResponseInterface';

/**
Expand Down Expand Up @@ -54,6 +56,10 @@ public function __invoke(ServerRequestInterface $request)
return $this->finalize($this->function);
}

// Attach the request again in the container to reflect from stack ---
$this->container->set(self::REQUEST, $request);
// -------------------------------------------------------------------

/** @var array<int, \Closure|string|array<int, string>> */
$function = $this->function;

Expand Down
65 changes: 0 additions & 65 deletions tests/Forward/ApplicationTest.php

This file was deleted.

36 changes: 0 additions & 36 deletions tests/Forward/Fixture/Config/app.php

This file was deleted.

27 changes: 0 additions & 27 deletions tests/Forward/Fixture/Router.php

This file was deleted.

28 changes: 0 additions & 28 deletions tests/Forward/Fixture/Routes/Hello.php

This file was deleted.

27 changes: 25 additions & 2 deletions tests/Forward/Fixture/Builder.php → tests/Sample/Builder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture;
namespace Rougin\Slytherin\Sample;

use Rougin\Slytherin\Application;
use Rougin\Slytherin\Configuration;
Expand All @@ -16,23 +16,46 @@ class Builder

protected $files = array();

protected $handlers = array();

protected $query = array();

protected $parsed = array();

protected $server = array();

public function addHandler($handler)
{
$this->handlers[] = $handler;

return $this;
}

public function make()
{
$config = new Configuration;

$config->load(__DIR__ . '/Config');

// Set the server request globals -----------------
$config->set('app.http.cookies', $this->cookies);

$config->set('app.http.files', $this->files);
$config->set('app.http.get', $this->query);

$config->set('app.http.get', (array) $this->query);

$config->set('app.http.post', $this->parsed);

$config->set('app.http.server', $this->server);
// ------------------------------------------------

// Set the custom middlewares ----------------
$items = $config->get('app.middlewares');

$items = array_merge($items, $this->handlers);

$config->set('app.middlewares', $items);
// -------------------------------------------

$container = new Container;

Expand Down
27 changes: 27 additions & 0 deletions tests/Sample/Config/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Rougin\Slytherin\Sample\Router;

$packages = array();

// Testing Packages -----------------------------------------------
$packages[] = 'Rougin\Slytherin\Sample\Packages\HttpPackage';
$packages[] = 'Rougin\Slytherin\Sample\Packages\MiddlewarePackage';
$packages[] = 'Rougin\Slytherin\Sample\Packages\RoutingPackage';
// ----------------------------------------------------------------

$config = array('name' => 'Slytherin');

$config['base_url'] = 'http://localhost:8000';

$config['environment'] = 'development';

$config['timezone'] = 'Asia/Manila';

$config['router'] = new Router;

$config['middlewares'] = array();

$config['packages'] = $packages;

return (array) $config;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Depots;
namespace Rougin\Slytherin\Sample\Depots;

/**
* @package Slytherin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Depots;
namespace Rougin\Slytherin\Sample\Depots;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand Down
23 changes: 23 additions & 0 deletions tests/Sample/Handlers/Parsed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rougin\Slytherin\Sample\Handlers;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Parsed implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$data = array('name' => 'Slytherin');

$request = $request->withParsedBody($data);

return $delegate->process($request);
}
}
21 changes: 21 additions & 0 deletions tests/Sample/Handlers/ToJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rougin\Slytherin\Sample\Handlers;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class ToJson implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
$response = $delegate->process($request);

return $response->withHeader('Content-Type', 'application/json');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Packages;
namespace Rougin\Slytherin\Sample\Packages;

use Rougin\Slytherin\Http\HttpIntegration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Packages;
namespace Rougin\Slytherin\Sample\Packages;

use Rougin\Slytherin\Middleware\MiddlewareIntegration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Packages;
namespace Rougin\Slytherin\Sample\Packages;

use Rougin\Slytherin\Routing\RoutingIntegration;

Expand Down
Loading

0 comments on commit 1adbc80

Please sign in to comment.