path.class.php 926 B

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