Skip to content

Commit

Permalink
Add Routing for creating HTTP routes directly to Application
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Dec 24, 2023
1 parent ef55c82 commit 0f3accf
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to `Slytherin` will be documented in this file.
- Support for all versions of `http-interop/http-middleware` (`0.3`, `0.4`, `0.5`)
- `ERRATUM.md` for changes in `README.md` for specified versions
- Support versions for PHP `v8.3`
- `Routing` for creating HTTP routes directly in `Application`

### Changed
- Third-party packages in `Routing` extends to Slytherin's `Dispatcher`, `Router`
Expand Down
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@
},
"require-dev":
{
"phpunit/phpunit": "~4.2|~5.7|~6.0|~7.0|~8.0|~9.0",
"sanmai/phpunit-legacy-adapter": "~6.1|~8.0"
"filp/whoops": "~2.0",
"http-interop/http-middleware": "0.4.1",
"league/container": "~4.0",
"nikic/fast-route": "~1.0",
"phpunit/phpunit": "~4.0|~5.0|~6.0|~7.0|~8.0|~9.0",
"phroute/phroute": "~2.0",
"rdlowrey/auryn": "~1.0",
"sanmai/phpunit-legacy-adapter": "~6.0|~8.0",
"twig/twig": "~1.0",
"zendframework/zend-diactoros": "~2.0",
"zendframework/zend-stratigility": "~3.0"
},
"autoload":
{
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<phpunit bootstrap="vendor/autoload.php" colors="true" stopOnError="true">
<testsuites>
<testsuite name="Slytherin Test Suite">
<directory>tests</directory>
Expand Down
6 changes: 4 additions & 2 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Rougin\Slytherin;

use Rougin\Slytherin\System\Routing;

/**
* Application
*
* NOTE: To be removed in v1.0.0. Use "System" instead.
* NOTE: To be removed in v1.0.0. Use "Routing" instead.
*
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Application extends System
class Application extends Routing
{
}
17 changes: 4 additions & 13 deletions src/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Rougin\Slytherin;

use Psr\Http\Message\ServerRequestInterface;
use Rougin\Slytherin\Component\Collection;
use Rougin\Slytherin\Container\Container;
use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Integration\Configuration;
Expand Down Expand Up @@ -50,26 +49,18 @@ class System
/**
* Initializes the application instance.
*
* @param mixed|null $container
* @param \Rougin\Slytherin\Integration\Configuration|null $config
* @param \Rougin\Slytherin\Container\ContainerInterface|null $container
* @param \Rougin\Slytherin\Integration\Configuration|null $config
*/
public function __construct($container = null, Configuration $config = null)
public function __construct(ContainerInterface $container = null, Configuration $config = null)
{
if (! $config) $config = new Configuration;

$this->config = $config;

if ($container instanceof Collection)
{
$container = $container->getContainer();
}

if (! $container) $container = new Container;

if ($container instanceof ContainerInterface)
{
$this->container = $container;
}
$this->container = $container;
}

/**
Expand Down
99 changes: 99 additions & 0 deletions src/System/Routing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Rougin\Slytherin\System;

use Rougin\Slytherin\Http\HttpIntegration;
use Rougin\Slytherin\Routing\Router;
use Rougin\Slytherin\Routing\RoutingIntegration;
use Rougin\Slytherin\System;

/**
* Routing
*
* A routing utility for defining HTTP routes directly.
*
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Routing extends System
{
/**
* @var \Rougin\Slytherin\Routing\RouterInterface|null
*/
protected $router = null;

/**
* Adds a new raw route.
*
* @param string $method
* @param string $uri
* @param callable|string[]|string $handler
* @param \Rougin\Slytherin\Middleware\MiddlewareInterface[]|string[] $middlewares
* @return self
*/
public function add($method, $uri, $handler, $middlewares = array())
{
if (is_null($this->router))
{
$this->router = new Router;
}

$this->router->add($method, $uri, $handler, $middlewares);

return $this;
}

/**
* Emits the headers from response and runs the application.
*
* @return void
*/
public function run()
{
if (is_null($this->router))
{
parent::run(); return;
}

// Prepare the HttpIntegration -------------------
$this->config->set('app.http.cookies', $_COOKIE);

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

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

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

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

$items = array(new HttpIntegration);
// -----------------------------------------------

// Prepare the RoutingIntegration -------------------
$items[] = new RoutingIntegration;

$this->integrate($items);

$this->container->set(System::ROUTER, $this->router);
// --------------------------------------------------

parent::run(); return;
}

/**
* Calls methods from the Router instance.
*
* @param string $method
* @param mixed[] $params
* @return mixed
*/
public function __call($method, $params)
{
array_unshift($params, $method);

/** @var callable $class */
$class = array($this, 'add');

return call_user_func_array($class, $params);
}
}
34 changes: 34 additions & 0 deletions tests/Forward/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rougin\Slytherin\Forward;

use Rougin\Slytherin\Application;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Builder
{
public function make()
{
$app = new Application;

$app->get('/', 'Rougin\Slytherin\Fixture\Classes\NewClass@index');

return $app;
}

public function setUrl($method, $uri)
{
$_SERVER['REQUEST_METHOD'] = $method;

$_SERVER['REQUEST_URI'] = $uri;

$_SERVER['SERVER_NAME'] = 'localhost';

$_SERVER['SERVER_PORT'] = '8000';

return $this;
}
}
36 changes: 36 additions & 0 deletions tests/Forward/ForwardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rougin\Slytherin\Forward;

use Rougin\Slytherin\Testcase;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class ForwardTest extends Testcase
{
/**
* @var \Rougin\Slytherin\Forward\Builder
*/
protected $builder;

protected function doSetUp()
{
$this->builder = new Builder;
}

/**
* @runInSeparateProcess
*
* @return void
*/
public function test_get_method()
{
$this->builder->setUrl('GET', '/');

$this->expectOutputString('Hello');

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

0 comments on commit 0f3accf

Please sign in to comment.