-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ObjectManager Config Writer to Framework
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
Showing
9 changed files
with
160 additions
and
11 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
...integration/testsuite/Magento/Framework/App/ObjectManager/ConfigWriter/FilesystemTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
lib/internal/Magento/Framework/App/ObjectManager/ConfigWriter/Filesystem.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
lib/internal/Magento/Framework/App/ObjectManager/ConfigWriterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters