Factory.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * System Configuration Mapper Factory
  8. */
  9. namespace Magento\Config\Model\Config\Structure\Mapper;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Factory
  15. {
  16. const MAPPER_SORTING = 'sorting';
  17. const MAPPER_PATH = 'path';
  18. const MAPPER_IGNORE = 'ignore';
  19. const MAPPER_DEPENDENCIES = 'dependencies';
  20. const MAPPER_ATTRIBUTE_INHERITANCE = 'attribute_inheritance';
  21. const MAPPER_EXTENDS = 'extends';
  22. /**
  23. * @var \Magento\Framework\ObjectManagerInterface
  24. */
  25. protected $_objectManager;
  26. /**
  27. * @var array
  28. */
  29. protected $_typeMap = [
  30. self::MAPPER_SORTING => \Magento\Config\Model\Config\Structure\Mapper\Sorting::class,
  31. self::MAPPER_PATH => \Magento\Config\Model\Config\Structure\Mapper\Path::class,
  32. self::MAPPER_IGNORE => \Magento\Config\Model\Config\Structure\Mapper\Ignore::class,
  33. self::MAPPER_DEPENDENCIES => \Magento\Config\Model\Config\Structure\Mapper\Dependencies::class,
  34. self::MAPPER_ATTRIBUTE_INHERITANCE =>
  35. \Magento\Config\Model\Config\Structure\Mapper\Attribute\Inheritance::class,
  36. self::MAPPER_EXTENDS => \Magento\Config\Model\Config\Structure\Mapper\ExtendsMapper::class,
  37. ];
  38. /**
  39. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  40. */
  41. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManager)
  42. {
  43. $this->_objectManager = $objectManager;
  44. }
  45. /**
  46. * Get mapper instance
  47. *
  48. * @param string $type
  49. * @return \Magento\Config\Model\Config\Structure\MapperInterface
  50. * @throws \Exception
  51. */
  52. public function create($type)
  53. {
  54. $className = $this->_getMapperClassNameByType($type);
  55. /** @var \Magento\Config\Model\Config\Structure\MapperInterface $mapperInstance */
  56. $mapperInstance = $this->_objectManager->create($className);
  57. if (false == $mapperInstance instanceof \Magento\Config\Model\Config\Structure\MapperInterface) {
  58. throw new \Exception(
  59. 'Mapper object is not instance on \Magento\Config\Model\Config\Structure\MapperInterface'
  60. );
  61. }
  62. return $mapperInstance;
  63. }
  64. /**
  65. * Get mapper class name by type
  66. *
  67. * @param string $type
  68. * @return string
  69. * @throws \InvalidArgumentException
  70. */
  71. protected function _getMapperClassNameByType($type)
  72. {
  73. if (false == isset($this->_typeMap[$type])) {
  74. throw new \InvalidArgumentException('Invalid mapper type: ' . $type);
  75. }
  76. return $this->_typeMap[$type];
  77. }
  78. }