Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Setup.php
Go to the documentation of this file.
1 <?php
9 
13 
14 class Setup implements SetupInterface
15 {
21  private $connection = null;
22 
28  private $tables = [];
29 
35  private $resourceModel;
36 
42  private $connectionName;
43 
50  public function __construct(
53  ) {
54  $this->resourceModel = $resource;
55  $this->connectionName = $connectionName;
56  }
57 
64  public function getConnection($connectionName = null)
65  {
66  if ($connectionName !== null) {
67  try {
68  return $this->resourceModel->getConnectionByName($connectionName);
69  } catch (\DomainException $exception) {
70  //Fallback to default connection
71  }
72  }
73  return $this->getDefaultConnection();
74  }
75 
81  private function getDefaultConnection()
82  {
83  if (null === $this->connection) {
84  $this->connection = $this->resourceModel->getConnection($this->connectionName);
85  }
86  return $this->connection;
87  }
88 
96  public function setTable($tableName, $realTableName)
97  {
98  $this->tables[$tableName] = $realTableName;
99  return $this;
100  }
101 
109  {
110  return $this->resourceModel->getTablePlaceholder($tableName);
111  }
112 
120  public function getTable($tableName, $connectionName = ResourceConnection::DEFAULT_CONNECTION)
121  {
122  $cacheKey = $this->_getTableCacheName($tableName);
123  if (!isset($this->tables[$cacheKey])) {
124  $this->tables[$cacheKey] = $this->resourceModel->getTableName($tableName, $connectionName);
125  }
126  return $this->tables[$cacheKey];
127  }
128 
135  private function _getTableCacheName($tableName)
136  {
137  if (is_array($tableName)) {
138  return join('_', $tableName);
139  }
140  return $tableName;
141  }
142 
150  public function tableExists($table, $connectionName = ResourceConnection::DEFAULT_CONNECTION)
151  {
152  $table = $this->getTable($table, $connectionName);
153  return $this->getConnection($connectionName)->isTableExists($table);
154  }
155 
162  public function run($sql)
163  {
164  $this->getConnection()->query($sql);
165  return $this;
166  }
167 
173  public function startSetup()
174  {
175  $this->getConnection()->startSetup();
176  return $this;
177  }
178 
184  public function endSetup()
185  {
186  $this->getConnection()->endSetup();
187  return $this;
188  }
189 }
$tableName
Definition: trigger.php:13
__construct(\Magento\Framework\App\ResourceConnection $resource, $connectionName=ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION)
Definition: Setup.php:50
$resource
Definition: bulk.php:12
tableExists($table, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
Definition: Setup.php:150
getConnection($connectionName=null)
Definition: Setup.php:64
getTablePlaceholder($tableName)
Definition: Setup.php:108
setTable($tableName, $realTableName)
Definition: Setup.php:96
$table
Definition: trigger.php:14
getTable($tableName, $connectionName=ResourceConnection::DEFAULT_CONNECTION)
Definition: Setup.php:120