Skip to content

Commit

Permalink
feat: Use PropertyAccess Component syntax for nested variables access
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo D committed Mar 1, 2024
1 parent 82f4710 commit 11c5955
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@
/my-app.*
/var/
/vendor/
.idea/
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,7 @@
* Add a `yaml_dump()` function to dump any PHP value to a YAML string
* Add a `yaml_parse()` function to parse a YAML string to a PHP value
* Remove the default timeout of 60 seconds from the Context
* Add possibility to use [Property Access Component](https://symfony.com/doc/current/components/property_access.html) syntax for getting variable from context.

## 0.13.1 (2024-02-27)

Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -43,6 +43,7 @@
"symfony/http-client": "^6.4.3",
"symfony/monolog-bridge": "^6.4.3",
"symfony/process": "^6.4.3",
"symfony/property-access": "^6.4",
"symfony/string": "^6.4.3",
"symfony/translation-contracts": "^3.4.1",
"symfony/var-dumper": "^6.4.3",
Expand Down
164 changes: 162 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions doc/getting-started/context.md
Expand Up @@ -53,13 +53,15 @@ function foo(): void
{
$foobar = variable('foobar', 'default value');

// Same as:
$context = context();
try {
$foobar = $context['foobar'];
} catch (\OutOfBoundsException) {
$foobar = 'default value;
}
// Same as:
$foobar = $context['foobar'] ?? 'default value';

// Getting nested values
$foobar = variable('[foo][bar][baz]', 'default value');

// Same as:
$foobar = $context['foo']['bar']['baz'] ?? 'default value';
}
```

Expand Down
4 changes: 4 additions & 0 deletions examples/context.php
Expand Up @@ -21,6 +21,9 @@ function defaultContext(): Context
'name' => 'my_default',
'production' => false,
'foo' => 'bar',
'nested' => [
'key' => 'nested_value',
],
]);
}

Expand Down Expand Up @@ -83,6 +86,7 @@ function contextInfo(): void
echo 'Production? ' . (variable('production', false) ? 'yes' : 'no') . "\n";
echo "verbosity: {$context->verbosityLevel->value}\n";
echo 'context: ' . variable('foo', 'N/A') . "\n";
echo 'nested key: ' . variable('[nested][key]', 'N/A') . "\n";
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Attribute/AsCommandArgument.php
Expand Up @@ -5,7 +5,7 @@
abstract class AsCommandArgument
{
public function __construct(
public readonly string|null $name = null,
public readonly ?string $name = null,
) {
}
}
2 changes: 1 addition & 1 deletion src/Attribute/AsOption.php
Expand Up @@ -12,7 +12,7 @@ class AsOption extends AsCommandArgument
public function __construct(
?string $name = null,
public readonly string|array|null $shortcut = null,
public readonly int|null $mode = null,
public readonly ?int $mode = null,
public readonly string $description = '',
public readonly array $suggestedValues = [],
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Attribute/AsTask.php
Expand Up @@ -11,7 +11,7 @@ class AsTask
*/
public function __construct(
public string $name = '',
public string|null $namespace = null,
public ?string $namespace = null,
public string $description = '',
public array $aliases = [],
public array $onSignals = [],
Expand Down
4 changes: 2 additions & 2 deletions src/Context.php
Expand Up @@ -17,7 +17,7 @@ public function __construct(
?string $currentDirectory = null,
public readonly bool $tty = false,
public readonly bool $pty = true,
public readonly float|null $timeout = null,
public readonly ?float $timeout = null,
public readonly bool $quiet = false,
public readonly bool $allowFailure = false,
public readonly bool $notify = false,
Expand Down Expand Up @@ -132,7 +132,7 @@ public function withPty(bool $pty = true): self
);
}

public function withTimeout(float|null $timeout): self
public function withTimeout(?float $timeout): self
{
return new self(
$this->data,
Expand Down
8 changes: 5 additions & 3 deletions src/ContextRegistry.php
Expand Up @@ -2,6 +2,8 @@

namespace Castor;

use Symfony\Component\PropertyAccess\PropertyAccess;

/** @internal */
class ContextRegistry
{
Expand Down Expand Up @@ -113,11 +115,11 @@ public function getVariable(string $key, mixed $default = null): mixed
{
$context = $this->getCurrentContext();

if (!isset($context[$key])) {
return $default;
if (str_starts_with($key, '[')) {
return PropertyAccess::createPropertyAccessor()->getValue($context, $key) ?? $default;
}

return $context[$key];
return $context[$key] ?? $default;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/SectionOutput.php
Expand Up @@ -14,7 +14,7 @@ class SectionOutput

private OutputInterface|ConsoleSectionOutput $consoleOutput;

private ConsoleOutput|null $mainOutput;
private ?ConsoleOutput $mainOutput;

/** @var \SplObjectStorage<Process, SectionDetails> */
private \SplObjectStorage $sections;
Expand Down
Expand Up @@ -2,3 +2,4 @@ context name: dynamic
Production? no
verbosity: 1
context: baz
nested key: N/A
Expand Up @@ -2,3 +2,4 @@ context name: my_default
Production? no
verbosity: 2
context: bar
nested key: nested_value
Expand Up @@ -2,3 +2,4 @@ context name: path
Production? yes
verbosity: 1
context: bar
nested key: N/A
Expand Up @@ -2,3 +2,4 @@ context name: production
Production? yes
verbosity: 1
context: bar
nested key: nested_value
Expand Up @@ -2,3 +2,4 @@ context name: run
Production? no
verbosity: 1
context: no defined
nested key: N/A
1 change: 1 addition & 0 deletions tests/Examples/Generated/ContextContextTest.php.output.txt
Expand Up @@ -2,3 +2,4 @@ context name: my_default
Production? no
verbosity: 1
context: bar
nested key: nested_value
Expand Up @@ -2,4 +2,5 @@ context name: dynamic
Production? no
verbosity: -1
context: bar
bar
nested key: N/A
bar

0 comments on commit 11c5955

Please sign in to comment.