|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magefix\Fixtures; |
|
4
|
|
|
|
|
5
|
|
|
use Mage; |
|
6
|
|
|
use Mage_Core_Model_Abstract; |
|
7
|
|
|
use Magefix\Fixture\Factory\Builder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class RegistryIterator |
|
11
|
|
|
* |
|
12
|
|
|
* @package Magefix\Fixtures |
|
13
|
|
|
* @author Carlo Tasca <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class RegistryIterator extends \ArrayObject |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param $hook |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
public function iterateByHook($hook) |
|
22
|
|
|
{ |
|
23
|
|
|
$registryIteratorIterator = $this->getIterator(); |
|
24
|
|
|
|
|
25
|
|
|
while ($registryIteratorIterator->valid()) { |
|
26
|
|
|
$key = $registryIteratorIterator->key(); |
|
27
|
|
|
$entry = $registryIteratorIterator->current(); |
|
28
|
|
|
$entryMatch = $this->isEntryMatch($hook, $key); |
|
29
|
|
|
|
|
30
|
|
|
$this->_changeRegistryEntry($entryMatch, $entry, $key); |
|
31
|
|
|
|
|
32
|
|
|
$registryIteratorIterator->next(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $match |
|
38
|
|
|
* |
|
39
|
|
|
* @return \Mage_Catalog_Model_Category|\Mage_Catalog_Model_Product|\Mage_Customer_Model_Customer|\Mage_Sales_Model_Order|null |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getMageModelForMatch($match) |
|
43
|
|
|
{ |
|
44
|
|
|
$model = null; |
|
45
|
|
|
|
|
46
|
|
|
if ($match == Builder::SIMPLE_PRODUCT_FIXTURE_TYPE || $match == Builder::CONFIGURABLE_PRODUCT_FIXTURE_TYPE |
|
47
|
|
|
|| $match == Builder::BUNDLE_PRODUCT_FIXTURE_TYPE |
|
48
|
|
|
) { |
|
49
|
|
|
$model = Mage::getModel('catalog/product'); |
|
50
|
|
|
} elseif ($match == Builder::CATEGORY_FIXTURE_TYPE) { |
|
51
|
|
|
$model = Mage::getModel('catalog/category'); |
|
52
|
|
|
} elseif ($match == Builder::CUSTOMER_FIXTURE_TYPE) { |
|
53
|
|
|
$model = Mage::getModel('customer/customer'); |
|
54
|
|
|
} elseif ($match == Builder::SALES_ORDER_FIXTURE_TYPE) { |
|
55
|
|
|
$model = Mage::getModel('sales/order'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $model; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $hook |
|
63
|
|
|
* @param $key |
|
64
|
|
|
* |
|
65
|
|
|
* @return string[] |
|
66
|
|
|
* |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isEntryMatch($hook, $key) |
|
69
|
|
|
{ |
|
70
|
|
|
$matches = []; |
|
71
|
|
|
$matchOr = Builder::SIMPLE_PRODUCT_FIXTURE_TYPE . '|' . Builder::BUNDLE_PRODUCT_FIXTURE_TYPE |
|
72
|
|
|
. '|' . Builder::CONFIGURABLE_PRODUCT_FIXTURE_TYPE . '|' . Builder::CUSTOMER_FIXTURE_TYPE |
|
73
|
|
|
. '|' . Builder::CATEGORY_FIXTURE_TYPE . '|' . Builder::SALES_ORDER_FIXTURE_TYPE; |
|
74
|
|
|
|
|
75
|
|
|
preg_match("/^({$matchOr})_{$hook}/", $key, $matches); |
|
76
|
|
|
|
|
77
|
|
|
return $matches; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $model |
|
82
|
|
|
* @param string $fixtureType |
|
83
|
|
|
* @param $entry |
|
84
|
|
|
* @param $key |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function _echoRegistryChangeMessage($model, $fixtureType, $entry, $key) |
|
87
|
|
|
{ |
|
88
|
|
|
echo $this->_deleteAndUnregisterFixture( |
|
89
|
|
|
$model, $fixtureType, $entry, $key |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param Mage_Core_Model_Abstract $model |
|
95
|
|
|
* @param string $fixtureType |
|
96
|
|
|
* @param $entry |
|
97
|
|
|
* @param $key |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
* @throws \Exception |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function _deleteAndUnregisterFixture(Mage_Core_Model_Abstract $model, $fixtureType, $entry, $key) |
|
103
|
|
|
{ |
|
104
|
|
|
$fixture = $model->load((int) $entry); |
|
105
|
|
|
$fixture->delete(); |
|
106
|
|
|
Builder::unregister($key); |
|
107
|
|
|
|
|
108
|
|
|
return $this->_deleteFixtureMessage($fixtureType, $entry); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $fixtureType |
|
113
|
|
|
* @param string $entry |
|
114
|
|
|
* |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function _deleteFixtureMessage($fixtureType, $entry) |
|
118
|
|
|
{ |
|
119
|
|
|
return "-- DELETED {$fixtureType} fixture with ID {$entry}\n"; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $entryMatch |
|
124
|
|
|
* @param $entry |
|
125
|
|
|
* @param $key |
|
126
|
|
|
* |
|
127
|
|
|
* @throws \Exception |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function _changeRegistryEntry(array $entryMatch, $entry, $key) |
|
130
|
|
|
{ |
|
131
|
|
|
$mageModel = $this->_instantiateMageModel($entryMatch); |
|
132
|
|
|
|
|
133
|
|
|
if ($this->_isExpectedType($mageModel)) { |
|
134
|
|
|
$this->_echoRegistryChangeMessage( |
|
135
|
|
|
$mageModel, $entryMatch[1], $entry, $key |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param $mageModel |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
* |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function _isExpectedType($mageModel) |
|
147
|
|
|
{ |
|
148
|
|
|
return !is_null($mageModel) && is_a($mageModel, 'Mage_Core_Model_Abstract'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param array $entryMatch |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
* @throws \Exception |
|
156
|
|
|
* |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function _instantiateMageModel(array $entryMatch) |
|
159
|
|
|
{ |
|
160
|
|
|
if (!isset($entryMatch[1])) { |
|
161
|
|
|
return; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$mageModel = $this->getMageModelForMatch($entryMatch[1]); |
|
165
|
|
|
|
|
166
|
|
|
if (is_null($mageModel)) { |
|
167
|
|
|
throw new \Exception('Cannot initialise Magento model from registry entry match.'); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $mageModel; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|