Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace demo;
- use mysql_xdevapi\Exception;
- use Phroute\Phroute\Dispatcher;
- use Phroute\Phroute\RouteCollector;
- use Phroute\Phroute\RouteDataArray;
- use Spatie\Async\Pool;
- require_once($_SERVER['DOCUMENT_ROOT'] . '/' . 'vendor/autoload.php');
- require_once($_SERVER['DOCUMENT_ROOT'] . '/' . 'src/components/twig.php');
- /**
- * Class router
- * @package demo
- */
- Class router extends twig {
- /**
- * Asynchronous pool object
- *
- * @var
- */
- private $pool;
- /**
- * PHRoute Objects.
- *
- * @var
- */
- private $parser, $collector, $dispatcher;
- /**
- * HTTP Requests objects
- *
- * @var
- */
- private $uri, $httpMethod;
- /**
- * router constructor.
- * @param string $uri
- * @param string $httpMethod
- * @throws \Exception
- */
- public function __construct(string $uri, string $httpMethod)
- {
- parent::__construct();
- $allowed_methods = [
- 'GET',
- 'HEAD',
- 'POST',
- ];
- $this->parser = new \Phroute\Phroute\RouteParser();
- $this->collector = new RouteCollector( $this->parser );
- $this->pool = \Spatie\Async\Pool::create();
- $this->uri = parse_url( $uri, PHP_URL_PATH );
- if ( in_array( $httpMethod, $allowed_methods ) ) {
- $this->httpMethod = $httpMethod;
- } else {
- throw new \Exception("This request method is not allowed.");
- }
- }
- /**
- * Sets the route
- */
- public function setRoute()
- {
- $this->pool = $this->pool->add(async(function () {
- $this->collector->get($this->uri, function ()
- {
- if ($this->uri === "/login" || $this->uri === "/signin")
- {
- return $this->getTwig()->render('login.html.twig', [
- 'title' => 'login'
- ]);
- }
- });
- })->then(function ($output) {
- return $output;
- })->catch(function (\Exception $exception) {
- return $exception;
- }));
- return await($this->pool);
- }
- /**
- * @return RouteDataArray
- */
- public function routeCollectorData() : RouteDataArray
- {
- $this->pool[] = (async(function () {
- $this->pool->add(function () {
- return await($this->collector->getData());
- })->then(function ($output) {
- return $output;
- })->catch(function (\Exception $exception) {
- throw new \Exception($exception->getMessage());
- })->timeout(function ($output) {
- return $output;
- });
- }));
- return await($this->pool);
- }
- /**
- * Renders the route specified by the user
- *
- * @return mixed|null
- */
- public function route()
- {
- $this->pool = $this->pool->add(async(function () {
- try {
- $this->setRoute();
- $this->dispatcher = new \Phroute\Phroute\Dispatcher( $this->collector->getData() );
- return await($this->dispatcher->dispatch( $this->httpMethod, $this->uri ) );
- } catch ( \Exception $e ) {
- return $this->badRoute();
- }
- })->then(function ($output) {
- return $output;
- })->catch(function (\Exception $exception) {
- throw new \Exception("Could not dispatch route!", $exception->getCode());
- }));
- return await($this->pool);
- }
- private function badRoute() {
- //TODO: Finish this later
- $async_data[] = $this->pool->add(async(function () {
- return await("this doesn't work");
- }));
- return $async_data;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement