Skip to content

Commit

Permalink
Move ObjectManager Config Writer to Framework
Browse files Browse the repository at this point in the history
The ObjectManager Config Writer class currently lives in the Setup module and
is needed in the Framework when writing the interception config to the
filesystem during compilation. This commit adds the classes to the Framework
and replaces any existing usage with the new interface/class.
  • Loading branch information
pmclain committed Oct 25, 2018
1 parent 86a30b6 commit 3219169
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\App\ObjectManager\ConfigWriter;

class FilesystemTest extends \PHPUnit\Framework\TestCase
{
const CACHE_KEY = 'filesystemtest';

/**
* @var \Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem
*/
private $configWriter;

/**
* @var \Magento\Framework\App\ObjectManager\ConfigLoader
*/
private $configReader;

protected function setUp()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$this->configWriter = $objectManager->create(
\Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class
);
$this->configReader = $objectManager->create(
\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::class
);
}

public function testWrite()
{
$sampleData = [
'classA' => true,
'classB' => false,
];

$this->configWriter->write(self::CACHE_KEY, $sampleData);
$this->assertEquals($sampleData, $this->configReader->load(self::CACHE_KEY));
}

public function testOverwrite()
{
$this->configWriter->write(self::CACHE_KEY, ['hello' => 'world']);

$sampleData = [
'classC' => false,
'classD' => true,
];

$this->configWriter->write(self::CACHE_KEY, $sampleData);
$this->assertEquals($sampleData, $this->configReader->load(self::CACHE_KEY));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\App\ObjectManager\ConfigWriter;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager\ConfigWriterInterface;

/**
* @inheritdoc
*/
class Filesystem implements ConfigWriterInterface
{
/**
* @var DirectoryList
*/
private $directoryList;

/**
* @param DirectoryList $directoryList
*/
public function __construct(
DirectoryList $directoryList
) {
$this->directoryList = $directoryList;
}

/**
* Writes config in storage
*
* @param string $key
* @param array $config
* @return void
*/
public function write(string $key, array $config)
{
$this->initialize();
$configuration = sprintf('<?php return %s;', var_export($config, true));
file_put_contents(
$this->directoryList->getPath(DirectoryList::GENERATED_METADATA) . '/' . $key . '.php',
$configuration
);
}

/**
* Initializes writer
*
* @return void
*/
private function initialize()
{
if (!file_exists($this->directoryList->getPath(DirectoryList::GENERATED_METADATA))) {
mkdir($this->directoryList->getPath(DirectoryList::GENERATED_METADATA));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Framework\App\ObjectManager;

/**
* Write compiled object manager configuration to storage
*/
interface ConfigWriterInterface
{
/**
* Writes config in storage
*
* @param string $key
* @param array $config
* @return void
*/
public function write(string $key, array $config);
}
8 changes: 4 additions & 4 deletions lib/internal/Magento/Framework/Interception/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Config implements \Magento\Framework\Interception\ConfigInterface
private $serializer;

/**
* @var \Magento\Setup\Module\Di\Compiler\Config\Writer\Filesystem
* @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
*/
private $configWriter;

Expand All @@ -99,7 +99,7 @@ class Config implements \Magento\Framework\Interception\ConfigInterface
* @param \Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions
* @param string $cacheId
* @param SerializerInterface|null $serializer
* @param \Magento\Setup\Module\Di\Compiler\Config\Writer\Filesystem $configWriter
* @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
* @param \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
*/
public function __construct(
Expand All @@ -111,7 +111,7 @@ public function __construct(
\Magento\Framework\ObjectManager\DefinitionInterface $classDefinitions,
$cacheId = 'interception',
SerializerInterface $serializer = null,
\Magento\Setup\Module\Di\Compiler\Config\Writer\Filesystem $configWriter = null,
\Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter = null,
\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader = null
) {
$this->_omConfig = $omConfig;
Expand All @@ -124,7 +124,7 @@ public function __construct(
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(Serialize::class);
$this->configWriter = $configWriter ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Setup\Module\Di\Compiler\Config\Writer\Filesystem::class);
->get(\Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class);
$this->compiledLoader = $compiledLoader ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::class);
$intercepted = $this->loadIntercepted();
Expand Down
4 changes: 2 additions & 2 deletions setup/src/Magento/Setup/Console/Command/DiCompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ private function configureObjectManager(OutputInterface $output)
{
$this->objectManager->configure(
[
'preferences' => [\Magento\Setup\Module\Di\Compiler\Config\WriterInterface::class =>
\Magento\Setup\Module\Di\Compiler\Config\Writer\Filesystem::class,
'preferences' => [\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class =>
\Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem::class,
], \Magento\Setup\Module\Di\Compiler\Config\ModificationChain::class => [
'arguments' => [
'modificationsList' => [
Expand Down
6 changes: 3 additions & 3 deletions setup/src/Magento/Setup/Module/Di/App/Task/Operation/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Area implements OperationInterface
private $configReader;

/**
* @var Config\WriterInterface
* @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
*/
private $configWriter;

Expand All @@ -46,15 +46,15 @@ class Area implements OperationInterface
* @param App\AreaList $areaList
* @param \Magento\Setup\Module\Di\Code\Reader\Decorator\Area $areaInstancesNamesList
* @param Config\Reader $configReader
* @param Config\WriterInterface $configWriter
* @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
* @param \Magento\Setup\Module\Di\Compiler\Config\ModificationChain $modificationChain
* @param array $data
*/
public function __construct(
App\AreaList $areaList,
\Magento\Setup\Module\Di\Code\Reader\Decorator\Area $areaInstancesNamesList,
Config\Reader $configReader,
Config\WriterInterface $configWriter,
\Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter,
Config\ModificationChain $modificationChain,
$data = []
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Setup\Module\Di\Compiler\Config\WriterInterface;

/**
* @deprecated Moved to Framework to allow broader reuse
* @see \Magento\Framework\App\ObjectManager\ConfigWriter\Filesystem
*/
class Filesystem implements WriterInterface
{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

/**
* Interface \Magento\Setup\Module\Di\Compiler\Config\WriterInterface
*
* @deprecated Moved to Framework to allow broader reuse
* @see \Magento\Framework\App\ObjectManager\ConfigWriterInterface
*/
interface WriterInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function setUp()
$this->configReaderMock = $this->getMockBuilder(\Magento\Setup\Module\Di\Compiler\Config\Reader::class)
->disableOriginalConstructor()
->getMock();
$this->configWriterMock = $this->getMockBuilder(\Magento\Setup\Module\Di\Compiler\Config\WriterInterface::class)
$this->configWriterMock = $this->getMockBuilder(\Magento\Framework\App\ObjectManager\ConfigWriterInterface::class)
->disableOriginalConstructor()
->getMock();
$this->configChain = $this->getMockBuilder(\Magento\Setup\Module\Di\Compiler\Config\ModificationChain::class)
Expand Down

0 comments on commit 3219169

Please sign in to comment.