Hello
I have created a custom rule. It generally seems to work (admin part is working, I get the right config from admin, the rule is constructed and match
is called). Nice!
For the evaluation of my rule (the implementation of match
) I need to use some services (services from my own plugin, the config of my plugin, some repositories, …). How can I get those services?
-
Injection in
__construct
with „services.xml“ does not work! Yes, I receive the services as arguments to__ construct
but theprivate $someService
variables are overridden somewhere after having been set in the constructor. Maybe this happens somewhere in the core where the values from the config form in the admin are set. All these variables arenull
whenmatch
is executed even though I set them to a non-null
value in the constructor. $this-\>container
is not defined.
The following snippet illustrates what I am trying to do:
class DemoRule extends Rule {
/**
* @var int
*/
protected $myInt;
private $someService;
public function __construct(SomeService $someService) {
$this->someService = $someService;
// Here `$this->someService` is defined.
// `$someService` really is an instance of
// `SomeService` and not `null` (as requested in
// "services.xml").
}
public function getName():string {
return 'some_name';
}
public function match(RuleScope $scope):bool {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// `$this->someService` is now `null`.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Did something weird happen when `$this->myInt` was
// magically set?
return false;
}
public function getConstraints():array {
return array(
'myInt' => array(new Type('int'))
);
}
}
Maybe there is some last-resort way of obtaining a service instance (something like Shopware()-\>Container()
)?
My questions: How can we use services in a custom rule (e.g. how do we get our plugin config, repositories and custom services)?