Skip to content

Commit

Permalink
Merge pull request #982 from magento-troll/MAGETWO-66100
Browse files Browse the repository at this point in the history
MAGETWO-65801: Remove usages of unserialize in module /Magento/Setup/. CE - missed usages
  • Loading branch information
rganin authored Mar 31, 2017
2 parents 50ed592 + 30e0c7e commit 47b5ed2
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ class Compiled implements ConfigLoaderInterface
*/
private $configCache = [];

/**
* @var SerializerInterface
*/
private $serializer;

/**
* {inheritdoc}
*/
Expand All @@ -33,7 +28,8 @@ public function load($area)
if (isset($this->configCache[$area])) {
return $this->configCache[$area];
}
$this->configCache[$area] = $this->getSerializer()->unserialize(\file_get_contents(self::getFilePath($area)));
$diConfiguration = include(self::getFilePath($area));
$this->configCache[$area] = $diConfiguration;
return $this->configCache[$area];
}

Expand All @@ -46,20 +42,6 @@ public function load($area)
public static function getFilePath($area)
{
$diPath = DirectoryList::getDefaultConfig()[DirectoryList::GENERATED_METADATA][DirectoryList::PATH];
return BP . '/' . $diPath . '/' . $area . '.ser';
}

/**
* Get serializer
*
* @return SerializerInterface
* @deprecated
*/
private function getSerializer()
{
if (null === $this->serializer) {
$this->serializer = new Serialize();
}
return $this->serializer;
return BP . '/' . $diPath . '/' . $area . '.php';
}
}
34 changes: 5 additions & 29 deletions lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
*/
namespace Magento\Framework\ObjectManager\Config;

use Magento\Framework\ObjectManager\ConfigCacheInterface;
use Magento\Framework\ObjectManager\ConfigInterface;
use Magento\Framework\ObjectManager\ConfigCacheInterface;
use Magento\Framework\ObjectManager\RelationsInterface;
use Magento\Framework\Serialize\Serializer\Serialize;
use Magento\Framework\Serialize\SerializerInterface;

/**
* Provides object manager configuration when in compiled mode
Expand All @@ -31,21 +29,16 @@ class Compiled implements ConfigInterface
*/
private $preferences;

/**
* @var SerializerInterface
*/
private $serializer;

/**
* Constructor
*
* @param array $data
*/
public function __construct($data)
{
$this->arguments = $data['arguments'];
$this->virtualTypes = $data['instanceTypes'];
$this->preferences = $data['preferences'];
$this->arguments = $data['arguments'] ?: [];
$this->virtualTypes = $data['instanceTypes'] ?: [];
$this->preferences = $data['preferences'] ?: [];
}

/**
Expand Down Expand Up @@ -83,9 +76,7 @@ public function setCache(ConfigCacheInterface $cache)
public function getArguments($type)
{
if (array_key_exists($type, $this->arguments)) {
if (is_string($this->arguments[$type])) {
$this->arguments[$type] = $this->getSerializer()->unserialize($this->arguments[$type]);
} elseif ($this->arguments[$type] === null) {
if ($this->arguments[$type] === null) {
$this->arguments[$type] = [];
}
return $this->arguments[$type];
Expand Down Expand Up @@ -173,19 +164,4 @@ public function getPreferences()
{
return $this->preferences;
}

/**
* Get serializer
*
* @return SerializerInterface
* @deprecated
*/
private function getSerializer()
{
if (null === $this->serializer) {
$this->serializer = \Magento\Framework\App\ObjectManager::getInstance()
->get(Serialize::class);
}
return $this->serializer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
namespace Magento\Framework\ObjectManager\Test\Unit\Config;

use Magento\Framework\ObjectManager\Config\Compiled;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManager;

class CompiledTest extends \PHPUnit_Framework_TestCase
Expand All @@ -16,11 +15,6 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
*/
private $objectManager;

/**
* @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $serializerMock;

/**
* @var \Magento\Framework\ObjectManager\Config\Compiled
*/
Expand All @@ -29,15 +23,14 @@ class CompiledTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->serializerMock = $this->getMock(SerializerInterface::class);

$initialData = [
'arguments' => [
'type1' => 'initial serialized configuration for type1',
'class_with_no_arguments_serialized' => null,
'class_with_arguments_serialized' => 'serialized arguments',
'class_with_arguments_unserialized' => ['unserialized', 'arguments'],
'class_with_no_arguments_unserialized' => [],
'class_with_arguments_string' => 'string arguments',
'class_with_arguments_array' => ['unserialized', 'arguments'],
'class_with_no_arguments_empty_array' => [],
],
'instanceTypes' => [
'instanceType1' => 'instanceTypeValue1',
Expand All @@ -53,58 +46,79 @@ protected function setUp()
Compiled::class,
[
'data' => $initialData,
'serializer' => $this->serializerMock
]
);
}

public function testExtend()
/**
* Test is it possible extend/overwrite arguments for the DI.
*
*/
public function testExtendArguments()
{
$configuration = [
'arguments' => [
'type1' => 'serialized configuration for type1',
'type2' => 'serialized configuration for type2'
'type1' => 'configuration for type1',
'type2' => [
'argument2_1' => 'newArgumentValue2_1',
]
],
'instanceTypes' => [
'instanceType2' => 'newInstanceTypeValue2',
'instanceType3' => 'newInstanceTypeValue3'
'instanceType3' => 'newInstanceTypeValue3',
],
'preferences' => [
'preference1' => 'newPreferenceValue1'
]
'preference1' => 'newPreferenceValue1',
],
];
$expectedArguments = [
'type1' => [
'argument1_1' => 'newArgumentValue1_1'
],
'type1' => 'configuration for type1',
'type2' => [
'argument2_1' => 'newArgumentValue2_1'
'argument2_1' => 'newArgumentValue2_1',
]
];
$expectedVirtualTypes = [

$this->compiled->extend($configuration);
foreach ($expectedArguments as $type => $arguments) {
$this->assertEquals($arguments, $this->compiled->getArguments($type));
}
}

/**
* Test getting virtual types from the DI.
*/
public function testVirtualTypes()
{
$configuration = [
'instanceTypes' => [
'instanceType2' => 'newInstanceTypeValue2',
'instanceType3' => 'newInstanceTypeValue3'
],
];
$expectedTypes = [
'instanceType1' => 'instanceTypeValue1',
'instanceType2' => 'newInstanceTypeValue2',
'instanceType3' => 'newInstanceTypeValue3'
];
$this->compiled->extend($configuration);
$this->assertEquals($expectedTypes, $this->compiled->getVirtualTypes());
}

/**
* Test getting preferences from the DI.
*/
public function testPreferences()
{
$configuration = [
'preferences' => [
'preference1' => 'newPreferenceValue1'
]
];
$expectedPreferences = [
'preference1' => 'newPreferenceValue1',
'preference2' => 'preferenceValue2'
];

$this->serializerMock->expects($this->at(0))
->method('unserialize')
->with($configuration['arguments']['type1'])
->willReturn($expectedArguments['type1']);
$this->serializerMock->expects($this->at(1))
->method('unserialize')
->with($configuration['arguments']['type2'])
->willReturn($expectedArguments['type2']);

$this->compiled->extend($configuration);
foreach ($expectedArguments as $type => $arguments) {
$this->assertEquals($arguments, $this->compiled->getArguments($type));
}
$this->assertEquals($expectedVirtualTypes, $this->compiled->getVirtualTypes());
$this->assertEquals($expectedPreferences, $this->compiled->getPreferences());
}

Expand All @@ -113,23 +127,17 @@ public function testExtend()
*/
public function testGetArgumentsSerialized()
{
$unserializedArguments = ['unserialized', 'arguments'];

// method called twice but after one unserialization, unserialized version should be stored
$this->serializerMock->expects($this->once())->method('unserialize')
->with('serialized arguments')
->willReturn($unserializedArguments);
$unserializedArguments = 'string arguments';

$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_serialized'));
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_serialized'));
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_string'));
}

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

Expand All @@ -139,25 +147,22 @@ public function testGetArgumentsSerializedEmpty()
public function testGetArgumentsUnserialized()
{
$unserializedArguments = ['unserialized', 'arguments'];
$this->serializerMock->expects($this->never())->method('unserialize');
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_unserialized'));
$this->assertSame($unserializedArguments, $this->compiled->getArguments('class_with_arguments_array'));
}

/**
* Arguments are defined but empty
*/
public function testGetArgumentsUnserializedEmpty()
{
$this->serializerMock->expects($this->never())->method('unserialize');
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_unserialized'));
$this->assertSame([], $this->compiled->getArguments('class_with_no_arguments_empty_array'));
}

/**
* Arguments not defined in array
*/
public function testGetArgumentsNotDefined()
{
$this->serializerMock->expects($this->never())->method('unserialize');
$this->assertSame(null, $this->compiled->getArguments('class_not_stored_in_config'));
}
}
3 changes: 0 additions & 3 deletions setup/src/Magento/Setup/Console/Command/DiCompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,6 @@ private function configureObjectManager(OutputInterface $output)
'InterceptionPreferencesResolving' =>
['instance' =>
\Magento\Setup\Module\Di\Compiler\Config\Chain\PreferencesResolving::class],
'ArgumentsSerialization' =>
['instance' =>
\Magento\Setup\Module\Di\Compiler\Config\Chain\ArgumentsSerialization::class],
]
]
], \Magento\Setup\Module\Di\Code\Generator\PluginList::class => [
Expand Down
1 change: 0 additions & 1 deletion setup/src/Magento/Setup/Model/AdminAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ private function saveAdminUser()
// User does not exist, create it
$adminData['username'] = $this->data[self::KEY_USER];
$adminData['email'] = $this->data[self::KEY_EMAIL];
$adminData['extra'] = serialize(null);
$this->setup->getConnection()->insert(
$this->setup->getTable('admin_user'),
$adminData
Expand Down
1 change: 1 addition & 0 deletions setup/src/Magento/Setup/Model/AdminAccountFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Setup\Module\Setup;
use Zend\ServiceManager\ServiceLocatorInterface;
use Magento\Framework\Serialize\Serializer\Json;

class AdminAccountFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,35 @@
namespace Magento\Setup\Module\Di\Compiler\Config\Chain;

use Magento\Setup\Module\Di\Compiler\Config\ModificationInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

/**
* Used for argument's array serialization and store to the DI configuration.
*
* @deprecated Di arguments are now stored in raw php format and could be cached by OPcache,
* this class will be removed in the next backward incompatible release.
*/
class ArgumentsSerialization implements ModificationInterface
{
/**
* Used for serialize/unserialize data.
*
* @var Json
*/
private $serializer;

/**
* Constructor.
*
* @param SerializerInterface|null $serializer
*/
public function __construct(SerializerInterface $serializer = null)
{
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
}

/**
* Modifies input config
*
Expand All @@ -24,7 +50,7 @@ public function modify(array $config)

foreach ($config['arguments'] as $key => $value) {
if ($value !== null) {
$config['arguments'][$key] = serialize($value);
$config['arguments'][$key] = $this->serializer->serialize($value);
}
}

Expand Down
Loading

0 comments on commit 47b5ed2

Please sign in to comment.