Matthias Lantsch(2 years, 2 months ago)
Implement php config dumper that preserves class names as in code references
Browse Filesdiff --git a/f7e27cabc00df27a2ba3303686605770165faadb b/09e19c6b2b928ed90359db1263fd745336ed98e4
index f7e27ca..09e19c6 100644
--- a/f7e27cabc00df27a2ba3303686605770165faadb
+++ b/09e19c6b2b928ed90359db1263fd745336ed98e4
@@ -14,19 +14,19 @@
"myclabs/php-enum": "^1.7",
"psr/container": "1.0.0"
},
- "provide": {
- "psr/container-implementation": "1.0.0"
- },
"require-dev": {
"holonet/hdev": "~1.0.0",
"phpunit/phpunit": "^8.4.1"
},
- "extra": {
- "branch-alias": {
- "dev-develop": "1.5.x-dev",
- "dev-master": "1.4.x-dev"
- }
+ "provide": {
+ "psr/container-implementation": "1.0.0"
},
+ "repositories": [
+ {
+ "type": "composer",
+ "url": "https://holonet.easylabs.ch/hgit/composer/"
+ }
+ ],
"autoload": {
"psr-4": {
"holonet\\common\\": "src/"
@@ -40,12 +40,12 @@
"holonet\\common\\tests\\": "tests/"
}
},
- "repositories": [
- {
- "type": "composer",
- "url": "https://holonet.easylabs.ch/hgit/composer/"
+ "extra": {
+ "branch-alias": {
+ "dev-develop": "1.5.x-dev",
+ "dev-master": "1.4.x-dev"
}
- ],
+ },
"scripts": {
"fix": [
"@composer normalize --diff",
<?php
/**
* This file is part of the holonet common library
* (c) Matthias Lantsch.
*
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @author Matthias Lantsch <[email protected]>
*/
namespace holonet\common;
/**
* Pretty print a php variable into parsable php code.
*/
class PhpExporter {
public function export($data): string {
$ret = var_export($data, true);
// replace the space after the array keyword
$ret = str_replace('array (', 'array(', $ret);
// replace new lines after array keys
$ret = preg_replace("/' =>[\\s]*array\\(/", '\' => array(', $ret);
// replace class names with constants
$data = (array)$data;
array_walk_recursive($data, function ($val) use (&$ret): void {
if (is_scalar($val) && class_exists((string)$val)) {
$ret = str_replace(str_replace('\\', '\\\\', "'{$val}'"), "{$val}::class", $ret);
}
});
return "return {$ret};";
}
public function exportFile($data): string {
return "<?php\n\n{$this->export($data)}";
}
}
<?php
/**
* This file is part of the hdev common library package
* (c) Matthias Lantsch.
*
* @license http://www.wtfpl.net/ Do what the fuck you want Public License
* @author Matthias Lantsch <[email protected]>
*/
namespace holonet\common\tests;
use holonet\common\Noun;
use holonet\common\PhpExporter;
use PHPUnit\Framework\TestCase;
/**
* @covers \holonet\common\PhpExporter
*
* @internal
*
* @small
*/
class PhpExporterTest extends TestCase {
public function testEval(): void {
$exporter = new PhpExporter();
$data = array(
'toplevel' => array(
'sublevel' => array(
'class' => \holonet\common\Noun::class
)
)
);
$exported = $exporter->export($data);
$this->assertSame($data, eval($exported));
}
public function testExportClassname(): void {
$exporter = new PhpExporter();
$val = Noun::class;
$exported = $exporter->export($val);
$this->assertFalse(str_contains($exported, "'"));
}
}