ConfigSetProcessorFactory.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Console\Command\ConfigSet;
  7. use Magento\Config\Console\Command\ConfigSetCommand;
  8. use Magento\Framework\Exception\ConfigurationMismatchException;
  9. use Magento\Framework\ObjectManagerInterface;
  10. /**
  11. * Creates different implementations of config:set processors of type ConfigSetProcessorInterface.
  12. *
  13. * @see ConfigSetProcessorInterface
  14. * @see ConfigSetCommand
  15. *
  16. * @api
  17. * @since 101.0.0
  18. */
  19. class ConfigSetProcessorFactory
  20. {
  21. /**#@+
  22. * Constants for processors.
  23. *
  24. * default - save configuration
  25. * lock - save and lock configuration
  26. */
  27. const TYPE_DEFAULT = 'default';
  28. /**
  29. * @deprecated
  30. * @see TYPE_LOCK_ENV or TYPE_LOCK_CONFIG
  31. */
  32. const TYPE_LOCK = 'lock';
  33. const TYPE_LOCK_ENV = 'lock-env';
  34. const TYPE_LOCK_CONFIG = 'lock-config';
  35. /**#@-*/
  36. /**#@-*/
  37. private $objectManager;
  38. /**
  39. * List of class names that implement config:set processors
  40. *
  41. * @var array
  42. * @see ConfigSetProcessorInterface
  43. */
  44. private $processors;
  45. /**
  46. * @param ObjectManagerInterface $objectManager
  47. * @param array $processors
  48. */
  49. public function __construct(
  50. ObjectManagerInterface $objectManager,
  51. array $processors = []
  52. ) {
  53. $this->objectManager = $objectManager;
  54. $this->processors = $processors;
  55. }
  56. /**
  57. * Creates an instance of specified processor.
  58. *
  59. * @param string $processorName The name of processor
  60. * @return ConfigSetProcessorInterface New processor instance
  61. * @throws ConfigurationMismatchException If processor type is not exists in processors array
  62. * or declared class has wrong implementation
  63. * @since 101.0.0
  64. */
  65. public function create($processorName)
  66. {
  67. if (!isset($this->processors[$processorName])) {
  68. throw new ConfigurationMismatchException(
  69. __('The class for "%1" type wasn\'t declared. Enter the class and try again.', $processorName)
  70. );
  71. }
  72. $object = $this->objectManager->create($this->processors[$processorName]);
  73. if (!$object instanceof ConfigSetProcessorInterface) {
  74. throw new ConfigurationMismatchException(
  75. __('%1 should implement %2', get_class($object), ConfigSetProcessorInterface::class)
  76. );
  77. }
  78. return $object;
  79. }
  80. }