Skip to content

Commit 91a407c

Browse files
committed
MAGETWO-33080: Preferences, Shared Instance creation and Compiled Factory optimization
- added backslashes remove for arguments
1 parent afc5f7c commit 91a407c

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

dev/tests/unit/testsuite/Magento/Tools/Di/App/CompilerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ private function getPreferences()
112112
'modificationsList' => [
113113
'PreferencesResolving' =>
114114
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\PreferencesResolving'],
115+
'BackslashTrim' =>
116+
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\BackslashTrim'],
115117
'ArgumentsSerialization' =>
116118
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\ArgumentsSerialization'],
117119
]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Tools\Di\Compiler\Config\Chain;
8+
9+
class BackslashTrimTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testModifyArgumentsDoNotExist()
12+
{
13+
$inputConfig = [
14+
'data' => []
15+
];
16+
$modifier = new BackslashTrim();
17+
$this->assertSame($inputConfig, $modifier->modify($inputConfig));
18+
}
19+
20+
public function testModifyArguments()
21+
{
22+
$modifier = new BackslashTrim();
23+
$this->assertEquals($this->getOutputConfig(), $modifier->modify($this->getInputConfig()));
24+
}
25+
26+
/**
27+
* Input config
28+
*
29+
* @return array
30+
*/
31+
private function getInputConfig()
32+
{
33+
return [
34+
'arguments' => [
35+
'\\Class' => [
36+
'argument_type' => ['_i_' => '\\Class\\Dependency'],
37+
'argument_not_shared' => ['_ins_' => '\\Class\\Dependency'],
38+
'array' => [
39+
'argument_type' => ['_i_' => '\\Class\\Dependency'],
40+
'argument_not_shared' => ['_ins_' => '\\Class\\Dependency'],
41+
'array' => [
42+
'argument_type' => ['_i_' => '\\Class\\Dependency'],
43+
'argument_not_shared' => ['_ins_' => '\\Class\\Dependency'],
44+
]
45+
]
46+
]
47+
]
48+
];
49+
}
50+
51+
/**
52+
* Output config
53+
*
54+
* @return array
55+
*/
56+
private function getOutputConfig()
57+
{
58+
return [
59+
'arguments' => [
60+
'Class' => [
61+
'argument_type' => ['_i_' => 'Class\\Dependency'],
62+
'argument_not_shared' => ['_ins_' => 'Class\\Dependency'],
63+
'array' => [
64+
'argument_type' => ['_i_' => 'Class\\Dependency'],
65+
'argument_not_shared' => ['_ins_' => 'Class\\Dependency'],
66+
'array' => [
67+
'argument_type' => ['_i_' => 'Class\\Dependency'],
68+
'argument_not_shared' => ['_ins_' => 'Class\\Dependency'],
69+
]
70+
]
71+
]
72+
]
73+
];
74+
}
75+
}

dev/tools/Magento/Tools/Di/App/Compiler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public function launch()
6868
'modificationsList' => [
6969
'PreferencesResolving' =>
7070
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\PreferencesResolving'],
71+
'BackslashTrim' =>
72+
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\BackslashTrim'],
7173
'ArgumentsSerialization' =>
7274
['instance' => 'Magento\Tools\Di\Compiler\Config\Chain\ArgumentsSerialization'],
7375
]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Tools\Di\Compiler\Config\Chain;
8+
9+
class BackslashTrim implements ModificationInterface
10+
{
11+
/**
12+
* Modifies input config
13+
*
14+
* @param array $config
15+
* @return array
16+
*/
17+
public function modify(array $config)
18+
{
19+
if (!isset($config['arguments'])) {
20+
return $config;
21+
}
22+
23+
$config['arguments'] = $this->resolveInstancesNames($config['arguments']);
24+
$this->resolveArguments($config['arguments']);
25+
26+
return $config;
27+
}
28+
29+
/**
30+
* Resolves instances names
31+
*
32+
* @param array $arguments
33+
* @return array
34+
*/
35+
private function resolveInstancesNames(array $arguments)
36+
{
37+
$resolvedInstances = [];
38+
foreach ($arguments as $instance => $constructor) {
39+
$resolvedInstances[ltrim($instance, '\\')] = $constructor;
40+
}
41+
42+
return $resolvedInstances;
43+
}
44+
45+
/**
46+
* Resolves instances arguments
47+
*
48+
* @param array $argument
49+
*/
50+
private function resolveArguments(&$argument)
51+
{
52+
if (!is_array($argument)) {
53+
return;
54+
}
55+
56+
foreach ($argument as $key => &$value) {
57+
if (in_array($key, ['_i_', '_ins_'])) {
58+
$value = ltrim($value, '\\');
59+
continue;
60+
}
61+
62+
if (is_array($value)) {
63+
$this->resolveArguments($value);
64+
}
65+
}
66+
return;
67+
}
68+
}

0 commit comments

Comments
 (0)