Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
336 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing; | ||
|
||
use ICanBoogie\HTTP\RequestMethod; | ||
|
||
use ICanBoogie\Routing\RouteMaker; | ||
use RuntimeException; | ||
|
||
use function ICanBoogie\hyphenate; | ||
use function ICanBoogie\pluralize; | ||
use function in_array; | ||
use function strlen; | ||
use function strrpos; | ||
use function strtolower; | ||
use function substr; | ||
|
||
final class ActionResolver | ||
{ | ||
private const CONTROLLER_SUFFIX = 'Controller'; | ||
private const ACTIONS = [ | ||
|
||
RouteMaker::ACTION_LIST, | ||
RouteMaker::ACTION_NEW, | ||
RouteMaker::ACTION_CREATE, | ||
RouteMaker::ACTION_SHOW, | ||
RouteMaker::ACTION_EDIT, | ||
RouteMaker::ACTION_UPDATE, | ||
RouteMaker::ACTION_DELETE, | ||
|
||
]; | ||
|
||
/** | ||
* @param class-string $class | ||
*/ | ||
public static function resolve_action(string $class, string $method): string | ||
{ | ||
$name = self::extract_action_name($method); | ||
|
||
if (!$name) { | ||
throw new RuntimeException("Unable to resolve action name from $class::$method"); | ||
} | ||
|
||
$unqualified_class = substr($class, strrpos($class, '\\') + 1); | ||
|
||
if (str_ends_with($unqualified_class, self::CONTROLLER_SUFFIX)) { | ||
$unqualified_class = substr($unqualified_class, 0, -strlen(self::CONTROLLER_SUFFIX)); | ||
} | ||
|
||
$base = pluralize(hyphenate($unqualified_class)); | ||
|
||
return "$base:$name"; | ||
} | ||
|
||
private static function extract_action_name(string $method): string | ||
{ | ||
if (in_array($method, self::ACTIONS)) { | ||
return $method; | ||
} | ||
|
||
foreach (RequestMethod::cases() as $case) { | ||
$try = strtolower($case->value) . '_'; | ||
|
||
if (str_starts_with($try, $method)) { | ||
$method = substr($method, strlen($try)); | ||
|
||
break; | ||
} | ||
} | ||
|
||
return $method; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing\Attribute; | ||
|
||
use Attribute; | ||
use ICanBoogie\HTTP\RequestMethod; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
final class Delete extends Route | ||
{ | ||
public function __construct( | ||
string $pattern, | ||
string|null $action = null, | ||
string|null $id = null, | ||
) { | ||
parent::__construct($pattern, $action, RequestMethod::METHOD_DELETE, $id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing\Attribute; | ||
|
||
use Attribute; | ||
use ICanBoogie\HTTP\RequestMethod; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
final class Get extends Route | ||
{ | ||
public function __construct( | ||
string $pattern, | ||
string|null $action = null, | ||
string|null $id = null, | ||
) { | ||
parent::__construct($pattern, $action, RequestMethod::METHOD_GET, $id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing\Attribute; | ||
|
||
use Attribute; | ||
use ICanBoogie\HTTP\RequestMethod; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
final class Post extends Route | ||
{ | ||
public function __construct( | ||
string $pattern, | ||
string|null $action = null, | ||
string|null $id = null, | ||
) { | ||
parent::__construct($pattern, $action, RequestMethod::METHOD_POST, $id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing\Attribute; | ||
|
||
use Attribute; | ||
use ICanBoogie\HTTP\RequestMethod; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
final class Put extends Route | ||
{ | ||
public function __construct( | ||
string $pattern, | ||
string|null $action = null, | ||
string|null $id = null, | ||
) { | ||
parent::__construct($pattern, $action, RequestMethod::METHOD_PUT, $id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing\Attribute; | ||
|
||
use Attribute; | ||
use ICanBoogie\HTTP\RequestMethod; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
class Route | ||
{ | ||
/** | ||
* @param string $pattern | ||
* Pattern of the route. | ||
* @param string|null $action | ||
* Identifier of a qualified action. e.g. 'articles:show'. | ||
* If it is not defined, the action might be resolved from the controller and the method. | ||
* @param RequestMethod|RequestMethod[] $methods | ||
* Request method(s) accepted by the respond. | ||
* @param string|null $id | ||
*/ | ||
public function __construct( | ||
public readonly string $pattern, | ||
public readonly string|null $action = null, | ||
public readonly RequestMethod|array $methods = RequestMethod::METHOD_ANY, | ||
public readonly string|null $id = null, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Test\ICanBoogie\Binding\Routing\Acme; | ||
|
||
use ICanBoogie\Binding\Routing\ActionResponder; | ||
use ICanBoogie\Binding\Routing\Attribute\Delete; | ||
use ICanBoogie\Binding\Routing\Attribute\Get; | ||
use ICanBoogie\Binding\Routing\Attribute\Post; | ||
use ICanBoogie\Binding\Routing\Attribute\Put; | ||
use ICanBoogie\Routing\Controller\ActionTrait; | ||
use ICanBoogie\Routing\ControllerAbstract; | ||
|
||
#[ActionResponder] | ||
final class ImageController extends ControllerAbstract | ||
{ | ||
use ActionTrait; | ||
|
||
#[Get('/images.html')] | ||
public function list(): void | ||
{ | ||
} | ||
|
||
#[Get('/images/:id.html')] | ||
public function show(): void | ||
{ | ||
} | ||
|
||
#[Post('/images')] | ||
public function create(): void | ||
{ | ||
} | ||
|
||
#[Put('/images/:id')] | ||
public function update(): void | ||
{ | ||
} | ||
|
||
#[Delete('/images/:id')] | ||
public function delete(): void | ||
{ | ||
} | ||
} |
Oops, something went wrong.