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 +++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 src/Concerns/CreatesGroups.php create mode 100644 src/Concerns/CreatesRoutes.php create mode 100644 src/Concerns/HasAncestors.php (limited to 'src/Concerns') 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; + } +} -- cgit v1.2.3