DriverPool.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Filesystem;
  7. /**
  8. * A pool of stream wrappers
  9. */
  10. class DriverPool
  11. {
  12. /**#@+
  13. * Available driver types
  14. */
  15. const FILE = 'file';
  16. const HTTP = 'http';
  17. const HTTPS = 'https';
  18. const ZLIB = 'compress.zlib';
  19. /**#@- */
  20. /**#@- */
  21. protected $types = [
  22. self::FILE => \Magento\Framework\Filesystem\Driver\File::class,
  23. self::HTTP => \Magento\Framework\Filesystem\Driver\Http::class,
  24. self::HTTPS => \Magento\Framework\Filesystem\Driver\Https::class,
  25. self::ZLIB => \Magento\Framework\Filesystem\Driver\Zlib::class,
  26. ];
  27. /**
  28. * The pool
  29. *
  30. * @var DriverInterface[]
  31. */
  32. private $pool = [];
  33. /**
  34. * Obtain extra types in constructor
  35. *
  36. * @param array $extraTypes
  37. * @throws \InvalidArgumentException
  38. */
  39. public function __construct($extraTypes = [])
  40. {
  41. foreach ($extraTypes as $code => $typeOrObject) {
  42. if (is_object($typeOrObject)) {
  43. $type = get_class($typeOrObject);
  44. $object = $typeOrObject;
  45. } else {
  46. $type = $typeOrObject;
  47. $object = false;
  48. }
  49. if (!is_subclass_of($type, \Magento\Framework\Filesystem\DriverInterface::class)) {
  50. throw new \InvalidArgumentException("The specified type '{$type}' does not implement DriverInterface.");
  51. }
  52. $this->types[$code] = $type;
  53. if ($object) {
  54. $this->pool[$code] = $typeOrObject;
  55. }
  56. }
  57. }
  58. /**
  59. * Gets a driver instance by code
  60. *
  61. * @param string $code
  62. * @return DriverInterface
  63. */
  64. public function getDriver($code)
  65. {
  66. if (!isset($this->types[$code])) {
  67. $code = self::FILE;
  68. }
  69. if (!isset($this->pool[$code])) {
  70. $class = $this->types[$code];
  71. $this->pool[$code] = new $class();
  72. }
  73. return $this->pool[$code];
  74. }
  75. }