-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support custom attributes for controller
- Loading branch information
1 parent
ad20dd5
commit 76f8146
Showing
2 changed files
with
41 additions
and
0 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,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); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Illuminate\Routing\Concerns; | ||
|
||
interface ICustomAttribute | ||
{ | ||
// | ||
} |