Skip to content

Commit 47b5ed2

Browse files
authored
Merge pull request #982 from magento-troll/MAGETWO-66100
MAGETWO-65801: Remove usages of unserialize in module /Magento/Setup/. CE - missed usages
2 parents 50ed592 + 30e0c7e commit 47b5ed2

File tree

10 files changed

+127
-135
lines changed

10 files changed

+127
-135
lines changed

lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ class Compiled implements ConfigLoaderInterface
2020
*/
2121
private $configCache = [];
2222

23-
/**
24-
* @var SerializerInterface
25-
*/
26-
private $serializer;
27-
2823
/**
2924
* {inheritdoc}
3025
*/
@@ -33,7 +28,8 @@ public function load($area)
3328
if (isset($this->configCache[$area])) {
3429
return $this->configCache[$area];
3530
}
36-
$this->configCache[$area] = $this->getSerializer()->unserialize(\file_get_contents(self::getFilePath($area)));
31+
$diConfiguration = include(self::getFilePath($area));
32+
$this->configCache[$area] = $diConfiguration;
3733
return $this->configCache[$area];
3834
}
3935

@@ -46,20 +42,6 @@ public function load($area)
4642
public static function getFilePath($area)
4743
{
4844
$diPath = DirectoryList::getDefaultConfig()[DirectoryList::GENERATED_METADATA][DirectoryList::PATH];
49-
return BP . '/' . $diPath . '/' . $area . '.ser';
50-
}
51-
52-
/**
53-
* Get serializer
54-
*
55-
* @return SerializerInterface
56-
* @deprecated
57-
*/
58-
private function getSerializer()
59-
{
60-
if (null === $this->serializer) {
61-
$this->serializer = new Serialize();
62-
}
63-
return $this->serializer;
45+
return BP . '/' . $diPath . '/' . $area . '.php';
6446
}
6547
}

lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
*/
66
namespace Magento\Framework\ObjectManager\Config;
77

8-
use Magento\Framework\ObjectManager\ConfigCacheInterface;
98
use Magento\Framework\ObjectManager\ConfigInterface;
9+
use Magento\Framework\ObjectManager\ConfigCacheInterface;
1010
use Magento\Framework\ObjectManager\RelationsInterface;
11-
use Magento\Framework\Serialize\Serializer\Serialize;
12-
use Magento\Framework\Serialize\SerializerInterface;
1311

1412
/**
1513
* Provides object manager configuration when in compiled mode
@@ -31,21 +29,16 @@ class Compiled implements ConfigInterface
3129
*/
3230
private $preferences;
3331

34-
/**
35-
* @var SerializerInterface
36-
*/
37-
private $serializer;
38-
3932
/**
4033
* Constructor
4134
*
4235
* @param array $data
4336
*/
4437
public function __construct($data)
4538
{
46-
$this->arguments = $data['arguments'];
47-
$this->virtualTypes = $data['instanceTypes'];
48-
$this->preferences = $data['preferences'];
39+
$this->arguments = $data['arguments'] ?: [];
40+
$this->virtualTypes = $data['instanceTypes'] ?: [];
41+
$this->preferences = $data['preferences'] ?: [];
4942
}
5043

5144
/**
@@ -83,9 +76,7 @@ public function setCache(ConfigCacheInterface $cache)
8376
public function getArguments($type)
8477
{
8578
if (array_key_exists($type, $this->arguments)) {
86-
if (is_string($this->arguments[$type])) {
87-
$this->arguments[$type] = $this->getSerializer()->unserialize($this->arguments[$type]);
88-
} elseif ($this->arguments[$type] === null) {
79+
if ($this->arguments[$type] === null) {
8980
$this->arguments[$type] = [];
9081
}
9182
return $this->arguments[$type];
@@ -173,19 +164,4 @@ public function getPreferences()
173164
{
174165
return $this->preferences;
175166
}
176-
177-
/**
178-
* Get serializer
179-
*
180-
* @return SerializerInterface
181-
* @deprecated
182-
*/
183-
private function getSerializer()
184-
{
185-
if (null === $this->serializer) {
186-
$this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
187-
->get(Serialize::class);
188-
}
189-
return $this->serializer;
190-
}
191167
}

lib/internal/Magento/Framework/ObjectManager/Test/Unit/Config/CompiledTest.php

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
namespace Magento\Framework\ObjectManager\Test\Unit\Config;
77

88
use Magento\Framework\ObjectManager\Config\Compiled;
9-
use Magento\Framework\Serialize\SerializerInterface;
109
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManager;
1110

1211
class CompiledTest extends \PHPUnit_Framework_TestCase
@@ -16,11 +15,6 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
1615
*/
1716
private $objectManager;
1817

19-
/**
20-
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
21-
*/
22-
private $serializerMock;
23-
2418
/**
2519
* @var \Magento\Framework\ObjectManager\Config\Compiled
2620
*/
@@ -29,15 +23,14 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
2923
protected function setUp()
3024
{
3125
$this->objectManager = new ObjectManager($this);
32-
$this->serializerMock = $this->getMock(SerializerInterface::class);
3326

3427
$initialData = [
3528
'arguments' => [
3629
'type1' => 'initial serialized configuration for type1',
3730
'class_with_no_arguments_serialized' => null,
38-
'class_with_arguments_serialized' => 'serialized arguments',
39-
'class_with_arguments_unserialized' => ['unserialized', 'arguments'],
40-
'class_with_no_arguments_unserialized' => [],
31+
'class_with_arguments_string' => 'string arguments',
32+
'class_with_arguments_array' => ['unserialized', 'arguments'],
33+
'class_with_no_arguments_empty_array' => [],
4134
],
4235
'instanceTypes' => [
4336
'instanceType1' => 'instanceTypeValue1',
@@ -53,58 +46,79 @@ protected function setUp()
5346
Compiled::class,
5447
[
5548
'data' => $initialData,
56-
'serializer' => $this->serializerMock
5749
]
5850
);
5951
}
6052

61-
public function testExtend()
53+
/**
54+
* Test is it possible extend/overwrite arguments for the DI.
55+
*
56+
*/
57+
public function testExtendArguments()
6258
{
6359
$configuration = [
6460
'arguments' => [
65-
'type1' => 'serialized configuration for type1',
66-
'type2' => 'serialized configuration for type2'
61+
'type1' => 'configuration for type1',
62+
'type2' => [
63+
'argument2_1' => 'newArgumentValue2_1',
64+
]
6765
],
6866
'instanceTypes' => [
6967
'instanceType2' => 'newInstanceTypeValue2',
70-
'instanceType3' => 'newInstanceTypeValue3'
68+
'instanceType3' => 'newInstanceTypeValue3',
7169
],
7270
'preferences' => [
73-
'preference1' => 'newPreferenceValue1'
74-
]
71+
'preference1' => 'newPreferenceValue1',
72+
],
7573
];
7674
$expectedArguments = [
77-
'type1' => [
78-
'argument1_1' => 'newArgumentValue1_1'
79-
],
75+
'type1' => 'configuration for type1',
8076
'type2' => [
81-
'argument2_1' => 'newArgumentValue2_1'
77+
'argument2_1' => 'newArgumentValue2_1',
8278
]
8379
];
84-
$expectedVirtualTypes = [
80+
81+
$this->compiled->extend($configuration);
82+
foreach ($expectedArguments as $type => $arguments) {
83+
$this->assertEquals($arguments, $this->compiled->getArguments($type));
84+
}
85+
}
86+
87+
/**
88+
* Test getting virtual types from the DI.
89+
*/
90+
public function testVirtualTypes()
91+
{
92+
$configuration = [
93+
'instanceTypes' => [
94+
'instanceType2' => 'newInstanceTypeValue2',
95+
'instanceType3' => 'newInstanceTypeValue3'
96+
],
97+
];
98+
$expectedTypes = [
8599
'instanceType1' => 'instanceTypeValue1',
86100
'instanceType2' => 'newInstanceTypeValue2',
87101
'instanceType3' => 'newInstanceTypeValue3'
88102
];
103+
$this->compiled->extend($configuration);
104+
$this->assertEquals($expectedTypes, $this->compiled->getVirtualTypes());
105+
}
106+
107+
/**
108+
* Test getting preferences from the DI.
109+
*/
110+
public function testPreferences()
111+
{
112+
$configuration = [
113+
'preferences' => [
114+
'preference1' => 'newPreferenceValue1'
115+
]
116+
];
89117
$expectedPreferences = [
90118
'preference1' => 'newPreferenceValue1',
91119
'preference2' => 'preferenceValue2'
92120
];
93-
94-
$this->serializerMock->expects($this->at(0))
95-
->method('unserialize')
96-
->with($configuration['arguments']['type1'])
97-
->willReturn($expectedArguments['type1']);
98-
$this->serializerMock->expects($this->at(1))
99-
->method('unserialize')
100-
->with($configuration['arguments']['type2'])
101-
->willReturn($expectedArguments['type2']);
102-
103121
$this->compiled->extend($configuration);
104-
foreach ($expectedArguments as $type => $arguments) {
105-
$this->assertEquals($arguments, $this->compiled->getArguments($type));
106-
}
107-
$this->assertEquals($expectedVirtualTypes, $this->compiled->getVirtualTypes());
108122
$this->assertEquals($expectedPreferences, $this->compiled->getPreferences());
109123
}
110124

@@ -113,23 +127,17 @@ public function testExtend()
113127
*/
114128
public function testGetArgumentsSerialized()
115129
{
116-
$unserializedArguments = ['unserialized', 'arguments'];
117-
118-
// method called twice but after one unserialization, unserialized version should be stored
119-
$this->serializerMock->expects($this->once())->method('unserialize')
120-
->with('serialized arguments')
121-
->willReturn($unserializedArguments);
130+
$unserializedArguments = 'string arguments';
122131

123-
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_serialized'));
124-
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_serialized'));
132+
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
133+
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
125134
}
126135

127136
/**
128137
* Arguments defined in array, have not previously been unserialized
129138
*/
130139
public function testGetArgumentsSerializedEmpty()
131140
{
132-
$this->serializerMock->expects($this->never())->method('unserialize');
133141
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_serialized'));
134142
}
135143

@@ -139,25 +147,22 @@ public function testGetArgumentsSerializedEmpty()
139147
public function testGetArgumentsUnserialized()
140148
{
141149
$unserializedArguments = ['unserialized', 'arguments'];
142-
$this->serializerMock->expects($this->never())->method('unserialize');
143-
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_unserialized'));
150+
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_array'));
144151
}
145152

146153
/**
147154
* Arguments are defined but empty
148155
*/
149156
public function testGetArgumentsUnserializedEmpty()
150157
{
151-
$this->serializerMock->expects($this->never())->method('unserialize');
152-
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_unserialized'));
158+
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_empty_array'));
153159
}
154160

155161
/**
156162
* Arguments not defined in array
157163
*/
158164
public function testGetArgumentsNotDefined()
159165
{
160-
$this->serializerMock->expects($this->never())->method('unserialize');
161166
$this->assertSame(null, $this->compiled->getArguments('class_not_stored_in_config'));
162167
}
163168
}

setup/src/Magento/Setup/Console/Command/DiCompileCommand.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,6 @@ private function configureObjectManager(OutputInterface $output)
284284
'InterceptionPreferencesResolving' =>
285285
['instance' =>
286286
\Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class],
287-
'ArgumentsSerialization' =>
288-
['instance' =>
289-
\Magento\Setup\Module\Di\Compiler\Config\Chain\ArgumentsSerialization::class],
290287
]
291288
]
292289
], \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [

setup/src/Magento/Setup/Model/AdminAccount.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ private function saveAdminUser()
119119
// User does not exist, create it
120120
$adminData['username'] = $this->data[self::KEY_USER];
121121
$adminData['email'] = $this->data[self::KEY_EMAIL];
122-
$adminData['extra'] = serialize(null);
123122
$this->setup->getConnection()->insert(
124123
$this->setup->getTable('admin_user'),
125124
$adminData

setup/src/Magento/Setup/Model/AdminAccountFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Setup\Module\Setup;
1010
use Zend\ServiceManager\ServiceLocatorInterface;
11+
use Magento\Framework\Serialize\Serializer\Json;
1112

1213
class AdminAccountFactory
1314
{

setup/src/Magento/Setup/Module/Di/Compiler/Config/Chain/ArgumentsSerialization.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,35 @@
77
namespace Magento\Setup\Module\Di\Compiler\Config\Chain;
88

99
use Magento\Setup\Module\Di\Compiler\Config\ModificationInterface;
10+
use Magento\Framework\Serialize\SerializerInterface;
11+
use Magento\Framework\App\ObjectManager;
12+
use Magento\Framework\Serialize\Serializer\Json;
1013

14+
/**
15+
* Used for argument's array serialization and store to the DI configuration.
16+
*
17+
* @deprecated Di arguments are now stored in raw php format and could be cached by OPcache,
18+
* this class will be removed in the next backward incompatible release.
19+
*/
1120
class ArgumentsSerialization implements ModificationInterface
1221
{
22+
/**
23+
* Used for serialize/unserialize data.
24+
*
25+
* @var Json
26+
*/
27+
private $serializer;
28+
29+
/**
30+
* Constructor.
31+
*
32+
* @param SerializerInterface|null $serializer
33+
*/
34+
public function __construct(SerializerInterface $serializer = null)
35+
{
36+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
37+
}
38+
1339
/**
1440
* Modifies input config
1541
*
@@ -24,7 +50,7 @@ public function modify(array $config)
2450

2551
foreach ($config['arguments'] as $key => $value) {
2652
if ($value !== null) {
27-
$config['arguments'][$key] = serialize($value);
53+
$config['arguments'][$key] = $this->serializer->serialize($value);
2854
}
2955
}
3056

0 commit comments

Comments
 (0)