Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mysql.php
Go to the documentation of this file.
1 <?php
7 
13 
14 // @codingStandardsIgnoreStart
15 
18 // @codingStandardsIgnoreEnd
19 {
23  protected $connectionConfig;
24 
28  private $mysqlFactory;
29 
36  public function __construct(
37  array $config,
38  MysqlFactory $mysqlFactory = null
39  ) {
40  $this->connectionConfig = $this->getValidConfig($config);
41  $this->mysqlFactory = $mysqlFactory ?: ObjectManager::getInstance()->get(MysqlFactory::class);
42  parent::__construct();
43  }
44 
48  public function getConnection(DB\LoggerInterface $logger = null, SelectFactory $selectFactory = null)
49  {
50  $connection = $this->getDbConnectionInstance($logger, $selectFactory);
51 
52  $profiler = $connection->getProfiler();
53  if ($profiler instanceof DB\Profiler) {
54  $profiler->setType($this->connectionConfig['type']);
55  $profiler->setHost($this->connectionConfig['host']);
56  }
57 
58  return $connection;
59  }
60 
68  protected function getDbConnectionInstance(DB\LoggerInterface $logger = null, SelectFactory $selectFactory = null)
69  {
70  return $this->mysqlFactory->create(
71  $this->getDbConnectionClassName(),
72  $this->connectionConfig,
73  $logger,
74  $selectFactory
75  );
76  }
77 
83  protected function getDbConnectionClassName()
84  {
85  return DB\Adapter\Pdo\Mysql::class;
86  }
87 
94  private function getValidConfig(array $config)
95  {
96  $default = ['initStatements' => 'SET NAMES utf8', 'type' => 'pdo_mysql', 'active' => false];
97  foreach ($default as $key => $value) {
98  if (!isset($config[$key])) {
99  $config[$key] = $value;
100  }
101  }
102  $required = ['host'];
103  foreach ($required as $name) {
104  if (!isset($config[$name])) {
105  throw new \InvalidArgumentException("MySQL adapter: Missing required configuration option '$name'");
106  }
107  }
108 
109  if (isset($config['port'])) {
110  throw new \InvalidArgumentException(
111  "Port must be configured within host (like '$config[host]:$config[port]') parameter, not within port"
112  );
113  }
114 
115  $config['active'] = !(
116  $config['active'] === 'false'
117  || $config['active'] === false
118  || $config['active'] === '0'
119  );
120 
121  return $config;
122  }
123 }
getDbConnectionInstance(DB\LoggerInterface $logger=null, SelectFactory $selectFactory=null)
Definition: Mysql.php:68
$config
Definition: fraud_order.php:17
$logger
__construct(array $config, MysqlFactory $mysqlFactory=null)
Definition: Mysql.php:36
$value
Definition: gender.phtml:16
$connection
Definition: bulk.php:13
getConnection(DB\LoggerInterface $logger=null, SelectFactory $selectFactory=null)
Definition: Mysql.php:48
$required
Definition: wrapper.phtml:8
if(!isset($_GET['name'])) $name
Definition: log.php:14