ezsystems/ezpublish-kernel
eZ Publish Kernel is the core of the eZ Publish/eZ Platform CMS, providing the content repository, field types, search integration, and services for building and extending PHP-based content applications with a modular, API-driven architecture.
Before 5.4, if one wanted to implement a service needing siteaccess-aware settings (e.g. language settings),
they needed to inject the whole ConfigResolver (ezpublish.config.resolver) and get the needed settings from it.
This was not very convenient nor explicit. Goal of this feature is to allow developers to inject these dynamic settings explicitly from their service definition (yml, xml, annotation...).
Static container parameters follows the %<parameter_name>% syntax in Symfony.
Dynamic parameters have the following: $<parameter_name>[; <namespace>[; <scope>]]$, default namespace being ezsettings,
and default scope being current siteaccess.
For more information, see ConfigResolver documentation.
This feature also introduces a DynamicSettingParser service that can be used for adding support of the dynamic settings syntax.
This service has ezpublish.config.dynamic_setting.parser for ID and implements
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\SiteAccessAware\DynamicSettingParserInterface.
A few limitations still remain:
config.yml or ezpublish.yml)
as they are meant primarily for parameter injection in services.onKernelRequest method (or equivalent).Defining a simple service needing languages parameter (i.e. prioritized languages).
Note: Internally,
languagesparameter is defined asezsettings.<siteaccess_name>.languages,ezsettingsbeing eZ internal namespace.
Before
parameters:
acme_test.my_service.class: Acme\TestBundle\MyServiceClass
services:
acme_test.my_service:
class: %acme_test.my_service.class%
arguments: [[@ezpublish](https://github.com/ezpublish).config.resolver]
namespace Acme\TestBundle;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
class MyServiceClass
{
/**
* Prioritized languages
*
* [@var](https://github.com/var) array
*/
private $languages;
public function __construct( ConfigResolverInterface $configResolver )
{
$this->languages = $configResolver->getParameter( 'languages' );
}
}
After, with setter injection (preferred)
parameters:
acme_test.my_service.class: Acme\TestBundle\MyServiceClass
services:
acme_test.my_service:
class: %acme_test.my_service.class%
calls:
- [setLanguages, ["$languages$"]]
namespace Acme\TestBundle;
class MyServiceClass
{
/**
* Prioritized languages
*
* [@var](https://github.com/var) array
*/
private $languages;
public function setLanguages( array $languages = null )
{
$this->languages = $languages;
}
}
Important: Ensure you always add
nullas a default value, especially if the argument is type-hinted.
After, with constructor injection
parameters:
acme_test.my_service.class: Acme\TestBundle\MyServiceClass
services:
acme_test.my_service:
class: %acme_test.my_service.class%
arguments: ["$languages$"]
namespace Acme\TestBundle;
class MyServiceClass
{
/**
* Prioritized languages
*
* [@var](https://github.com/var) array
*/
private $languages;
public function __construct( array $languages )
{
$this->languages = $languages;
}
}
Tip: Setter injection for dynamic settings should always be preferred, as it makes it possible to update your services that depend on them when ConfigResolver is updating its scope (e.g. when previewing content in a given SiteAccess). Constructor injection will make your service be reset in that case.
However, only one dynamic setting should be injected by setter.
parameters:
acme_test.my_service.class: Acme\TestBundle\MyServiceClass
# "acme" is our parameter namespace.
# Null is the default value.
acme.default.some_parameter: ~
acme.ezdemo_site.some_parameter: foo
acme.ezdemo_site_admin.some_parameter: bar
services:
acme_test.my_service:
class: %acme_test.my_service.class%
# The following argument will automatically resolve to the right value, depending on the current SiteAccess.
# We specify "acme" as the namespace we want to use for parameter resolving.
calls:
- [setSomeParameter, ["$some_parameter;acme$"]]
namespace Acme\TestBundle;
class MyServiceClass
{
private $myParameter;
public function setSomeParameter( $myParameter = null )
{
// Will be "foo" for ezdemo_site, "bar" for ezdemo_site_admin, or null if another SiteAccess.
$this->myParameter = $myParameter;
}
}
How can I help you explore Laravel packages today?