Skip to content

Commit

Permalink
Fixed route's collection prototyping support
Browse files Browse the repository at this point in the history
  • Loading branch information
divineniiquaye committed May 24, 2022
1 parent 95d8177 commit af60fe7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public static function to(string $pattern, $methods = self::DEFAULT_METHODS, $ha
/**
* Sets a custom key and value into route
*
* @param mixed $value
*
* @return $this
*/
public function setData(string $key, $value)
Expand Down
11 changes: 2 additions & 9 deletions src/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,19 +356,12 @@ protected function injectRoute(Route $route): Route
foreach ($defaultsStack as $routeMethod => $arguments) {
if ('prefix' === $routeMethod) {
$route->prefix(\implode('', \array_merge(...$arguments)));

continue;
}

if (\count($arguments) > 1) {
foreach ($arguments as $argument) {
$route->{$routeMethod}(...$argument);
}

continue;
foreach ($arguments as $parameters) {
\call_user_func_array([$route, $routeMethod], $parameters);
}

$route->{$routeMethod}(...$arguments[0]);
}
}

Expand Down
60 changes: 37 additions & 23 deletions src/Traits/PrototypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ trait PrototypeTrait
private array $prototypes = [];

/**
* Allows a proxied method call to route's.
* Allows a proxied method call to route(s).
*
* @throws \RuntimeException if locked
* @throws \UnexpectedValueException if bind method is called used for route
*
* @return $this
*/
Expand All @@ -41,14 +42,12 @@ public function prototype(array $routeData)
foreach ($routeData as $routeMethod => $arguments) {
$arguments = \is_array($arguments) ? $arguments : [$arguments];

if (null !== $this->route) {
$this->route->{$routeMethod}(...$arguments);

continue;
if (null === $this->route && 'bind' === $routeMethod) {
throw new \UnexpectedValueException(\sprintf('Binding the name "%s" is only supported on routes.', $arguments[0]));
}

if ('bind' === $routeMethod) {
throw new \UnexpectedValueException(\sprintf('Binding the name "%s" is only supported on routes.', $arguments[0]));
if ('s' === $routeMethod[-1]) {
$arguments = [$arguments];
}

$this->doPrototype($routeMethod, $arguments);
Expand Down Expand Up @@ -83,11 +82,12 @@ public function end()
}

/**
* Prototype a name to a route, which is required for generating
* url from named routes.
* Prototype a unique name to a route.
*
* @see Route::bind() for more information
*
* @throws \UnderflowException if route doesn't exist
*
* @return $this
*/
public function bind(string $routeName)
Expand All @@ -102,10 +102,12 @@ public function bind(string $routeName)
}

/**
* Prototype the route's handler executed when matched.
* Prototype a handler to a route.
*
* @param mixed $to PHP class, object or callable that returns the response when matched
*
* @throws \UnderflowException if route doesn't exist
*
* @return $this
*/
public function run($to)
Expand All @@ -120,7 +122,7 @@ public function run($to)
}

/**
* Prototype the optional default value which maybe required by routes.
* Prototype optional default values to route(s)
*
* @param mixed $default The default value
*
Expand All @@ -134,7 +136,7 @@ public function default(string $variable, $default)
}

/**
* Prototype the optional default values which maybe required by routes.
* Prototype optional default values to route(s)
*
* @param array<string,mixed> $values
*
Expand Down Expand Up @@ -176,7 +178,7 @@ public function asserts(array $regexps)
}

/**
* Prototype the arguments supplied to route handler's constructor/factory.
* Prototype the parameter supplied to route handler's constructor/factory.
*
* @param mixed $value The parameter value
*
Expand All @@ -190,7 +192,7 @@ public function argument(string $parameter, $value)
}

/**
* Prototype the arguments supplied to route handler's constructor/factory.
* Prototype the parameters supplied to route handler's constructor/factory.
*
* @param array<int|string> $parameters The route handler parameters
*
Expand Down Expand Up @@ -224,35 +226,35 @@ public function namespace(string $namespace)
*/
public function method(string ...$methods)
{
return $this->doPrototype(__FUNCTION__, \func_get_args());
return $this->doPrototype(__FUNCTION__, $methods);
}

/**
* Prototype HTTP host scheme(s) to all routes.
* Prototype HTTP host scheme(s) to route(s)
*
* @see Route::scheme() for more information
*
* @return $this
*/
public function scheme(string ...$schemes)
{
return $this->doPrototype(__FUNCTION__, \func_get_args());
return $this->doPrototype(__FUNCTION__, $schemes);
}

/**
* Prototype HTTP host scheme(s) to all routes.
* Prototype HTTP host name(s) to route(s)
*
* @see Route::scheme() for more information
* @see Route::domain() for more information
*
* @return $this
*/
public function domain(string ...$hosts)
{
return $this->doPrototype(__FUNCTION__, \func_get_args());
return $this->doPrototype(__FUNCTION__, $hosts);
}

/**
* Prototype a prefix prepended to route's path.
* Prototype prefix path to route(s)
*
* @see Route::prefix() for more information
*
Expand All @@ -264,15 +266,27 @@ public function prefix(string $path)
}

/**
* Prototype named middleware group(s) to all routes.
* Prototype a set of named grouped middleware(s) to route(s)
*
* @see Route::piped() for more information
*
* @return $this
*/
public function piped(string ...$to)
{
return $this->doPrototype(__FUNCTION__, \func_get_args());
return $this->doPrototype(__FUNCTION__, $to);
}

/**
* Prototype a custom key and value to route(s)
*
* @param mixed $value
*
* @return $this
*/
public function set(string $key, $value)
{
return $this->doPrototype('setData', \func_get_args());
}

/**
Expand Down

0 comments on commit af60fe7

Please sign in to comment.