adamwojs/ezplatform-location-reference

eZ Platform Location Reference

v0.2.0 2019-08-21 17:01 UTC

This package is not auto-updated.

Last update: 2024-09-13 09:40:03 UTC


README

问题

eZ Platform配置中的常见情况是引用某个位置,通常使用位置ID,例如:

这种方法的几个问题

此包引入了基于 Symfony 表达式语言 组件的 领域特定语言,允许使用有意义的描述性表达式来引用位置。

用法

解析位置引用

可以使用 LocationReferenceResolver 解析位置引用表达式,例如:

<?php

namespace App\Service; 

class FooService 
{
    /**
     * @var \AdamWojs\EzPlatformLocationReference\LocationReferenceResolverInterface  
     */
    private $locationReferenceResolver;

    public function __construct(LocationReferenceResolverInterface $locationReferenceResolver)
    {
        $this->locationReferenceResolver = $locationReferenceResolver;
    }

    public function foo(): void
    {
        $location = $this->locationReferenceResolver->resolve(
            'remote_id("babe4a915b1dd5d369e79adb9d6c0c6a")'
        );

        // ...
    }
}

从 SiteAccess 意识的配置中检索位置引用

可以使用 LocationConfigResolver 从 SiteAccess 意识的配置中检索位置引用

<?php 

interface LocationConfigResolverInterface
{
    public function getLocation(string $name, ?string $namespace = null, ?string $scope = null): Location;

    public function getLocationReference(string $name, ?string $namespace = null, ?string $scope = null): LocationReference;
}

getLocationgetLocationReference 方法的参数与 \eZ\Publish\Core\MVC\ConfigResolverInterface::getParameter 的参数完全相同。

示例

<?php 

class BarService 
{
    /**
     * @var \AdamWojs\EzPlatformLocationReference\ConfigResolver\LocationConfigResolverInterface  
     */
    private $locationConfigResolver;

    public function __construct(LocationConfigResolverInterface $locationConfigResolver)
    {
        $this->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');
    }
}

可用函数