Decorate a Shopware 6 core service without modifying the abstract class

138 views Asked by At

After decorating a Shopware 6 core service and adding a new method, how do you use the call up the new method. I am not allowed to modify Shopware 6 bundle code base. Example:


//abstract class begins
abstract class AbstractCoreService
{
  public function doSomething();
}
//abstract class ends


//service class begins
class CoreService extends AbstractCoreService
{
  public function doSomething(){
    return 'I did Something'
  }
}
//service class ends


//decorator begins
class CoreServiceDecorator extends AbstractCoreService
{
    public function doSomething(){
     return 'I did Something More';
    }

    public function doSomethingElse(){
     return 'I did Something Else';
    }
}
//decorator ends

How do I use the method doSomethingElse? I get an undefined method error code Editor.

Below is my xml file code. PS, I followed this Shopware 6 guide Docorate A Service

        <service id="AbstractCoreService" id="CoreService" />

        <service id="CoreServiceDecorator" decorates="AbstractCoreService">
        </service>

Here is me using the doSomethingElse method of the CoreServiceDecorator class.

use AbstractCoreService;

class DoRunService extends AbstractCoreService;
{
 
 private AbstractCoreService $coreService;

 __construct(AbstractCoreService $coreService)
 {
   $this->coreService = $coreService; 
 }

 public function doSomething(){
  return $this->coreService->doSomething();
 }

 public function doRun(){
  return $this->coreService->doSomethingElse();
  //here the error message from my code Editor is `undefined method`
 }

}

The xml file includes

 <service id="DoRunService">
  <argument id="AbstractCoreService" type="service">
 </service>
0

There are 0 answers