path.class.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. foreach($this->handles as $k => $fn){
  18. try{
  19. $fn($req, $res, $args, $err);
  20. }catch(\Exception $e){
  21. $err = $e;
  22. }
  23. }
  24. }
  25. public function __clone(){
  26. // No cloning for now
  27. }
  28. public function __destruct(){
  29. // Nothing to do here
  30. }
  31. public function jsonSerialize(){
  32. return [
  33. 'dypath'=>$this->path
  34. ];
  35. }
  36. public function __toString(){
  37. return "[Path {$this->path}]";
  38. }
  39. public function handle(callable $fn){
  40. array_push($this->handles,$fn);
  41. return $this;
  42. }
  43. public function matches(string $path){
  44. return $this->path->matches($path);
  45. }
  46. public function args(string $path){
  47. return $this->path->args($path);
  48. }
  49. }
  50. ?>