path.class.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. 'dypath'=>$this->path
  36. ];
  37. }
  38. public function __toString(){
  39. return "[Path {$this->path}]";
  40. }
  41. public function handle(callable $fn){
  42. array_push($this->handles, $fn);
  43. return $this;
  44. }
  45. public function matches(string $path){
  46. return $this->path->matches($path);
  47. }
  48. public function args(string $path){
  49. return $this->path->args($path);
  50. }
  51. }
  52. ?>