Skip to content

Commit

Permalink
Forward slash not require if setting $namespace in Router
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 26, 2023
1 parent e439b28 commit 5a0feff
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ All notable changes to `Slytherin` will be documented in this file.

### Fixed
- Type hinting of all classes using `PHPStan`
- Forward slash (`\\`) not required if setting `$namespace` in `Router`
- Checking of available instances in `Container::value`
- Changed `ServerRequestInterface` in middleware does not reflect if the said interface is defined as an argument
- If `ServerRequestInterface` is an argument with a middleware

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

Expand Down
2 changes: 2 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Application

const RENDERER = 'Rougin\Slytherin\Template\RendererInterface';

const ROUTER = 'Rougin\Slytherin\Routing\RouterInterface';

const SERVER_REQUEST = 'Psr\Http\Message\ServerRequestInterface';

/**
Expand Down
15 changes: 13 additions & 2 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class Router implements RouterInterface
*/
public function __construct(array $routes = array())
{
// Set prefix conditions if above is defined ---
$this->prefix($this->prefix, $this->namespace);
// ---------------------------------------------

foreach ($routes as $route)
{
/** @var string */
Expand Down Expand Up @@ -224,10 +228,17 @@ public function restful($route, $class, $middlewares = array())
*/
public function prefix($prefix = '', $namespace = null)
{
$this->namespace = ($namespace !== null) ? $namespace . '\\' : $this->namespace;

$this->prefix = $prefix;

if ($namespace)
{
$namespace = $namespace . '\\';

$namespace = str_replace('\\\\', '\\', $namespace);

$this->namespace = $namespace;
}

return $this;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Sample/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Builder

protected $query = array();

protected $packages = array();

protected $parsed = array();

protected $server = array();
Expand All @@ -31,6 +33,13 @@ public function addHandler($handler)
return $this;
}

public function addPackage($package)
{
$this->packages[] = $package;

return $this;
}

public function make()
{
$config = new Configuration;
Expand Down Expand Up @@ -61,8 +70,12 @@ public function make()

$app = new Application($container, $config);

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

$items = array_merge($items, $this->packages);
// -------------------------------------------

return $app->integrate($items);
}

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

namespace Rougin\Slytherin\Sample\Handlers;
namespace Rougin\Slytherin\Sample\Handlers\Parsed;

use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
Expand All @@ -10,7 +10,7 @@
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Parsed implements MiddlewareInterface
class Request implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
Expand Down
23 changes: 23 additions & 0 deletions tests/Sample/Handlers/Parsed/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rougin\Slytherin\Sample\Handlers\Parsed;

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

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

$response->getBody()->write('From middleware!');

return $response;
}
}
38 changes: 38 additions & 0 deletions tests/Sample/Packages/SamplePackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Rougin\Slytherin\Sample\Packages;

use Rougin\Slytherin\Application;
use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Integration\Configuration;
use Rougin\Slytherin\Integration\IntegrationInterface;
use Rougin\Slytherin\Sample\Retuor;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class SamplePackage implements IntegrationInterface
{
public function define(ContainerInterface $container, Configuration $config)
{
// Sets the default timezone ------------
$timezone = $config->get('app.timezone');

date_default_timezone_set($timezone);
// --------------------------------------

/** @var \Rougin\Slytherin\Routing\RouterInterface */
$router = $container->get(Application::ROUTER);

// Merge new routes to existing ---
$new = new Retuor;

$router->merge($new->routes());
// --------------------------------

$container->set(Application::ROUTER, $router);

return $container;
}
}
21 changes: 21 additions & 0 deletions tests/Sample/Retuor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rougin\Slytherin\Sample;

use Rougin\Slytherin\Routing\Router;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Retuor extends Router
{
protected $namespace = 'Rougin\Slytherin\Sample\Routes';

public function routes($parsed = false)
{
$this->get('/world', 'Hello@world');

return parent::routes($parsed);
}
}
2 changes: 2 additions & 0 deletions tests/Sample/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function routes($parsed = false)

$this->get('/hello', 'Hello@index');

$this->get('/response', 'Hello@response');

$this->get('/hi/:name', 'Hello@name');

$this->get('without-slash', 'Hello@string');
Expand Down
14 changes: 14 additions & 0 deletions tests/Sample/Routes/Hello.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rougin\Slytherin\Sample\Routes;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Sample\Depots\TestDepot;

Expand Down Expand Up @@ -41,11 +42,24 @@ public function param(ServerRequestInterface $request)
return 'Hello, ' . $data['name'] . '!';
}

/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function response(ResponseInterface $response)
{
return $response;
}

/**
* @return string
*/
public function string()
{
return 'This is a simple string.';
}

public function world()
{
return 'Hello string world!';
}
}
62 changes: 59 additions & 3 deletions tests/Sample/SampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Rougin\Slytherin\Forward;

use Rougin\Slytherin\Sample\Builder;
use Rougin\Slytherin\Sample\Handlers\Parsed;
use Rougin\Slytherin\Sample\Handlers\Parsed\Request;
use Rougin\Slytherin\Sample\Handlers\Parsed\Response;
use Rougin\Slytherin\Sample\Packages\SamplePackage;
use Rougin\Slytherin\Testcase;

/**
Expand All @@ -22,6 +24,28 @@ protected function doSetUp()
$this->builder = new Builder;
}

/**
* @runInSeparateProcess
*
* @return void
*/
public function test_with_sample_package_added()
{
$this->builder->addPackage(new SamplePackage);

$this->builder->setUrl('GET', '/');

$this->expectOutputString('Welcome home!');

$this->builder->make()->run();

$timezone = date_default_timezone_get();

$expected = 'Asia/Manila';

$this->assertEquals($expected, $timezone);
}

/**
* @runInSeparateProcess
*
Expand Down Expand Up @@ -127,7 +151,7 @@ public function test_with_callable_as_the_route_and_only_as_the_output()
*/
public function test_with_middleware_changing_the_request_constructor()
{
$this->builder->addHandler(new Parsed);
$this->builder->addHandler(new Request);

$this->builder->setUrl('GET', '/handler/conts');

Expand All @@ -143,12 +167,44 @@ public function test_with_middleware_changing_the_request_constructor()
*/
public function test_with_middleware_changing_the_request_parameter()
{
$this->builder->addHandler(new Parsed);
$this->builder->addHandler(new Request);

$this->builder->setUrl('GET', '/handler/param');

$this->expectOutputString('Hello, Slytherin!');

$this->builder->make()->run();
}

/**
* @runInSeparateProcess
*
* @return void
*/
public function test_with_middleware_changing_the_response_parameter()
{
$this->builder->addHandler(new Response);

$this->builder->setUrl('GET', '/response');

$this->expectOutputString('From middleware!');

$this->builder->make()->run();
}

/**
* @runInSeparateProcess
*
* @return void
*/
public function test_without_forward_slash_in_the_router_namespace()
{
$this->builder->addPackage(new SamplePackage);

$this->builder->setUrl('GET', '/world');

$this->expectOutputString('Hello string world!');

$this->builder->make()->run();
}
}

0 comments on commit 5a0feff

Please sign in to comment.