path.class.php 1.1 KB

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