diff --git a/README.md b/README.md index 31d19d4..2f9d019 100644 --- a/README.md +++ b/README.md @@ -505,6 +505,28 @@ try { } ``` +## Literal code expressions + +```php +use Brick\VarExporter\VarExporter; +use Brick\Code; + +echo VarExporter::export([ + 'literal_expression' => Code::create("$app_root", ". '/../private'"); +], VarExporter::ALLOW_CODE); +``` + +This code will output: + +```php +[ + 'literal_expression' => $app_root + . '/../private', +] +``` + +Also, you can implement CodeInterface in your custom code. + ## Limitations - Exporting internal classes other than `stdClass` and `Closure`, and classes implementing `__set_state()` (most notably DateTime classes) is currently not supported. `VarExporter` will throw an `ExportException` if it finds one. diff --git a/src/Code.php b/src/Code.php new file mode 100644 index 0000000..dcb7847 --- /dev/null +++ b/src/Code.php @@ -0,0 +1,29 @@ +lines = $lines; + } + + public static function create(string ...$lines): self { + return new self($lines); + } + + public function toLines(): array + { + return $this->lines; + } + +} diff --git a/src/CodeInterface.php b/src/CodeInterface.php new file mode 100644 index 0000000..84219e4 --- /dev/null +++ b/src/CodeInterface.php @@ -0,0 +1,13 @@ +objectExporters[] = new ObjectExporter\CodeExporter($this); $this->objectExporters[] = new ObjectExporter\StdClassExporter($this); if (! ($options & VarExporter::NO_CLOSURES)) { @@ -113,6 +121,7 @@ public function __construct(int $options, int $indentLevel = 0) $this->inlineScalarList = (bool) ($options & VarExporter::INLINE_SCALAR_LIST); $this->closureSnapshotUses = (bool) ($options & VarExporter::CLOSURE_SNAPSHOT_USES); $this->trailingCommaInArray = (bool) ($options & VarExporter::TRAILING_COMMA_IN_ARRAY); + $this->allowCode = (bool) ($options & VarExporter::ALLOW_CODE); $this->indentLevel = $indentLevel; } diff --git a/src/Internal/ObjectExporter/CodeExporter.php b/src/Internal/ObjectExporter/CodeExporter.php new file mode 100644 index 0000000..74c1ebe --- /dev/null +++ b/src/Internal/ObjectExporter/CodeExporter.php @@ -0,0 +1,30 @@ +exporter->allowCode + && $reflectionObject->implementsInterface(CodeInterface::class); + } + + public function export(object $object, \ReflectionObject $reflectionObject, array $path, array $parentIds): array + { + assert($object instanceof CodeInterface); + return $object->toLines(); + } + +} diff --git a/src/VarExporter.php b/src/VarExporter.php index 13fc356..873d12e 100644 --- a/src/VarExporter.php +++ b/src/VarExporter.php @@ -54,6 +54,11 @@ final class VarExporter */ public const INLINE_SCALAR_LIST = 1 << 7; + /** + * Any value implementing CodeInterface will print a literal expression. + */ + public const ALLOW_CODE = 1 << 8; + /** * @deprecated Please use INLINE_SCALAR_LIST instead. */