-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Routing for creating HTTP routes directly to Application
- Loading branch information
Showing
8 changed files
with
190 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |