AdapterFactory.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Image;
  7. class AdapterFactory
  8. {
  9. /**
  10. * @var Adapter\ConfigInterface
  11. */
  12. protected $config;
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface
  15. */
  16. protected $objectManager;
  17. /**
  18. * @var array
  19. */
  20. protected $adapterMap;
  21. /**
  22. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  23. * @param Adapter\ConfigInterface $config
  24. * @param array $adapterMap
  25. */
  26. public function __construct(
  27. \Magento\Framework\ObjectManagerInterface $objectManager,
  28. \Magento\Framework\Image\Adapter\ConfigInterface $config,
  29. array $adapterMap = []
  30. ) {
  31. $this->objectManager = $objectManager;
  32. $this->config = $config;
  33. $this->adapterMap = $adapterMap;
  34. }
  35. /**
  36. * Return specified image adapter
  37. *
  38. * @param string $adapterAlias
  39. * @return \Magento\Framework\Image\Adapter\AdapterInterface
  40. * @throws \InvalidArgumentException
  41. */
  42. public function create($adapterAlias = null)
  43. {
  44. $this->adapterMap = array_merge($this->config->getAdapters(), $this->adapterMap);
  45. $adapterAlias = !empty($adapterAlias) ? $adapterAlias : $this->config->getAdapterAlias();
  46. if (empty($adapterAlias)) {
  47. throw new \InvalidArgumentException('Image adapter is not selected.');
  48. }
  49. if (empty($this->adapterMap[$adapterAlias]['class'])) {
  50. throw new \InvalidArgumentException("Image adapter for '{$adapterAlias}' is not setup.");
  51. }
  52. $imageAdapter = $this->objectManager->create($this->adapterMap[$adapterAlias]['class']);
  53. if (!$imageAdapter instanceof Adapter\AdapterInterface) {
  54. throw new \InvalidArgumentException(
  55. $this->adapterMap[$adapterAlias]['class'] .
  56. ' is not instance of \Magento\Framework\Image\Adapter\AdapterInterface'
  57. );
  58. }
  59. $imageAdapter->checkDependencies();
  60. return $imageAdapter;
  61. }
  62. }