Adjusting a service

Overview

In this guide you'll learn how to adjust a service. You can read more about service decoration in the Symfony documentation.

Prerequisites

In order to add your own custom service for your plugin, you first need a plugin as base. Therefore, you can refer to the Plugin Base Guide.
Here's a video explaining service decorations with an easy example from our free online training "Backend Development".

Decorating the service

First of all we have to create a new service for this example which gets decorated in the next step. Then we have to add a new service to our services.xml with the attribute decorates pointing to our service we want to decorate. Next we have to add our service decorator as argument, but we append an .inner to the end of the service to keep the old one as reference.
Here's our example services.xml:
<plugin root>/src/Resources/config/services.xml
1
<?xml version="1.0" ?>
2
​
3
<container xmlns="http://symfony.com/schema/dic/services"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6
​
7
<services>
8
<service id="Swag\BasicExample\Service\ExampleService" />
9
​
10
<service id="Swag\BasicExample\Service\ExampleServiceDecorator" decorates="Swag\BasicExample\Service\ExampleService">
11
<argument type="service" id="Swag\BasicExample\Service\ExampleServiceDecorator.inner" />
12
</service>
13
</services>
14
</container>
Copied!
Now we have to define an abstract class because it's more beautiful and not so strict like interfaces. With an abstract class we can add new functions easier, you can read more about this at the end of this article. The abstract class has to include an abstract function called getDecorated() which has the return type of our instance.
To avoid misunderstandings: The abstract service class and the implementation of it is not part of the decoration process itself and most of the times comes either from the Shopware core or from a plugin you want to extend. They are added here to have an example to decorate.
Therefore, this is how your abstract class could then look like:
<plugin root>/src/Service/AbstractExampleService.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Service;
4
​
5
abstract class AbstractExampleService
6
{
7
abstract public function getDecorated(): AbstractExampleService;
8
​
9
abstract public function doSomething(): string;
10
}
Copied!
Now we have our abstract class, but no service which uses it. So we create our ExampleService which extends from our AbstractExampleService. In our service the getDecorated() function has to throw an DecorationPatternException because it has no decoration yet.
Therefore, your service could then look like this:
<plugin root>/src/Service/ExampleService.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Service;
4
​
5
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
6
​
7
class ExampleService extends AbstractExampleService
8
{
9
public function getDecorated(): AbstractExampleService
10
{
11
throw new DecorationPatternException(self::class);
12
}
13
​
14
public function doSomething(): string
15
{
16
return 'Did something.';
17
}
18
}
Copied!
The last step is creating our decorated service called ExampleServiceDecorator in this example. Our decorated service has to extend from the AbstractExampleService and the constructor has to accept an instance of AbstractExampleService. Furthermore, the getDecorated() function has to return the decorated service passed into the constructor.
Your service could then look like below:
<plugin root>/src/Service/ExampleServiceDecorator.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Service;
4
​
5
class ExampleServiceDecorator extends AbstractExampleService
6
{
7
/**
8
* @var AbstractExampleService
9
*/
10
private $decoratedService;
11
​
12
public function __construct(AbstractExampleService $exampleService)
13
{
14
$this->decoratedService = $exampleService;
15
}
16
​
17
public function getDecorated(): AbstractExampleService
18
{
19
return $this->decoratedService;
20
}
21
​
22
public function doSomething(): string
23
{
24
$originalResult = $this->decoratedService->doSomething();
25
​
26
return $originalResult . ' Did something additionally.';
27
}
28
}
Copied!

Adding new functions to an existing service

If you plan to add new functions to your service, it is recommended to add them as normal public functions due to backwards compatibility, if you decorate the service at several places. In this example we add a new function called doSomethingNew() which first calls the getDecorated() and then our new function doSomethingNew() because if our decorator does not implement it yet, it will call it from the parent. The advantage of adding it as normal public function is that you can implement it step by step into your other services without any issues. After you have implemented the function in every service decorator, you can make it abstract for the next release. If you add it directly as an abstract function, you will get errors because the function is required for every service decorator.
Here's our example abstract class:
<plugin root>/src/Service/AbstractExampleService.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Service;
4
​
5
abstract class AbstractExampleService
6
{
7
abstract public function getDecorated(): AbstractExampleService;
8
​
9
abstract public function doSomething(): string;
10
​
11
public function doSomethingNew(): string
12
{
13
return $this->getDecorated()->doSomethingNew();
14
}
15
}
Copied!
After we have implemented our new function in the abstract class, we implement it in our service too.
<plugin root>/src/Service/ExampleService.php
1
<?php declare(strict_types=1);
2
​
3
namespace Swag\BasicExample\Service;
4
​
5
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
6
​
7
class ExampleService extends AbstractExampleService
8
{
9
public function getDecorated(): AbstractExampleService
10
{
11
throw new DecorationPatternException(self::class);
12
}
13
​
14
public function doSomething(): string
15
{
16
return 'Did something.';
17
}
18
​
19
public function doSomethingNew(): string
20
{
21
return 'Did something new.';
22
}
23
}
Copied!
Last modified 3mo ago