From 6ad39e3ba64e518bcdba1fc32b6247375be39a8a Mon Sep 17 00:00:00 2001 From: Sam Light Date: Wed, 10 Jun 2026 19:00:32 +0100 Subject: shared code in traits for common features for groups --- src/Concerns/CreatesGroups.php | 28 ++++++++++++++++++ src/Concerns/CreatesRoutes.php | 41 ++++++++++++++++++++++++++ src/Concerns/HasAncestors.php | 37 ++++++++++++++++++++++++ src/Group.php | 65 ++++++++++++++++++++++++++++++++++++++++++ src/GroupDefinition.php | 22 +++++++++++++- src/PathSegment.php | 29 +------------------ src/Router.php | 43 +++++++++------------------- 7 files changed, 206 insertions(+), 59 deletions(-) create mode 100644 src/Concerns/CreatesGroups.php create mode 100644 src/Concerns/CreatesRoutes.php create mode 100644 src/Concerns/HasAncestors.php create mode 100644 src/Group.php (limited to 'src') diff --git a/src/Concerns/CreatesGroups.php b/src/Concerns/CreatesGroups.php new file mode 100644 index 0000000..e6e4588 --- /dev/null +++ b/src/Concerns/CreatesGroups.php @@ -0,0 +1,28 @@ +getRouter(), + parent: $this->getGroup(), + prefix: $value, + ); + } + + public function name(string $value): Group + { + return new Group( + router: $this->getRouter(), + parent: $this->getGroup(), + name: $value, + ); + } +} diff --git a/src/Concerns/CreatesRoutes.php b/src/Concerns/CreatesRoutes.php new file mode 100644 index 0000000..6a8f65b --- /dev/null +++ b/src/Concerns/CreatesRoutes.php @@ -0,0 +1,41 @@ +make(HttpMethod::Get, $path, $handler); + } + + public function post(string $path, callable $handler): RouteDefinition + { + return $this->make(HttpMethod::Post, $path, $handler); + } + + public function put(string $path, callable $handler): RouteDefinition + { + return $this->make(HttpMethod::Put, $path, $handler); + } + + public function patch(string $path, callable $handler): RouteDefinition + { + return $this->make(HttpMethod::Patch, $path, $handler); + } + + public function delete(string $path, callable $handler): RouteDefinition + { + return $this->make(HttpMethod::Delete, $path, $handler); + } + + public function any(string $path, callable $handler): RouteDefinition + { + return $this->make(HttpMethod::Any, $path, $handler); + } +} diff --git a/src/Concerns/HasAncestors.php b/src/Concerns/HasAncestors.php new file mode 100644 index 0000000..248e9c8 --- /dev/null +++ b/src/Concerns/HasAncestors.php @@ -0,0 +1,37 @@ +parent; + while ( + null !== $instance + && $count++ < self::MAX_ANCESTORY_DEPTH + ) { + $results[] = $instance; + $instance = $instance->parent; + } + + return array_reverse($results); + } + + /** @return self[] */ + public function getAncestorsAndSelf(): array + { + $results = $this->getAncestors(); + $results[] = $this; + + return $results; + } +} diff --git a/src/Group.php b/src/Group.php new file mode 100644 index 0000000..4e6c71e --- /dev/null +++ b/src/Group.php @@ -0,0 +1,65 @@ +router; + } + + public function getParent(): ?static + { + return $this->parent; + } + + public function prefix(string $value): static + { + $this->prefix = $value; + + return $this; + } + + public function getPrefix(): ?string + { + return $this->prefix; + } + + public function getPath(): string + { + + } + + public function name(string $value): static + { + $this->name = $value; + + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + + /** @param callable(self): void $cb */ + public function group(callable $cb): void + { + ($cb)(new GroupDefinition( + $this + )); + } +} diff --git a/src/GroupDefinition.php b/src/GroupDefinition.php index 444ba91..41e359d 100644 --- a/src/GroupDefinition.php +++ b/src/GroupDefinition.php @@ -4,10 +4,30 @@ declare(strict_types=1); namespace Lightscale\Router; +use Lightscale\Router\Enums\HttpMethod; + class GroupDefinition { + use Concerns\CreatesGroups; + use Concerns\CreatesRoutes; + public function __construct( - private Router $router, + private Group $group, ) { } + + public function make(HttpMethod $method, string $path, callable $handler): RouteDefinition + { + + } + + private function getGroup(): Group + { + return $this->group; + } + + private function getRouter(): Router + { + return $this->group->getRouter(); + } } diff --git a/src/PathSegment.php b/src/PathSegment.php index 2fc4cbb..01f3fc7 100644 --- a/src/PathSegment.php +++ b/src/PathSegment.php @@ -10,7 +10,7 @@ use Lightscale\Router\Exceptions\MissingParameterException; class PathSegment { - private const MAX_ANCESTORY_DEPTH = 100; + use Concerns\HasAncestors; /** @var array */ protected array $children; @@ -73,33 +73,6 @@ class PathSegment return $this->parent; } - /** @return self[] */ - public function getAncestors(): array - { - $results = []; - - $count = 0; - $instance = $this->parent; - while ( - null !== $instance - && $count++ < self::MAX_ANCESTORY_DEPTH - ) { - $results[] = $instance; - $instance = $instance->parent; - } - - return array_reverse($results); - } - - /** @return self[] */ - public function getAncestorsAndSelf(): array - { - $results = $this->getAncestors(); - $results[] = $this; - - return $results; - } - public function addChild(self $segment): void { $segment->setParent($this); diff --git a/src/Router.php b/src/Router.php index b219651..d6b255a 100644 --- a/src/Router.php +++ b/src/Router.php @@ -12,6 +12,9 @@ use Psr\Http\Message\ResponseInterface; class Router { + use Concerns\CreatesRoutes; + use Concerns\CreatesGroups; + private PathSegment $root; private Strategy $strategy; @@ -24,6 +27,16 @@ class Router $this->strategy = new BasicStrategy(); } + private function getRouter(): self + { + return $this; + } + + private function getGroup(): null + { + return null; + } + public function getStrategy(): Strategy { return $this->strategy; @@ -136,36 +149,6 @@ class Router return new RouteDefinition($this, $route); } - public function get(string $path, callable $handler): RouteDefinition - { - return $this->make(HttpMethod::Get, $path, $handler); - } - - public function post(string $path, callable $handler): RouteDefinition - { - return $this->make(HttpMethod::Post, $path, $handler); - } - - public function put(string $path, callable $handler): RouteDefinition - { - return $this->make(HttpMethod::Put, $path, $handler); - } - - public function patch(string $path, callable $handler): RouteDefinition - { - return $this->make(HttpMethod::Patch, $path, $handler); - } - - public function delete(string $path, callable $handler): RouteDefinition - { - return $this->make(HttpMethod::Delete, $path, $handler); - } - - public function any(string $path, callable $handler): RouteDefinition - { - return $this->make(HttpMethod::Any, $path, $handler); - } - public function addNamedRoute(string $name, Route $route): void { $this->namedRoutes[$name] = $route; -- cgit v1.2.3