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
8 changed files
with
169 additions
and
4 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
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,17 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Used by {@link AttributeCompilerPass} to tag services with `action_alias`. | ||
*/ | ||
#[Attribute(Attribute::TARGET_METHOD|Attribute::IS_REPEATABLE)] | ||
final class Action | ||
{ | ||
public function __construct( | ||
public readonly ?string $action = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace ICanBoogie\Binding\Routing; | ||
|
||
use ICanBoogie\HTTP\RequestMethod; | ||
use olvlvl\ComposerAttributeCollector\Attributes; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
use function class_exists; | ||
use function ICanBoogie\hyphenate; | ||
use function ICanBoogie\pluralize; | ||
use function str_ends_with; | ||
use function str_starts_with; | ||
use function strlen; | ||
use function strrpos; | ||
use function strtolower; | ||
use function substr; | ||
|
||
final class AttributeCompilerPass implements CompilerPassInterface | ||
{ | ||
private const CONTROLLER_SUFFIX = 'Controller'; | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!class_exists(Attributes::class)) { | ||
return; | ||
} | ||
|
||
$this->process_actions($container); | ||
} | ||
|
||
/** | ||
* Configures tag `{ name: action_alias, action: X }` from methods with the attribute {@link Action}. | ||
*/ | ||
private function process_actions(ContainerBuilder $container): void | ||
{ | ||
$target_methods = Attributes::findTargetMethods(Action::class); | ||
|
||
foreach ($target_methods as $method) { | ||
$class = $method->class; | ||
$attribute = $method->attribute; | ||
$action = $attribute->action ?? $this->resolve_action($class, $method->name); | ||
|
||
$definition = $container->findDefinition($class); | ||
$definition->addTag(ActionAliasCompilerPass::TAG, [ ActionAliasCompilerPass::TAG_KEY => $action ]); | ||
} | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
*/ | ||
private function resolve_action(string $class, string $method): string | ||
{ | ||
$name = $this->extract_action_name($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 function extract_action_name(string $method): string | ||
{ | ||
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
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