1
0

path.class.php 1.3 KB

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