Extend a core module that is used by another

Edit on GitHub

This document describes how to extend a core module that is used by another core module.

Extra consideration must be taken when extending core modules that are already in use by another module.

The following example extends the Cart -> Calculation modules.

1. Modify the interface

Add the foo() method to CalculationFacade on the project level and call it from the Cart module.

The CalculationFacade needs to implement the CartToCalculationInterface because this interface is used in the Cart module.

You can also add your own interface as follows:

<?php
namespace Pyz\Zed\Cart\Dependency\Facade;

use Spryker\Zed\Cart\Dependency\Facade\CartToCalculationInterface as SprykerCartToCalculationInterface;

interface CartToCalculationInterface extends SprykerCartToCalculationInterface
{
    public function foo();
}

2. Add the new method to the interface

The interface needs to extend one from the core.

<?php
namespace Pyz\Zed\Calculation\Business;

use Pyz\Zed\Cart\Dependency\Facade\CartToCalculationInterface;
use Spryker\Zed\Calculation\Business\CalculationFacade as SprykerCalculationFacade;

class CalculationFacade extends SprykerCalculationFacade implements CartToCalculationInterface
{
    public function foo()
    {
        die('<pre><b>'.print_r('!!', true).'</b>'.PHP_EOL.__CLASS__.' '.__LINE__);
    }

}

3. Remove the bridge

In the Cart module’s dependency provider, remove the bridge to directly use the facade.

class CartDependencyProvider extends SprykerCartDependencyProvider
{

public function provideBusinessLayerDependencies(Container $container)
{
	self::provideBusinessLayerDependencies($container);

	$container[self::FACADE_CALCULATION] = function (Container $container) {
		return $container->getLocator()->calculation()->facade();
	};
}

Bridges are for core-level only. If you use them at the project level, you are doing it wrong.

Spryker is constantly improving type declarations of all methods. Thus, some bridge interfaces might be incompatible with Facade interfaces, so this approach does not work. To prevent this, consider module version patch-lock.

Info

The described case is only practical when you are “between” two core bundles, and you want to make it right. For your own modules, use the general module interface—for example, MyModuleInterface.