/** * @param string $filename * @param string $configJson * @param bool|null $merge */ public function setConfig($filename, $configJson, $merge = null) { $merge = (bool) $merge; $configJson = (object) CM_Util::jsonDecode($configJson); $configFile = new CM_File(DIR_ROOT . 'resources/config/' . $filename . '.php'); $config = new CM_Config_Node(); if ($merge && $configFile->exists()) { $config->extendWithFile($configFile); } $config->extendWithConfig($configJson); $configStr = $config->exportAsString('$config'); $indentation = ' '; $indent = function ($content) use($indentation) { return preg_replace('/(:?^|[\\n])/', '$1' . $indentation, $content); }; $configFile->ensureParentDirectory(); $configFile->write(join(PHP_EOL, ['<?php', '// This is autogenerated config file. You should not change it manually.', '', 'return function (CM_Config_Node $config) {', $indent($configStr), '};', ''])); $this->_getStreamOutput()->writeln('Created `' . $configFile->getPath() . '`'); }
public function testExportAsString() { $node = new CM_Config_Node(); $node->foo->bar->foo = 1; $node->foo->bar->bar = '1'; $node->foo->bar->array = ['foo' => '1', 'CM_Config_NodeTest::TEST' => 2, 'CM_Config_NodeTest::NONEXISTENT' => 3, 'NonexistentClass::FOO' => 4, 'CM_Config_NodeTest::TEST_0' => 5, 'CM_Config_NodeTest::TEST_NULL' => 6]; $node->foo->bar->boolean = false; $expected = <<<'EOD' $config->foo->bar->foo = 1; $config->foo->bar->bar = '1'; $config->foo->bar->array = []; $config->foo->bar->array['foo'] = '1'; $config->foo->bar->array[CM_Config_NodeTest::TEST] = 2; $config->foo->bar->array['CM_Config_NodeTest::NONEXISTENT'] = 3; $config->foo->bar->array['NonexistentClass::FOO'] = 4; $config->foo->bar->array[CM_Config_NodeTest::TEST_0] = 5; $config->foo->bar->array['CM_Config_NodeTest::TEST_NULL'] = 6; $config->foo->bar->boolean = false; EOD; $this->assertSame($expected, $node->exportAsString('$config')); }