Factory.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Profiler driver factory
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Profiler\Driver;
  9. use Magento\Framework\Profiler\DriverInterface;
  10. class Factory
  11. {
  12. /**
  13. * Default driver type
  14. *
  15. * @var string
  16. */
  17. protected $_defaultDriverType;
  18. /**
  19. * Default driver class prefix
  20. *
  21. * @var string
  22. */
  23. protected $_defaultDriverPrefix;
  24. /**
  25. * Constructor
  26. *
  27. * @param string $defaultDriverPrefix
  28. * @param string $defaultDriverType
  29. */
  30. public function __construct(
  31. $defaultDriverPrefix = 'Magento\Framework\Profiler\Driver\\',
  32. $defaultDriverType = 'standard'
  33. ) {
  34. $this->_defaultDriverPrefix = $defaultDriverPrefix;
  35. $this->_defaultDriverType = $defaultDriverType;
  36. }
  37. /**
  38. * Create instance of profiler driver
  39. *
  40. * @param array $config|null
  41. * @return DriverInterface
  42. * @throws \InvalidArgumentException
  43. */
  44. public function create(array $config = null)
  45. {
  46. $type = isset($config['type']) ? $config['type'] : $this->_defaultDriverType;
  47. if (class_exists($type)) {
  48. $class = $type;
  49. } else {
  50. $class = $this->_defaultDriverPrefix . ucfirst($type);
  51. if (!class_exists($class)) {
  52. throw new \InvalidArgumentException(
  53. sprintf("Cannot create profiler driver, class \"%s\" doesn't exist.", $class)
  54. );
  55. }
  56. }
  57. $driver = new $class($config);
  58. if (!$driver instanceof DriverInterface) {
  59. throw new \InvalidArgumentException(
  60. sprintf(
  61. "Driver class \"%s\" must implement \Magento\Framework\Profiler\DriverInterface.",
  62. get_class($driver)
  63. )
  64. );
  65. }
  66. return $driver;
  67. }
  68. }