diff --git a/.coderabbit.yaml b/.coderabbit.yaml new file mode 100644 index 0000000..11bd52f --- /dev/null +++ b/.coderabbit.yaml @@ -0,0 +1,2 @@ +reviews: + request_changes_workflow: true diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index aa287aa..f1c9a98 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -15,13 +15,15 @@ jobs: operating-system: - ubuntu-latest php-version: - - '7.3' - - '7.4' - '8.0' - '8.1' + - '8.2' + - '8.3' + - '8.4' + - '8.5' steps: - name: Checkout - uses: actions/checkout@v1 + uses: actions/checkout@v4 - name: Setup PHP ${{ matrix.php-version }} uses: shivammathur/setup-php@v2 @@ -33,10 +35,10 @@ jobs: - name: Get composer cache directory id: composer-cache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - name: Cache dependencies - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} diff --git a/composer.json b/composer.json index 62f680c..b82e8a8 100644 --- a/composer.json +++ b/composer.json @@ -5,10 +5,9 @@ ], "license": "MIT", "require": { - "php": "^7.3 || ^8.0", + "php": "^8.0", "ray/di": "~2.0", - "laminas/laminas-permissions-acl": "~2.5", - "koriym/attributes": "^1.0" + "laminas/laminas-permissions-acl": "~2.5" }, "require-dev": { "phpunit/phpunit": "^8.5" diff --git a/src/Annotation/RequiresRoles.php b/src/Annotation/RequiresRoles.php index a26abc5..afe65aa 100644 --- a/src/Annotation/RequiresRoles.php +++ b/src/Annotation/RequiresRoles.php @@ -8,20 +8,10 @@ use Attribute; -/** - * @Annotation - * @Target({"CLASS", "METHOD"}) - */ #[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_CLASS)] final class RequiresRoles { - /** - * @var array - */ - public $value; - - public function __construct(array $value) + public function __construct(public array $value) { - $this->value = $value; } } diff --git a/src/RequiredRolesInterceptor.php b/src/RequiredRolesInterceptor.php index 91d8612..af45b69 100644 --- a/src/RequiredRolesInterceptor.php +++ b/src/RequiredRolesInterceptor.php @@ -6,7 +6,6 @@ */ namespace Ray\RoleModule; -use Doctrine\Common\Annotations\Reader; use Ray\Aop\MethodInterceptor; use Ray\Aop\MethodInvocation; use Ray\RoleModule\Annotation\RequiresRoles; @@ -16,18 +15,12 @@ class RequiredRolesInterceptor implements MethodInterceptor { - private $reader; + private AclInterface $acl; - /** - * @var AclInterface - */ - private $acl; - - private $roleProvider; + private RoleProviderInterface $roleProvider; - public function __construct(Reader $reader, AclInterface $acl, RoleProviderInterface $roleProvider) + public function __construct(AclInterface $acl, RoleProviderInterface $roleProvider) { - $this->reader = $reader; $this->acl = $acl; $this->roleProvider = $roleProvider; } @@ -37,12 +30,16 @@ public function __construct(Reader $reader, AclInterface $acl, RoleProviderInter */ public function invoke(MethodInvocation $invocation) { - /** @var $annotation RequiresRoles */ - $annotation = $this->reader->getMethodAnnotation($invocation->getMethod(), RequiresRoles::class); - if (! $annotation) { + $attrs = $invocation->getMethod()->getAttributes(RequiresRoles::class); + if (! $attrs) { $class = new \ReflectionClass($invocation->getThis()); - $annotation = $this->reader->getClassAnnotation($class, RequiresRoles::class); + $attrs = $class->getAttributes(RequiresRoles::class); } + if (! $attrs) { + return $invocation->proceed(); + } + + $annotation = $attrs[0]->newInstance(); $target = get_class($invocation->getThis()); $this->acl->addResource(new GenericResource($target)); foreach ($annotation->value as $role) { diff --git a/src/ZendAclModule.php b/src/ZendAclModule.php index b722195..749c96a 100644 --- a/src/ZendAclModule.php +++ b/src/ZendAclModule.php @@ -6,10 +6,6 @@ */ namespace Ray\RoleModule; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\Common\Annotations\Reader; -use Koriym\Attributes\AttributeReader; -use Koriym\Attributes\DualReader; use Ray\RoleModule\Annotation\RequiresRoles; use Ray\Di\AbstractModule; use Ray\Di\Scope; @@ -42,12 +38,6 @@ public function __construct(AclInterface $acl, $roleProviderClass) */ protected function configure() { - $this->bind(Reader::class)->toConstructor(DualReader::class, [ - 'annotationReader' => 'annotation', - 'attributeReader' => 'attribute', - ]); - $this->bind(Reader::class)->annotatedWith('annotation')->to(AnnotationReader::class); - $this->bind(Reader::class)->annotatedWith('attribute')->to(AttributeReader::class); $this->bind(AclInterface::class)->toInstance($this->acl); $this->bind(RoleProviderInterface::class)->to($this->roleProvider)->in(Scope::SINGLETON); // method diff --git a/tests/Fake/FakeClassResource.php b/tests/Fake/FakeClassResource.php index 740e3cc..196e47c 100644 --- a/tests/Fake/FakeClassResource.php +++ b/tests/Fake/FakeClassResource.php @@ -9,9 +9,6 @@ use Ray\RoleModule\Annotation\RequiresRoles; -/** - * @RequiresRoles({"admin"}) - */ #[RequiresRoles(value: ['admin'])] class FakeClassResource { diff --git a/tests/Fake/FakeResource.php b/tests/Fake/FakeResource.php index def7df0..34c4fb0 100644 --- a/tests/Fake/FakeResource.php +++ b/tests/Fake/FakeResource.php @@ -11,9 +11,6 @@ class FakeResource { - /** - * @RequiresRoles({"admin"}) - */ #[RequiresRoles(value: ['admin'])] public function createUser() { diff --git a/tests/RequiredRoleInterceptorTest.php b/tests/RequiredRoleInterceptorTest.php index 9d2b38c..4a83ff5 100644 --- a/tests/RequiredRoleInterceptorTest.php +++ b/tests/RequiredRoleInterceptorTest.php @@ -2,9 +2,6 @@ namespace Ray\RoleModule; -use Doctrine\Common\Annotations\AnnotationReader; -use Koriym\Attributes\AttributeReader; -use Koriym\Attributes\DualReader; use PHPUnit\Framework\TestCase; use Ray\Aop\ReflectiveMethodInvocation; use Ray\RoleModule\Exception\RequiredRolesException; @@ -18,7 +15,7 @@ private function factory($obj, $method, AclInterface $acl, RoleProviderInterface { $invocation = new ReflectiveMethodInvocation( $obj, $method, $args, [ - new RequiredRolesInterceptor(new DualReader(new AnnotationReader(),new AttributeReader()), $acl, $roleProvider) + new RequiredRolesInterceptor($acl, $roleProvider) ] ); return $invocation; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8ba056f..ee65168 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,11 +1,4 @@ = 8) { - ServiceLocator::setReader(new AttributeReader()); -}