vendor/easycorp/easyadmin-bundle/src/Configuration/ConfigManager.php line 104

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Configuration;
  3. use EasyCorp\Bundle\EasyAdminBundle\Exception\UndefinedEntityException;
  4. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  5. use Symfony\Contracts\Cache\CacheInterface;
  6. final class ConfigManager
  7. {
  8.     private const CACHE_KEY 'easyadmin.processed_config';
  9.     /** @var array */
  10.     private $backendConfig;
  11.     private $debug;
  12.     private $propertyAccessor;
  13.     private $cache;
  14.     /** @var array */
  15.     private $originalBackendConfig;
  16.     /** @var ConfigPassInterface[] */
  17.     private $configPasses;
  18.     public function __construct(array $originalBackendConfigbool $debugPropertyAccessorInterface $propertyAccessorCacheInterface $cache)
  19.     {
  20.         $this->originalBackendConfig $originalBackendConfig;
  21.         $this->debug $debug;
  22.         $this->propertyAccessor $propertyAccessor;
  23.         $this->cache $cache;
  24.     }
  25.     /**
  26.      * @param ConfigPassInterface $configPass
  27.      */
  28.     public function addConfigPass(ConfigPassInterface $configPass)
  29.     {
  30.         $this->configPasses[] = $configPass;
  31.     }
  32.     public function getBackendConfig(string $propertyPath null)
  33.     {
  34.         $this->loadBackendConfig();
  35.         if (empty($propertyPath)) {
  36.             return $this->backendConfig;
  37.         }
  38.         // turns 'design.menu' into '[design][menu]', the format required by PropertyAccess
  39.         $propertyPath '['.str_replace('.'']['$propertyPath).']';
  40.         return $this->propertyAccessor->getValue($this->backendConfig$propertyPath);
  41.     }
  42.     public function getEntityConfig(string $entityName): array
  43.     {
  44.         $backendConfig $this->getBackendConfig();
  45.         if (!isset($backendConfig['entities'][$entityName])) {
  46.             throw new UndefinedEntityException(['entity_name' => $entityName]);
  47.         }
  48.         return $backendConfig['entities'][$entityName];
  49.     }
  50.     public function getEntityConfigByClass(string $fqcn): ?array
  51.     {
  52.         $backendConfig $this->getBackendConfig();
  53.         foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
  54.             if ($entityConfig['class'] === $fqcn) {
  55.                 return $entityConfig;
  56.             }
  57.         }
  58.         return null;
  59.     }
  60.     public function getActionConfig(string $entityNamestring $viewstring $action): array
  61.     {
  62.         try {
  63.             $entityConfig $this->getEntityConfig($entityName);
  64.         } catch (\Exception $e) {
  65.             $entityConfig = [];
  66.         }
  67.         return $entityConfig[$view]['actions'][$action] ?? [];
  68.     }
  69.     public function isActionEnabled(string $entityNamestring $viewstring $action): bool
  70.     {
  71.         $entityConfig $this->getEntityConfig($entityName);
  72.         return !\in_array($action$entityConfig['disabled_actions'], true) && \array_key_exists($action$entityConfig[$view]['actions']);
  73.     }
  74.     /**
  75.      * It processes the given backend configuration to generate the fully
  76.      * processed configuration used in the application.
  77.      *
  78.      * @param array $backendConfig
  79.      *
  80.      * @return array
  81.      */
  82.     private function doProcessConfig($backendConfig): array
  83.     {
  84.         foreach ($this->configPasses as $configPass) {
  85.             $backendConfig $configPass->process($backendConfig);
  86.         }
  87.         return $backendConfig;
  88.     }
  89.     private function loadBackendConfig(): array
  90.     {
  91.         if (null !== $this->backendConfig) {
  92.             return $this->backendConfig;
  93.         }
  94.         if (true === $this->debug) {
  95.             return $this->backendConfig $this->doProcessConfig($this->originalBackendConfig);
  96.         }
  97.         return $this->backendConfig $this->cache->get(self::CACHE_KEY, function () {
  98.             return $this->doProcessConfig($this->originalBackendConfig);
  99.         });
  100.     }
  101. }