<?php
namespace Roothirsch\CoreBundle\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use Flagception\Manager\FeatureManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Yaml\Yaml;
final class FeatureSubscriber implements EventSubscriberInterface
{
/**
* @var FeatureManagerInterface
*/
private $featureManager;
/**
* @var string
*/
private $configDir;
public function __construct(FeatureManagerInterface $featureManager, $configDir)
{
$this->featureManager = $featureManager;
$this->configDir = $configDir;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['load', EventPriorities::PRE_SERIALIZE],
];
}
public function load(\Symfony\Component\HttpKernel\Event\ViewEvent $event)
{
if ($event->getRequest()->getPathInfo() == '/api/users/current') {
$event->getControllerResult()->setFeatures($this->getFeatures());
}
}
private function getFeatures()
{
$config = Yaml::parse(file_get_contents($this->configDir . 'packages/flagception.yaml'));
$features = [];
foreach (array_keys($config['flagception']['features']) as $featureName) {
$features[$featureName] = $this->featureManager->isActive($featureName);
}
return $features;
}
}