MysqlFactory.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\DB\Adapter\Pdo;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\DB\LoggerInterface;
  9. use Magento\Framework\DB\SelectFactory;
  10. use Magento\Framework\ObjectManagerInterface;
  11. /**
  12. * Factory for Mysql adapter
  13. */
  14. class MysqlFactory
  15. {
  16. /**
  17. * @var ObjectManagerInterface
  18. */
  19. private $objectManager;
  20. /**
  21. * Constructor
  22. *
  23. * @param ObjectManagerInterface $objectManager
  24. */
  25. public function __construct(
  26. ObjectManagerInterface $objectManager
  27. ) {
  28. $this->objectManager = $objectManager;
  29. }
  30. /**
  31. * Create instance of Mysql adapter
  32. *
  33. * @param string $className
  34. * @param array $config
  35. * @param LoggerInterface|null $logger
  36. * @param SelectFactory|null $selectFactory
  37. * @return Mysql
  38. * @throws \InvalidArgumentException
  39. */
  40. public function create(
  41. $className,
  42. array $config,
  43. LoggerInterface $logger = null,
  44. SelectFactory $selectFactory = null
  45. ) {
  46. if (!in_array(Mysql::class, class_parents($className, true) + [$className => $className])) {
  47. throw new \InvalidArgumentException('Invalid class, ' . $className . ' must extend ' . Mysql::class . '.');
  48. }
  49. $arguments = [
  50. 'config' => $config
  51. ];
  52. if ($logger) {
  53. $arguments['logger'] = $logger;
  54. }
  55. if ($selectFactory) {
  56. $arguments['selectFactory'] = $selectFactory;
  57. }
  58. return $this->objectManager->create(
  59. $className,
  60. $arguments
  61. );
  62. }
  63. }