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

Injector syntax docs update #10221

Merged
Merged
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
38 changes: 37 additions & 1 deletion docs/en/02_Developer_Guides/05_Extending/05_Injector.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,31 @@ SilverStripe\Core\Injector\Injector:
- [ pushHandler, [ %$DefaultHandler ] ]
```

## Using constants as variables
### Special YML Syntax

You can use the special `%$` prefix in the configuration yml to fetch items via the Injector. For example:

```yml
App\Services\MediumQueuedJobService:
properties:
queueRunner: '%$App\Tasks\Engines\MediumQueueAsyncRunner'
```

It is equivalent of calling `Injector::get()->instance(MediumQueueAsyncRunner::class)` and assigning the result to the `MediumQueuedJobService::queueRunner` property. This can be useful as these properties can easily updated if provided in a module or be changed for unit testing. It can also be used to provide constructor arguments such as [this example from the assets module](https://github.com/silverstripe/silverstripe-assets/blob/1/_config/asset.yml):

```yml
SilverStripe\Core\Injector\Injector:
# Define the secondary adapter for protected assets
SilverStripe\Assets\Flysystem\ProtectedAdapter:
class: SilverStripe\Assets\Flysystem\ProtectedAssetAdapter
# Define the secondary filesystem for protected assets
League\Flysystem\Filesystem.protected:
class: League\Flysystem\Filesystem
constructor:
FilesystemAdapter: '%$SilverStripe\Assets\Flysystem\ProtectedAdapter'
```

## Using constants and environment variables

Any of the core constants can be used as a service argument by quoting with back ticks "`". Please ensure you also quote the entire value (see below).

Expand All @@ -152,6 +176,18 @@ CachingService:
CacheDir: '`TEMP_DIR`'
```

Environment variables can be used in the same way:

```yml
App\Services\MyService:
class: App\Services\MyService
constructor:
baseURL: '`SS_API_URI`'
credentials:
id: '`SS_API_CLIENT_ID`'
secret: '`SS_API_CLIENT_SECRET`'
```

Note: undefined variables will be replaced with null.


Expand Down