forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModuleResource.php
135 lines (121 loc) · 4.18 KB
/
ModuleResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
// @codingStandardsIgnoreFile
namespace Magento\Framework\Module;
/**
* Resource Model
*/
class ModuleResource extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb implements \Magento\Framework\Module\ResourceInterface
{
/**
* Database versions
*
* @var array
*/
protected static $schemaVersions = null;
/**
* Resource data versions cache array
*
* @var array
*/
protected static $dataVersions = null;
/**
* Define main table
*
* @return void
*/
protected function _construct()
{
$this->_init('setup_module', 'module');
}
/**
* Fill static versions arrays.
* This routine fetches Db and Data versions of at once to optimize sql requests. However, when upgrading, it's
* possible that 'data' column will be created only after all Db installs are passed. So $neededType contains
* information on main purpose of calling this routine, and even when 'data' column is absent - it won't require
* reissuing new sql just to get 'db' version of module.
*
* @param string $needType Can be 'db' or 'data'
* @return $this
*/
protected function _loadVersion($needType)
{
if ($needType == 'db' && is_null(self::$schemaVersions) || $needType == 'data' && is_null(self::$dataVersions)) {
self::$schemaVersions = [];
// Db version column always exists
self::$dataVersions = null;
// Data version array will be filled only if Data column exist
if ($this->getConnection()->isTableExists($this->getMainTable())) {
$select = $this->getConnection()->select()->from($this->getMainTable(), '*');
$rowset = $this->getConnection()->fetchAll($select);
foreach ($rowset as $row) {
self::$schemaVersions[$row['module']] = $row['schema_version'];
if (array_key_exists('data_version', $row)) {
if (is_null(self::$dataVersions)) {
self::$dataVersions = [];
}
self::$dataVersions[$row['module']] = $row['data_version'];
}
}
}
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getDbVersion($moduleName)
{
if (!$this->getConnection()) {
return false;
}
$this->_loadVersion('db');
return isset(self::$schemaVersions[$moduleName]) ? self::$schemaVersions[$moduleName] : false;
}
/**
* {@inheritdoc}
*/
public function setDbVersion($moduleName, $version)
{
$dbModuleInfo = ['module' => $moduleName, 'schema_version' => $version];
if ($this->getDbVersion($moduleName)) {
self::$schemaVersions[$moduleName] = $version;
return $this->getConnection()->update(
$this->getMainTable(),
$dbModuleInfo,
['module = ?' => $moduleName]
);
} else {
self::$schemaVersions[$moduleName] = $version;
return $this->getConnection()->insert($this->getMainTable(), $dbModuleInfo);
}
}
/**
* {@inheritdoc}
*/
public function getDataVersion($moduleName)
{
if (!$this->getConnection()) {
return false;
}
$this->_loadVersion('data');
return isset(self::$dataVersions[$moduleName]) ? self::$dataVersions[$moduleName] : false;
}
/**
* {@inheritdoc}
*/
public function setDataVersion($moduleName, $version)
{
$data = ['module' => $moduleName, 'data_version' => $version];
if ($this->getDbVersion($moduleName) || $this->getDataVersion($moduleName)) {
self::$dataVersions[$moduleName] = $version;
$this->getConnection()->update($this->getMainTable(), $data, ['module = ?' => $moduleName]);
} else {
self::$dataVersions[$moduleName] = $version;
$this->getConnection()->insert($this->getMainTable(), $data);
}
}
}