1
0

path.class.php 1.4 KB

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