https://github.com/adamwojs/ezplatform-location-reference
Domain Specific Language for referencing eZ Platform locations
https://github.com/adamwojs/ezplatform-location-reference
domain-specific-language dsl expression-language ezplatform ezplatform-bundle
Last synced: 2 months ago
JSON representation
Domain Specific Language for referencing eZ Platform locations
- Host: GitHub
- URL: https://github.com/adamwojs/ezplatform-location-reference
- Owner: adamwojs
- License: mit
- Created: 2019-06-30T13:33:30.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-21T17:52:44.000Z (almost 6 years ago)
- Last Synced: 2025-02-05T05:20:12.134Z (4 months ago)
- Topics: domain-specific-language, dsl, expression-language, ezplatform, ezplatform-bundle
- Language: PHP
- Homepage:
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ezplatform-location-reference
## Problem
The common case in the eZ Platform configuration is referencing to some location, usually using Location ID e.g.
* Content root for Site Access (https://doc.ezplatform.com/en/latest/guide/multisite/#location_id)
* Folder used to store Image Assets (https://doc.ezplatform.com/en/latest/api/field_type_reference/#configuration)There are several issues in this approaches:
* Location IDs differ between environments
* Using [Magic numbers](https://en.wikipedia.org/wiki/Magic_number_%28programming%29) is a [code smell](https://en.wikipedia.org/wiki/Code_smell)This package introduces [Domain Specific Language](https://en.wikipedia.org/wiki/Domain-specific_language), based on Symfony [Expression Language](https://symfony.com/doc/current/components/expression_language.html) component, allowing to refer locations using meaningful and descriptive expressions.
## Usage
### Resolve location references
Location reference expressions could be resolved using `LocationReferenceResolver` e.g.
```php
locationReferenceResolver = $locationReferenceResolver;
}public function foo(): void
{
$location = $this->locationReferenceResolver->resolve(
'remote_id("babe4a915b1dd5d369e79adb9d6c0c6a")'
);// ...
}
}
```### Retrieving location reference from SiteAccess aware configuration
Location references could be retrieved from the SiteAccess aware configuration
using `LocationConfigResolver`:```php
locationConfigResolver = $locationConfigResolver;
}// ...
public function foo(): void
{
// Get reference to location
$reference = $this->locationConfigResolver->getLocationReference('content.tree_root.location_id');
// Resolve location reference
$location = $reference->getLocation();
// Return null if location is not available (not found or unauthorized)
$location = $reference->getLocationOrNull();
// Return $defaultLocation if location is not available (not found or unauthorized)
$location = $reference->getLocationOrDefault($defaultLocation);
// Get reference and immediately resolve
$location = $this->locationConfigResolver->getLocation('fieldtypes.ezimageasset.parent_location');
}
}
```### Available functions
| Function | Description | Example |
|-------------|------------------------------|-------------------------------------------------|
| `root` | Load root location | `root()` |
| `parent` | Load parent location | `parent(local_id(54))` |
| `local_id` | Load location by ID | `local_id(54)` |
| `remote_id` | Load location by remote ID | `remote_id("babe4a915b1dd5d369e79adb9d6c0c6a")` |
| `path` | Load location by path string | `path("/1/2/54")` |
| `named` | Load named reference | `named("MEDIA")` |