From 1cfec96ed584849fcbe84300cff40783c9c832aa Mon Sep 17 00:00:00 2001 From: Igor Melnikov <imelnikov@magento.com> Date: Tue, 1 Nov 2016 14:54:12 -0500 Subject: [PATCH] MAGETWO-60353: Replace json_decode in \Magento\Framework\ObjectManager\DefinitionFactory::_unpack with SerializerInterface Introducing SerializerInterface, removing obsolete code --- .../ObjectManager/ConfigLoader/Compiled.php | 2 +- .../Framework/App/ObjectManagerFactory.php | 4 +- .../ObjectManager/Definition/Compiled.php | 2 +- .../ObjectManager/DefinitionFactory.php | 78 +++-------- .../Test/Unit/DefinitionFactoryTest.php | 122 ++++++------------ .../Di/Compiler/Config/Writer/Filesystem.php | 2 +- 6 files changed, 61 insertions(+), 149 deletions(-) diff --git a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php index 26ef880e151..8b927377525 100644 --- a/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php +++ b/lib/internal/Magento/Framework/App/ObjectManager/ConfigLoader/Compiled.php @@ -46,7 +46,7 @@ class Compiled implements ConfigLoaderInterface public static function getFilePath($area) { $diPath = DirectoryList::getDefaultConfig()[DirectoryList::DI][DirectoryList::PATH]; - return BP . $diPath . '/' . $area . '.json'; + return BP . $diPath . '/' . $area . '.ser'; } /** diff --git a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php index bfe94a9dccc..a16795ec6c9 100644 --- a/lib/internal/Magento/Framework/App/ObjectManagerFactory.php +++ b/lib/internal/Magento/Framework/App/ObjectManagerFactory.php @@ -117,7 +117,7 @@ class ObjectManagerFactory $arguments = array_merge($deploymentConfig->get(), $arguments); $definitionFactory = new \Magento\Framework\ObjectManager\DefinitionFactory( $this->driverPool->getDriver(DriverPool::FILE), - $this->directoryList->getPath(DirectoryList::DI), + new \Magento\Framework\Serialize\Serializer\Json(), $this->directoryList->getPath(DirectoryList::GENERATION) ); @@ -127,7 +127,7 @@ class ObjectManagerFactory /** @var EnvironmentFactory $envFactory */ $envFactory = new $this->envFactoryClassName($relations, $definitions); /** @var EnvironmentInterface $env */ - $env = $envFactory->createEnvironment(); + $env = $envFactory->createEnvironment(); /** @var ConfigInterface $diConfig */ $diConfig = $env->getDiConfig(); diff --git a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php index db91618dccd..e2a4f0c34a2 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Definition/Compiled.php @@ -22,7 +22,7 @@ class Compiled implements \Magento\Framework\ObjectManager\DefinitionInterface /** * @var \Magento\Framework\Code\Reader\ClassReaderInterface */ - protected $reader ; + protected $reader; /** * @var SerializerInterface diff --git a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php index 4e9026eb7b2..f818296c526 100644 --- a/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php +++ b/lib/internal/Magento/Framework/ObjectManager/DefinitionFactory.php @@ -1,33 +1,22 @@ <?php /** - * Object manager definition factory - * * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. - * */ - -// @codingStandardsIgnoreFile - namespace Magento\Framework\ObjectManager; use Magento\Framework\Filesystem\DriverInterface; use Magento\Framework\Interception\Code\Generator as InterceptionGenerator; use Magento\Framework\ObjectManager\Definition\Runtime; use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\ObjectManager\Definition\Compiled; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class DefinitionFactory { - /** - * Directory containing compiled class metadata - * - * @var string - */ - protected $_definitionDir; - /** * Class generation dir * @@ -35,14 +24,6 @@ class DefinitionFactory */ protected $_generationDir; - /** - * Format of definitions - * - * @var string - * @deprecated - */ - protected $_definitionFormat; - /** * Filesystem Driver * @@ -51,40 +32,40 @@ class DefinitionFactory protected $_filesystemDriver; /** - * List of definition models - * - * @var array + * @var string */ - protected static $definitionClasses = \Magento\Framework\ObjectManager\Definition\Compiled::class; + protected static $definitionClasses = Compiled::class; /** * @var \Magento\Framework\Code\Generator */ protected $codeGenerator; + /** + * @var SerializerInterface + */ + private $serializer; + /** * @param DriverInterface $filesystemDriver - * @param string $definitionDir + * @param SerializerInterface $serializer * @param string $generationDir - * @param string $definitionFormat */ public function __construct( DriverInterface $filesystemDriver, - $definitionDir, - $generationDir, - $definitionFormat = null + SerializerInterface $serializer, + $generationDir ) { $this->_filesystemDriver = $filesystemDriver; - $this->_definitionDir = $definitionDir; + $this->serializer = $serializer; $this->_generationDir = $generationDir; - $this->_definitionFormat = $definitionFormat; } /** * Create class definitions * * @param mixed $definitions - * @return Runtime + * @return Compiled|Runtime */ public function createClassDefinition($definitions = false) { @@ -110,14 +91,7 @@ class DefinitionFactory */ public function createPluginDefinition() { - $path = $this->_definitionDir . '/plugins.json'; - if ($this->_filesystemDriver->isReadable($path)) { - return new \Magento\Framework\Interception\Definition\Compiled( - $this->_unpack($this->_filesystemDriver->fileGetContents($path)) - ); - } else { - return new \Magento\Framework\Interception\Definition\Runtime(); - } + return new \Magento\Framework\Interception\Definition\Runtime(); } /** @@ -127,25 +101,7 @@ class DefinitionFactory */ public function createRelations() { - $path = $this->_definitionDir . '/relations.json'; - if ($this->_filesystemDriver->isReadable($path)) { - return new \Magento\Framework\ObjectManager\Relations\Compiled( - $this->_unpack($this->_filesystemDriver->fileGetContents($path)) - ); - } else { - return new \Magento\Framework\ObjectManager\Relations\Runtime(); - } - } - - /** - * Gets supported definition formats - * - * @return array - * @deprecated - */ - public static function getSupportedFormats() - { - return []; + return new \Magento\Framework\ObjectManager\Relations\Runtime(); } /** @@ -156,7 +112,7 @@ class DefinitionFactory */ protected function _unpack($definitions) { - return json_decode($definitions); + return $this->serializer->unserialize($definitions); } /** diff --git a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php index 3861d274b52..ca086c23aef 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php +++ b/lib/internal/Magento/Framework/ObjectManager/Test/Unit/DefinitionFactoryTest.php @@ -3,118 +3,74 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Framework\ObjectManager\Test\Unit; use Magento\Framework\ObjectManager\Definition\Compiled; +use Magento\Framework\Serialize\SerializerInterface; +use Magento\Framework\Filesystem\DriverInterface; +use Magento\Framework\ObjectManager\DefinitionFactory; +use Magento\Framework\ObjectManager\Definition\Runtime; class DefinitionFactoryTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\Filesystem\DriverInterface | \PHPUnit_Framework_MockObject_MockObject + * @var DriverInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $filesystemDriverMock; + private $filesystemDriverMock; /** - * @var \Magento\Framework\ObjectManager\DefinitionFactory + * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $model; + private $serializerMock; /** - * @var string + * @var DefinitionFactory */ - protected $sampleContent; + private $definitionFactory; protected function setUp() { - $this->sampleContent = '[1,2,3]'; - $this->filesystemDriverMock = $this->getMock( - \Magento\Framework\Filesystem\Driver\File::class, - [], - [], - '', - false - ); - $this->model = new \Magento\Framework\ObjectManager\DefinitionFactory( + $this->filesystemDriverMock = $this->getMock(DriverInterface::class); + $this->serializerMock = $this->getMock(SerializerInterface::class); + $this->definitionFactory = new DefinitionFactory( $this->filesystemDriverMock, - 'DefinitionDir', - 'GenerationDir' + $this->serializerMock, + 'generation dir' ); } - public function testCreateClassDefinitionFromString() + public function testCreateClassDefinitionSerialized() { + $serializedDefinitions = 'serialized definitions'; + $definitions = [[], []]; + $this->serializerMock->expects($this->once()) + ->method('unserialize') + ->with($serializedDefinitions) + ->willReturn($definitions); $this->assertInstanceOf( - \Magento\Framework\ObjectManager\Definition\Compiled::class, - $this->model->createClassDefinition($this->sampleContent) + Compiled::class, + $this->definitionFactory->createClassDefinition($serializedDefinitions) ); } - /** - * @param string $path - * @param string $callMethod - * @param string $expectedClass - * @dataProvider createPluginsAndRelationsReadableDataProvider - */ - public function testCreatePluginsAndRelationsReadable($path, $callMethod, $expectedClass) - { - $this->filesystemDriverMock->expects($this->once())->method('isReadable') - ->with($path) - ->will($this->returnValue(true)); - $this->filesystemDriverMock->expects($this->once())->method('fileGetContents') - ->with($path) - ->will($this->returnValue($this->sampleContent)); - $this->assertInstanceOf($expectedClass, $this->model->$callMethod()); - } - - public function createPluginsAndRelationsReadableDataProvider() + public function testCreateClassDefinitionArray() { - return [ - 'relations' => [ - 'DefinitionDir/relations.json', - 'createRelations', \Magento\Framework\ObjectManager\Relations\Compiled::class, - ], - 'plugins' => [ - 'DefinitionDir/plugins.json', - 'createPluginDefinition', \Magento\Framework\Interception\Definition\Compiled::class, - ], - ]; - } - - /** - * @param string $path - * @param string $callMethod - * @param string $expectedClass - * @dataProvider createPluginsAndRelationsNotReadableDataProvider - */ - public function testCreatePluginsAndRelationsNotReadable($path, $callMethod, $expectedClass) - { - $this->filesystemDriverMock->expects($this->once())->method('isReadable') - ->with($path) - ->will($this->returnValue(false)); - $this->assertInstanceOf($expectedClass, $this->model->$callMethod()); - } - - public function createPluginsAndRelationsNotReadableDataProvider() - { - return [ - 'relations' => [ - 'DefinitionDir/relations.json', - 'createRelations', \Magento\Framework\ObjectManager\Relations\Runtime::class, - ], - 'plugins' => [ - 'DefinitionDir/plugins.json', - 'createPluginDefinition', \Magento\Framework\Interception\Definition\Runtime::class, - ], - ]; + $definitions = [[], []]; + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $this->assertInstanceOf( + Compiled::class, + $this->definitionFactory->createClassDefinition($definitions) + ); } - public function testGetSupportedFormats() + public function testCreateClassDefinition() { - $actual = \Magento\Framework\ObjectManager\DefinitionFactory::getSupportedFormats(); - $this->assertInternalType('array', $actual); - foreach ($actual as $className) { - $this->assertInternalType('string', $className); - } + $this->serializerMock->expects($this->never()) + ->method('unserialize'); + $this->assertInstanceOf( + Runtime::class, + $this->definitionFactory->createClassDefinition() + ); } } diff --git a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php index 3e2620fc208..342b26a22e3 100644 --- a/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php +++ b/setup/src/Magento/Setup/Module/Di/Compiler/Config/Writer/Filesystem.php @@ -46,7 +46,7 @@ class Filesystem implements WriterInterface $this->initialize(); file_put_contents( - $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.json', + $this->directoryList->getPath(DirectoryList::DI) . '/' . $key . '.ser', $this->getSerializer()->serialize($config) ); } -- GitLab