Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

support php8 attributes, fixes #97 #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/Annotations/Feature.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

namespace Flagception\Bundle\FlagceptionBundle\Annotations;

use \Attribute;
/**
* Class Feature
*
* @author Michel Chowanski <[email protected]>
* @package Flagception\Bundle\FlagceptionBundle\Annotations
* @Annotation
*/

#[Attribute(Attribute::TARGET_ALL)]
class Feature
{
/**
Expand All @@ -17,4 +19,17 @@ class Feature
* @var string
*/
public $name;

public function __construct($name) {
if (is_string($name)) {
$this->name = $name;
} else {
$this->name = $name['value'];
}
}

public function getName()
{
return $this->name;
}
}
23 changes: 23 additions & 0 deletions src/Listener/AnnotationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ public function onKernelController(ControllerEvent $event)
}

$object = new ReflectionClass($controller[0]);

// check for php8 attributes on the controller class
if (method_exists($object, 'getAttributes')) {
foreach ($object->getAttributes(Feature::class) as $attribute) {
$name = $attribute->getArguments()['name'];
if (!$this->manager->isActive($name)) {
throw new NotFoundHttpException(sprintf('Feature %s for class %s is not active.', $name, $object->getName()));
}
}
}


foreach ($this->reader->getClassAnnotations($object) as $annotation) {
if ($annotation instanceof Feature) {
if (!$this->manager->isActive($annotation->name)) {
Expand All @@ -83,6 +95,17 @@ public function onKernelController(ControllerEvent $event)
}

$method = $object->getMethod($controller[1]);

// check for php8 attributes on the method
if (method_exists($method, 'getAttributes')) {
foreach ($method->getAttributes(Feature::class) as $attribute) {
$name = $attribute->getArguments()['name'];
if (!$this->manager->isActive($name)) {
throw new NotFoundHttpException(sprintf('Feature %s for method %s is not active.', $name, $method->getName()));
}
}
}

foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
if ($annotation instanceof Feature) {
if (!$this->manager->isActive($annotation->name)) {
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
services:
flagception.manager.feature_manager:
class: Flagception\Manager\FeatureManager
tags:
- { name: routing.condition_service }
arguments:
- '@flagception.activator.chain_activator'
- '@flagception.decorator.chain_decorator'
public: true

Flagception\Manager\FeatureManagerInterface: '@flagception.manager.feature_manager'

flagception.expression_language:
Expand Down