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 cf1809f
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 12 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
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
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 cf1809f

Please sign in to comment.