Advertisement
Guest User

Asynchronous PHP Router (Unfinished)

a guest
Oct 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. <?php
  2. namespace demo;
  3. use mysql_xdevapi\Exception;
  4. use Phroute\Phroute\Dispatcher;
  5. use Phroute\Phroute\RouteCollector;
  6. use Phroute\Phroute\RouteDataArray;
  7. use Spatie\Async\Pool;
  8.  
  9. require_once($_SERVER['DOCUMENT_ROOT'] . '/' . 'vendor/autoload.php');
  10. require_once($_SERVER['DOCUMENT_ROOT'] . '/' . 'src/components/twig.php');
  11.  
  12. /**
  13. * Class router
  14. * @package demo
  15. */
  16. Class router extends twig {
  17.  
  18. /**
  19. * Asynchronous pool object
  20. *
  21. * @var
  22. */
  23. private $pool;
  24.  
  25. /**
  26. * PHRoute Objects.
  27. *
  28. * @var
  29. */
  30. private $parser, $collector, $dispatcher;
  31.  
  32. /**
  33. * HTTP Requests objects
  34. *
  35. * @var
  36. */
  37. private $uri, $httpMethod;
  38.  
  39. /**
  40. * router constructor.
  41. * @param string $uri
  42. * @param string $httpMethod
  43. * @throws \Exception
  44. */
  45. public function __construct(string $uri, string $httpMethod)
  46. {
  47. parent::__construct();
  48.  
  49. $allowed_methods = [
  50. 'GET',
  51. 'HEAD',
  52. 'POST',
  53. ];
  54.  
  55. $this->parser = new \Phroute\Phroute\RouteParser();
  56. $this->collector = new RouteCollector( $this->parser );
  57. $this->pool = \Spatie\Async\Pool::create();
  58.  
  59. $this->uri = parse_url( $uri, PHP_URL_PATH );
  60.  
  61. if ( in_array( $httpMethod, $allowed_methods ) ) {
  62. $this->httpMethod = $httpMethod;
  63. } else {
  64. throw new \Exception("This request method is not allowed.");
  65. }
  66. }
  67.  
  68. /**
  69. * Sets the route
  70. */
  71. public function setRoute()
  72. {
  73. $this->pool = $this->pool->add(async(function () {
  74. $this->collector->get($this->uri, function ()
  75. {
  76. if ($this->uri === "/login" || $this->uri === "/signin")
  77. {
  78. return $this->getTwig()->render('login.html.twig', [
  79. 'title' => 'login'
  80. ]);
  81. }
  82. });
  83. })->then(function ($output) {
  84. return $output;
  85. })->catch(function (\Exception $exception) {
  86. return $exception;
  87. }));
  88. return await($this->pool);
  89. }
  90.  
  91. /**
  92. * @return RouteDataArray
  93. */
  94. public function routeCollectorData() : RouteDataArray
  95. {
  96. $this->pool[] = (async(function () {
  97. $this->pool->add(function () {
  98. return await($this->collector->getData());
  99. })->then(function ($output) {
  100. return $output;
  101. })->catch(function (\Exception $exception) {
  102. throw new \Exception($exception->getMessage());
  103. })->timeout(function ($output) {
  104. return $output;
  105. });
  106. }));
  107. return await($this->pool);
  108. }
  109.  
  110. /**
  111. * Renders the route specified by the user
  112. *
  113. * @return mixed|null
  114. */
  115. public function route()
  116. {
  117. $this->pool = $this->pool->add(async(function () {
  118. try {
  119. $this->setRoute();
  120. $this->dispatcher = new \Phroute\Phroute\Dispatcher( $this->collector->getData() );
  121. return await($this->dispatcher->dispatch( $this->httpMethod, $this->uri ) );
  122. } catch ( \Exception $e ) {
  123. return $this->badRoute();
  124. }
  125. })->then(function ($output) {
  126. return $output;
  127. })->catch(function (\Exception $exception) {
  128. throw new \Exception("Could not dispatch route!", $exception->getCode());
  129. }));
  130. return await($this->pool);
  131. }
  132.  
  133. private function badRoute() {
  134. //TODO: Finish this later
  135.  
  136. $async_data[] = $this->pool->add(async(function () {
  137. return await("this doesn't work");
  138. }));
  139. return $async_data;
  140. }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement