mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
DOC Injector syntax docs update (#10221)
This commit is contained in:
parent
5c54276b6f
commit
6d90eec0c8
@ -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).
|
||||
|
||||
@ -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.
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user