vendor/pimcore/pimcore/lib/Extension/Bundle/Config/StateConfig.php line 95

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Pimcore
  5. *
  6. * This source file is available under two different licenses:
  7. * - GNU General Public License version 3 (GPLv3)
  8. * - Pimcore Commercial License (PCL)
  9. * Full copyright and license information is available in
  10. * LICENSE.md which is distributed with this source code.
  11. *
  12. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13. * @license http://www.pimcore.org/license GPLv3 and PCL
  14. */
  15. namespace Pimcore\Extension\Bundle\Config;
  16. use Pimcore\Config as PimcoreConfig;
  17. use Pimcore\Extension\Config;
  18. use Symfony\Component\OptionsResolver\Options;
  19. use Symfony\Component\OptionsResolver\OptionsResolver;
  20. /**
  21. * @internal
  22. *
  23. * @deprecated
  24. */
  25. final class StateConfig
  26. {
  27. /**
  28. * @var OptionsResolver
  29. */
  30. private static $optionsResolver;
  31. /**
  32. * @var array
  33. */
  34. private static $optionDefaults = [
  35. 'enabled' => false,
  36. 'priority' => 10,
  37. 'environments' => [],
  38. ];
  39. /**
  40. * @var Config
  41. */
  42. private $config;
  43. /**
  44. * @param Config $config
  45. */
  46. public function __construct(Config $config)
  47. {
  48. $this->config = $config;
  49. }
  50. /**
  51. * Lists enabled bundles from config
  52. *
  53. * @return array
  54. */
  55. public function getEnabledBundles(): array
  56. {
  57. $result = [];
  58. $bundles = $this->getBundlesFromConfig();
  59. foreach ($bundles as $bundleName => $options) {
  60. if ($options['enabled']) {
  61. $result[$bundleName] = $options;
  62. }
  63. }
  64. return $result;
  65. }
  66. /**
  67. * Lists enabled bundle names from config
  68. *
  69. * @return array
  70. */
  71. public function getEnabledBundleNames(): array
  72. {
  73. return array_keys($this->getEnabledBundles());
  74. }
  75. /**
  76. * Loads bundles which are defined in configuration
  77. *
  78. * @return array
  79. */
  80. private function getBundlesFromConfig(): array
  81. {
  82. $config = $this->config->loadConfig();
  83. if (!isset($config->bundle)) {
  84. return [];
  85. }
  86. $bundles = $config->bundle->toArray();
  87. $result = [];
  88. foreach ($bundles as $bundleName => $options) {
  89. $result[$bundleName] = $this->normalizeOptions($options);
  90. }
  91. return $result;
  92. }
  93. /**
  94. * Returns the normalized bundle state from the extension manager config
  95. *
  96. * @param string $bundle
  97. *
  98. * @return array
  99. */
  100. public function getState(string $bundle): array
  101. {
  102. $bundles = $this->getBundlesFromConfig();
  103. if (isset($bundles[$bundle])) {
  104. return $bundles[$bundle];
  105. }
  106. return $this->normalizeOptions([]);
  107. }
  108. /**
  109. * Sets the normalized bundle state on the extension manager config
  110. *
  111. * @param string $bundle
  112. * @param array $options
  113. */
  114. public function setState(string $bundle, array $options)
  115. {
  116. $config = $this->config->loadConfig();
  117. $this->updateBundleState($config, $bundle, $options);
  118. $this->config->saveConfig($config);
  119. }
  120. /**
  121. * Batch update bundle states
  122. *
  123. * @param array $states
  124. */
  125. public function setStates(array $states)
  126. {
  127. $config = $this->config->loadConfig();
  128. foreach ($states as $bundle => $options) {
  129. $this->updateBundleState($config, $bundle, $options);
  130. }
  131. $this->config->saveConfig($config);
  132. }
  133. private function updateBundleState(PimcoreConfig\Config $config, string $bundle, array $options)
  134. {
  135. if (!isset($config->bundle)) {
  136. $config->bundle = new PimcoreConfig\Config([], true);
  137. }
  138. $state = [];
  139. if (isset($config->bundle->$bundle)) {
  140. $currentState = $config->bundle->$bundle;
  141. if ($currentState instanceof PimcoreConfig\Config) {
  142. $currentState = $currentState->toArray();
  143. }
  144. $state = $this->normalizeOptions($currentState);
  145. }
  146. $state = array_merge($state, $options);
  147. $state = $this->prepareWriteOptions($state);
  148. $config->bundle->$bundle = $state;
  149. }
  150. /**
  151. * Prepares options for writing. If all options besides enabled are the same as the default
  152. * value, just the state will be written as bool,
  153. *
  154. * @param array $options
  155. *
  156. * @return array|bool
  157. */
  158. private function prepareWriteOptions(array $options)
  159. {
  160. $options = $this->normalizeOptions($options);
  161. $isDefault = true;
  162. foreach (array_keys(self::$optionDefaults) as $option) {
  163. if ('enabled' === $option) {
  164. continue;
  165. }
  166. if ($options[$option] !== static::$optionDefaults[$option]) {
  167. $isDefault = false;
  168. break;
  169. }
  170. }
  171. if ($isDefault) {
  172. return $options['enabled'];
  173. }
  174. return $options;
  175. }
  176. /**
  177. * Normalizes options array as expected in extension manager config
  178. *
  179. * @param array|bool $options
  180. *
  181. * @return array
  182. */
  183. public function normalizeOptions($options): array
  184. {
  185. if (is_bool($options)) {
  186. $options = ['enabled' => $options];
  187. } elseif (!is_array($options)) {
  188. throw new \InvalidArgumentException(sprintf(
  189. 'Expected options as bool or as array, but got %s',
  190. get_debug_type($options)
  191. ));
  192. }
  193. $resolver = self::getOptionsResolver();
  194. $options = $resolver->resolve($options);
  195. return $options;
  196. }
  197. private static function getOptionsResolver(): OptionsResolver
  198. {
  199. if (null !== self::$optionsResolver) {
  200. return self::$optionsResolver;
  201. }
  202. $resolver = new OptionsResolver();
  203. $resolver->setDefaults(self::$optionDefaults);
  204. $resolver->setRequired(array_keys(self::$optionDefaults));
  205. $resolver->setAllowedTypes('enabled', 'bool');
  206. $resolver->setAllowedTypes('priority', 'int');
  207. $resolver->setAllowedTypes('environments', 'array');
  208. $resolver->setNormalizer('environments', function (Options $options, $value) {
  209. // normalize to string and trim
  210. $value = array_map(function ($item) {
  211. $item = (string)$item;
  212. $item = trim($item);
  213. return $item;
  214. }, $value);
  215. // remove empty values
  216. $value = array_filter($value, function ($item) {
  217. return !empty($item);
  218. });
  219. return $value;
  220. });
  221. self::$optionsResolver = $resolver;
  222. return self::$optionsResolver;
  223. }
  224. }