Skip to content

Commit cb0a213

Browse files
author
Franciszek Wawrzak
committed
get object manager from DI
1 parent d97dba0 commit cb0a213

File tree

8 files changed

+109
-64
lines changed

8 files changed

+109
-64
lines changed

lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledInterceptor.php

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Magento\Framework\CompiledInterception\Generator;
88

9-
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\ObjectManagerInterface;
1010
use Magento\Framework\Code\Generator\EntityAbstract;
1111
use Magento\Framework\Config\Scope;
1212
use Magento\Framework\Interception\Code\Generator\Interceptor;
@@ -64,21 +64,14 @@ protected function _generateCode()
6464
$this->_classGenerator->setExtendedClass($typeName);
6565
}
6666

67-
$this->_classGenerator->addUse(ObjectManager::class);
68-
$this->_classGenerator->addUse(Scope::class);
69-
7067
$this->classMethods = [];
7168
$this->classProperties = [];
69+
$this->injectPropertiesSettersToConstructor($reflection->getConstructor(), [
70+
Scope::class => '____scope',
71+
ObjectManagerInterface::class => '____om',
72+
]);
7273
$this->overrideMethodsAndGeneratePluginGetters($reflection);
7374

74-
array_unshift($this->classMethods, $this->_getConstructorInfo($reflection->getConstructor()));
75-
array_unshift($this->classProperties, [
76-
'name' => '____scope',
77-
'visibility' => 'private',
78-
'docblock' => [
79-
'tags' => [['name' => 'var', 'description' => 'Scope']],
80-
]
81-
]);
8275
//return parent::_generateCode();
8376
return EntityAbstract::_generateCode();
8477
}
@@ -104,53 +97,67 @@ protected function overrideMethodsAndGeneratePluginGetters(\ReflectionClass $ref
10497
}
10598
}
10699

107-
protected function _getClassMethods()
108-
{
109-
return $this->classMethods;
110-
}
111-
112-
protected function _getClassProperties()
113-
{
114-
return $this->classProperties;
115-
}
116-
117-
protected function _getConstructorInfo(\ReflectionMethod $parentConstructor = null)
100+
protected function injectPropertiesSettersToConstructor(\ReflectionMethod $parentConstructor = null, $properties = [])
118101
{
119102
if ($parentConstructor == null) {
120-
$parameters = [[
121-
'name' => 'scope',
122-
'type' => Scope::class
123-
]];
124-
$body = ["\$this->____scope = \$scope;"];
103+
$parameters = [];
104+
$body = [];
125105
} else {
126106
$parameters = $parentConstructor->getParameters();
127-
$addScopeParam = true;
128-
$scopeParamName = '____scope';
129107
foreach ($parameters as $parameter) {
130108
$parentCallParams[] = '$' . $parameter->getName();
131-
if ($parameter->getType() == Scope::class) {
132-
$scopeParamName = $parameter->getName();
133-
$addScopeParam = false;
109+
}
110+
$body = ["parent::__construct(" . implode(', ', $parentCallParams) .");"];
111+
}
112+
foreach ($properties as $type => $name) {
113+
$this->_classGenerator->addUse($type);
114+
$this->classProperties[] = [
115+
'name' => $name,
116+
'visibility' => 'private',
117+
'docblock' => [
118+
'tags' => [['name' => 'var', 'description' => substr(strrchr($type, "\\"), 1)]],
119+
]
120+
];
121+
}
122+
$extraParams = $properties;
123+
$extraSetters = array_combine($properties, $properties);
124+
foreach ($parameters as $parameter) {
125+
if ($parameter->getType()) {
126+
$type = $parameter->getType()->getName();
127+
if (isset($properties[$type])) {
128+
$extraSetters[$properties[$type]] = $parameter->getName();
129+
unset($extraParams[$type]);
134130
}
135131
}
136-
137-
$parameters = array_map(array($this, '_getMethodParameterInfo'), $parameters);
138-
$addScopeParam && array_unshift($parameters, [
139-
'name' => $scopeParamName,
140-
'type' => Scope::class
132+
}
133+
$parameters = array_map(array($this, '_getMethodParameterInfo'), $parameters);
134+
foreach ($extraParams as $type => $name) {
135+
array_unshift($parameters, [
136+
'name' => $name,
137+
'type' => $type
141138
]);
142-
$body = [
143-
"\$this->____scope = \$$scopeParamName;",
144-
"parent::__construct(" . implode(', ', $parentCallParams) .");"
145-
];
139+
}
140+
foreach ($extraSetters as $name => $paramName) {
141+
array_unshift($body, "\$this->$name = \$$paramName;");
146142
}
147143

148-
return [
144+
$this->classMethods[] = [
149145
'name' => '__construct',
150146
'parameters' => $parameters,
151147
'body' => implode("\n", $body),
152148
'docblock' => ['shortDescription' => '{@inheritdoc}'],
153149
];
150+
151+
}
152+
153+
protected function _getClassMethods()
154+
{
155+
return $this->classMethods;
156+
}
157+
158+
protected function _getClassProperties()
159+
{
160+
return $this->classProperties;
154161
}
155162

156163
private function addCodeSubBlock(&$body, $sub, $indent = 1)
@@ -211,7 +218,7 @@ protected function _getMethodSourceFromConfig(\ReflectionMethod $method, $conf,
211218
}
212219
}
213220
foreach ($chain as $lp => $piece) {
214-
//if ($first) $first = false; else $body[] = "";
221+
if ($first) $first = false; else $body[] = "";
215222
if (!$returnVoid) {
216223
$piece[0] = (($lp + 1 == count($chain)) ? "return " : "\$result = ") . $piece[0];
217224
}
@@ -278,7 +285,7 @@ protected function _getPluginGetterInfo($plugin)
278285
$varName = "\$this->____plugin_" . $plugin['clean_name'];
279286

280287
$body[] = "if ($varName === null) {";
281-
$body[] = "\t$varName = ObjectManager::getInstance()->get(\\" . "{$plugin['class']}::class);";
288+
$body[] = "\t$varName = \$this->____om->get(\\" . "{$plugin['class']}::class);";
282289
$body[] = "}";
283290
$body[] = "return $varName;";
284291

lib/internal/Magento/Framework/CompiledInterception/Generator/CompiledPluginList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Magento\Framework\Interception\DefinitionInterface;
1111
use Magento\Framework\Interception\PluginListInterface as InterceptionPluginList;
1212
use Magento\Framework\Interception\ObjectManager\ConfigInterface;
13-
use Magento\Framework\ObjectManager\Config\Reader\Dom\Proxy;
13+
use Magento\Framework\ObjectManager\Config\Reader\Dom;
1414
use Magento\Framework\ObjectManager\RelationsInterface;
1515
use Magento\Framework\ObjectManager\DefinitionInterface as ClassDefinitions;
1616

@@ -121,7 +121,7 @@ public function __construct(
121121
$this->_omConfig = $omConfig;
122122
} else {
123123
$objectManager = ObjectManager::getInstance();
124-
$this->_reader = $objectManager->get(Proxy::class);
124+
$this->_reader = $objectManager->get(Dom::class);
125125
$this->_omConfig = $objectManager->get(ConfigInterface::class);
126126
}
127127
$this->_relations = new \Magento\Framework\ObjectManager\Relations\Runtime();

lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/CompiledInterceptorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Magento\Framework\Code\Generator\Io;
1010
use Magento\Framework\CompiledInterception\Generator\CompiledInterceptor;
1111

12-
use Magento\Framework\CompiledInterception\Test\Unit\PluginList\CompiledPluginListTest;
12+
use Magento\Framework\CompiledInterception\Test\Unit\CompiledPluginList\CompiledPluginListTest;
1313
use \PHPUnit_Framework_MockObject_MockObject as MockObject;
1414

1515
class CompiledInterceptorTest extends \PHPUnit\Framework\TestCase

lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItem.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem;
22

3-
use Magento\Framework\App\ObjectManager;
43
use Magento\Framework\Config\Scope;
4+
use Magento\Framework\ObjectManagerInterface;
55

66
/**
77
* Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItem
@@ -13,6 +13,11 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
1313
*/
1414
private $____scope = null;
1515

16+
/**
17+
* @var ObjectManagerInterface
18+
*/
19+
private $____om = null;
20+
1621
/**
1722
* @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced
1823
*/
@@ -26,9 +31,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
2631
/**
2732
* {@inheritdoc}
2833
*/
29-
public function __construct(\Magento\Framework\Config\Scope $scope)
34+
public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope)
3035
{
31-
$this->____scope = $scope;
36+
$this->____om = $____om;
37+
$this->____scope = $____scope;
3238
}
3339

3440
/**
@@ -41,18 +47,23 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
4147
case 'frontend':
4248
case 'emptyscope':
4349
$this->____plugin_advanced_plugin()->beforeGetName($this);
50+
4451
$result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){
4552
return parent::getName();
4653
});
54+
4755
return $this->____plugin_advanced_plugin()->afterGetName($this, $result);
4856
case 'backend':
4957
$this->____plugin_advanced_plugin()->beforeGetName($this);
58+
5059
$result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){
5160
$result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){
5261
return parent::getName();
5362
});
63+
5464
return $this->____plugin_complex_plugin()->afterGetName($this, $result);
5565
});
66+
5667
return $this->____plugin_advanced_plugin()->afterGetName($this, $result);
5768
default:
5869
return parent::getName();
@@ -68,6 +79,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
6879
case 'backend':
6980
$beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value);
7081
if ($beforeResult !== null) list($value) = (array)$beforeResult;
82+
7183
return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){
7284
return parent::setValue($value);
7385
}, $value);
@@ -143,7 +155,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
143155
public function ____plugin_advanced_plugin()
144156
{
145157
if ($this->____plugin_advanced_plugin === null) {
146-
$this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class);
158+
$this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class);
147159
}
148160
return $this->____plugin_advanced_plugin;
149161
}
@@ -155,7 +167,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
155167
public function ____plugin_complex_plugin()
156168
{
157169
if ($this->____plugin_complex_plugin === null) {
158-
$this->____plugin_complex_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class);
170+
$this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class);
159171
}
160172
return $this->____plugin_complex_plugin;
161173
}

lib/internal/Magento/Framework/CompiledInterception/Test/Unit/CompiledInterceptor/_out_interceptors/ComplexItemTyped.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped;
22

3-
use Magento\Framework\App\ObjectManager;
43
use Magento\Framework\Config\Scope;
4+
use Magento\Framework\ObjectManagerInterface;
55

66
/**
77
* Interceptor class for @see \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ComplexItemTyped
@@ -13,6 +13,11 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
1313
*/
1414
private $____scope = null;
1515

16+
/**
17+
* @var ObjectManagerInterface
18+
*/
19+
private $____om = null;
20+
1621
/**
1722
* @var \Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex
1823
*/
@@ -26,9 +31,10 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
2631
/**
2732
* {@inheritdoc}
2833
*/
29-
public function __construct(\Magento\Framework\Config\Scope $scope)
34+
public function __construct(\Magento\Framework\ObjectManagerInterface $____om, \Magento\Framework\Config\Scope $____scope)
3035
{
31-
$this->____scope = $scope;
36+
$this->____om = $____om;
37+
$this->____scope = $____scope;
3238
}
3339

3440
/**
@@ -39,6 +45,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
3945
switch($this->____scope->getCurrentScope()){
4046
case 'backend':
4147
parent::returnVoid();
48+
4249
(($tmp = $this->____plugin_complex_plugin()->afterReturnVoid($this, $result)) !== null) ? $tmp : $result;
4350
default:
4451
parent::returnVoid();
@@ -53,6 +60,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
5360
switch($this->____scope->getCurrentScope()){
5461
case 'backend':
5562
$result = parent::getNullableValue();
63+
5664
return $this->____plugin_complex_plugin()->afterGetNullableValue($this, $result);
5765
default:
5866
return parent::getNullableValue();
@@ -67,12 +75,15 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
6775
switch($this->____scope->getCurrentScope()){
6876
case 'backend':
6977
$this->____plugin_advanced_plugin()->beforeGetName($this);
78+
7079
$result = $this->____plugin_advanced_plugin()->aroundGetName($this, function(){
7180
$result = $this->____plugin_complex_plugin()->aroundGetName($this, function(){
7281
return parent::getName();
7382
});
83+
7484
return $this->____plugin_complex_plugin()->afterGetName($this, $result);
7585
});
86+
7687
return $this->____plugin_advanced_plugin()->afterGetName($this, $result);
7788
default:
7889
return parent::getName();
@@ -88,6 +99,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
8899
case 'backend':
89100
$beforeResult = $this->____plugin_complex_plugin()->beforeSetValue($this, $value);
90101
if ($beforeResult !== null) list($value) = (array)$beforeResult;
102+
91103
return $this->____plugin_complex_plugin()->aroundSetValue($this, function($value){
92104
return parent::setValue($value);
93105
}, $value);
@@ -148,7 +160,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
148160
public function ____plugin_complex_plugin()
149161
{
150162
if ($this->____plugin_complex_plugin === null) {
151-
$this->____plugin_complex_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class);
163+
$this->____plugin_complex_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Complex::class);
152164
}
153165
return $this->____plugin_complex_plugin;
154166
}
@@ -160,7 +172,7 @@ class Interceptor extends \Magento\Framework\CompiledInterception\Test\Unit\Cust
160172
public function ____plugin_advanced_plugin()
161173
{
162174
if ($this->____plugin_advanced_plugin === null) {
163-
$this->____plugin_advanced_plugin = ObjectManager::getInstance()->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class);
175+
$this->____plugin_advanced_plugin = $this->____om->get(\Magento\Framework\CompiledInterception\Test\Unit\Custom\Module\Model\ItemPlugin\Advanced::class);
164176
}
165177
return $this->____plugin_advanced_plugin;
166178
}

0 commit comments

Comments
 (0)