path.class.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Juju\App;
  3. require_once('dypath.class.php');
  4. $dir = dirname(__DIR__).'/Http/';
  5. require_once(realpath("{$dir}/request.class.php"));
  6. require_once(realpath("{$dir}/response.class.php"));
  7. require_once('arguments.class.php');
  8. use \Juju\Http\{Request, Response};
  9. class Path implements \JsonSerializable{
  10. private $handles = [];
  11. public $path;
  12. public function __construct(string $path){
  13. $this->path = new DyPath($path);
  14. }
  15. public function __invoke(Request $req, Response $res, Arguments $args){
  16. $err = null;
  17. $ret = true;
  18. foreach($this->handles as $k => $fn){
  19. try{
  20. if($fn($req, $res, $args, $err) === false){
  21. $ret = false;
  22. }
  23. }catch(\Exception $e){
  24. $err = $e;
  25. }
  26. }
  27. return $ret;
  28. }
  29. public function __clone(){
  30. // No cloning for now
  31. }
  32. public function __destruct(){
  33. // Nothing to do here
  34. }
  35. public function jsonSerialize(){
  36. return [
  37. 'dypath'=>$this->path
  38. ];
  39. }
  40. public function __toString(){
  41. return "[Path {$this->path}]";
  42. }
  43. public function handle(callable $fn){
  44. array_push($this->handles, $fn);
  45. return $this;
  46. }
  47. public function matches(string $path){
  48. return $this->path->matches($path);
  49. }
  50. public function args(string $path){
  51. return $this->path->args($path);
  52. }
  53. }
  54. ?>