Skip to content

Commit

Permalink
feat: Support custom attributes for controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jojomak350 committed Jan 17, 2025
1 parent ad20dd5 commit 76f8146
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Illuminate/Routing/Concerns/HasCustomAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Illuminate\Routing\Concerns;

use Illuminate\Support\Collection;
use ReflectionAttribute;
use ReflectionMethod;

trait HasCustomAttributes
{
public function callAction($method, $parameters)
{
$reflection = new ReflectionMethod($this, $method);

$attributes = $this->gteCustomAttributes($reflection);

$attributes->each(function (ReflectionAttribute $attribute) {
$instance = $attribute->newInstance();

if(method_exists($instance, 'handle')) {
$attribute->newInstance()->handle(...$attribute->getArguments());
}
});

return parent::callAction($method, $parameters);
}

private function gteCustomAttributes(ReflectionMethod $reflection): Collection
{
return collect($reflection->getAttributes())
->filter(fn(ReflectionAttribute $attr) => $attr->newInstance() instanceof ICustomAttribute);
}
}
8 changes: 8 additions & 0 deletions src/Illuminate/Routing/Concerns/ICustomAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Illuminate\Routing\Concerns;

interface ICustomAttribute
{
//
}

0 comments on commit 76f8146

Please sign in to comment.