PlaceholderFactory.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Placeholder;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\ObjectManagerInterface;
  9. /**
  10. * @api
  11. * @since 100.1.2
  12. */
  13. class PlaceholderFactory
  14. {
  15. /**
  16. * @const string Environment type
  17. */
  18. const TYPE_ENVIRONMENT = 'environment';
  19. /**
  20. * @var ObjectManagerInterface
  21. */
  22. private $objectManager;
  23. /**
  24. * @var array
  25. */
  26. private $types;
  27. /**
  28. * @param ObjectManagerInterface $objectManager
  29. * @param array $types
  30. */
  31. public function __construct(ObjectManagerInterface $objectManager, array $types = [])
  32. {
  33. $this->objectManager = $objectManager;
  34. $this->types = $types;
  35. }
  36. /**
  37. * Create placeholder
  38. *
  39. * @param string $type
  40. * @return PlaceholderInterface
  41. * @throws LocalizedException
  42. * @since 100.1.2
  43. */
  44. public function create($type)
  45. {
  46. if (!isset($this->types[$type])) {
  47. throw new LocalizedException(__('There is no defined type ' . $type));
  48. }
  49. $object = $this->objectManager->create($this->types[$type]);
  50. if (!$object instanceof PlaceholderInterface) {
  51. throw new LocalizedException(__('Object is not instance of ' . PlaceholderInterface::class));
  52. }
  53. return $object;
  54. }
  55. }