Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Experimental support for use of expressions in custom variables #78

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
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ You can use [Composer](https://getcomposer.org/) to install the bundle to your p
composer require --dev theofidry/psysh-bundle
```

Then, enable the bundle by updating your `app/AppKernel.php` file to enable the bundle:
Then, enable the bundle by updating your `app/AppKernel.php` file to enable the bundle:
(not needed on symfony 5, bundle is automaticaly registred in `config/bundles.php`)
```php
<?php
Expand Down Expand Up @@ -115,17 +115,21 @@ services:
### Adding custom variables
It is possible to add custom variables to the shell via configuration.
Variables can be of any type, container parameters references (e.g. `%kernel.debug%`) or even services
(prefixed with `@`, e.g. `"@my_service"`).
(prefixed with `@`, e.g. `"@my_service"`). An arbitrary evaluated expression can be specified as a
string enclosed with `{` and `}`, though keep in mind that malformed expressions will prevent Psysh
from loading. In an evaluated expression, the special token `%containerId` can be used to access the
container itself, by using `service('%containerId')`.

```yaml
# app/config/config_dev.yml
# config/packages/psysh.yaml

psysh:
variables:
foo: bar
router: "@router"
some: [thing, else]
debug: "%kernel.debug%"
doctrine: "{service('%containerId').get('doctrine')}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't @=service('%containerId').get('doctrine') be enough here?

```

Now if you run `php app/console psysh` and then `ls`, you will see the variables `$foo`, `$router`, `$some` and `$debug`,
Expand Down
14 changes: 10 additions & 4 deletions src/DependencyInjection/PsyshExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use function array_merge;
use function is_string;
use function preg_replace;
use function sprintf;
use function strpos;
use function substr;
Expand Down Expand Up @@ -50,16 +51,21 @@ public function load(array $configs, ContainerBuilder $container): void

$config = $this->processConfiguration(new Configuration(), $configs);

$containerId = 'test.service_container';

foreach ($config['variables'] as $name => $value) {
if (is_string($value) && strpos($value, '@') === 0) {
$value = new Reference(substr($value, 1));
if (is_string($value)) {
if (strpos($value, '@') === 0) {
$value = new Reference(substr($value, 1));
} else if (strpos($value, '{') === 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above I think if we are to introduce support for expressions we should we the "standard" expression? Or maybe I'm confusing with something else... Maybe OverblogGraphQLBundle YAML config

$expression = preg_replace('/%containerId\b/', $containerId, substr($value, 1, -1));
$value = new Expression($expression);
}
}

$config['variables'][$name] = $value;
}

$containerId = 'test.service_container';

$container
->findDefinition('psysh.shell')
->addMethodCall(
Expand Down