Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CodeExporter, fixes #30 #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions src/Code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);
namespace Brick\VarExporter;

final class Code implements CodeInterface {

/**
* @var string[]
*/
private array $lines;

/**
* @param string[] $lines
*/
private function __construct(array $lines) {
$this->lines = $lines;
}

public static function create(string ...$lines): self {
return new self($lines);
}

public function toLines(): array
{
return $this->lines;
}

}
13 changes: 13 additions & 0 deletions src/CodeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);
namespace Brick\VarExporter;

interface CodeInterface {

/**
* @return string[]
*/
public function toLines(): array;

}
9 changes: 9 additions & 0 deletions src/Internal/GenericExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ final class GenericExporter
*/
public $trailingCommaInArray;

/**
* @psalm-readonly
*
* @var bool
*/
public $allowCode;

/**
* @psalm-readonly
*
Expand All @@ -83,6 +90,7 @@ final class GenericExporter

public function __construct(int $options, int $indentLevel = 0)
{
$this->objectExporters[] = new ObjectExporter\CodeExporter($this);
$this->objectExporters[] = new ObjectExporter\StdClassExporter($this);

if (! ($options & VarExporter::NO_CLOSURES)) {
Expand Down Expand Up @@ -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;
}
Expand Down
30 changes: 30 additions & 0 deletions src/Internal/ObjectExporter/CodeExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Brick\VarExporter\Internal\ObjectExporter;

use Brick\VarExporter\Internal\ObjectExporter;
use Brick\VarExporter\CodeInterface;

/**
* Handles instances of LiteralInterface.
*
* @internal This class is for internal use, and not part of the public API. It may change at any time without warning.
*/
final class CodeExporter extends ObjectExporter
{

public function supports(\ReflectionObject $reflectionObject): bool
{
return $this->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();
}

}
5 changes: 5 additions & 0 deletions src/VarExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ final class VarExporter
*/
public const INLINE_ARRAY = 1 << 11;

/**
* Any value implementing CodeInterface will print a literal expression.
*/
public const ALLOW_CODE = 1 << 12;

/**
* @param mixed $var The variable to export.
* @param int $options A bitmask of options. Possible values are `VarExporter::*` constants.
Expand Down