Creating a Module Override
To extend and optimize critical parts of third-party modules, we need to create overrides for the classes and methods of interest.
Ector offers an integrated override and versioning system, making it easy to create these overrides.
Creating the Override
Using an Ector CLI command, we can scaffold a typical override and use it as a base to overwrite the behavior of the module we intend to optimize.
Steps to Create an Override
Identify the Module and Method:
- Identify the module and the method you want to override.
Use Ector CLI:
- Navigate to the
modules/ector_cli
directory and run the command:
php bin/ector_cli override:create
- The command will prompt for the module name, class name, and method name to be overridden, creating the override file ready for modification.
- Navigate to the
Locate the Overrides:
- The overrides are located in
modules/ector_core/override/modules
, where you will find a folder named after the overridden module and the override file.
- The overrides are located in
Example of an Override
Here is an example of an override for the blockreassurance
module:
<?php
require_once _PS_MODULE_DIR_ . 'ector_core/vendor/autoload.php';
class blockreassuranceOverride extends blockreassurance
{
use Ector\Core\EctorOverride;
#[Ector\Core\EctorOverrideAlias([
'5.1.4' => ['5.1.4']
])]
public function hookActionFrontControllerSetMedia()
{
return $this->ectorOverrideMethod(__FUNCTION__, $this->version);
}
public function hookActionFrontControllerSetMedia_5_1_4()
{
Media::addJsDef([
'psr_icon_color' => Configuration::get('PSR_ICON_COLOR'),
]);
}
}
In this case, we have overridden the hookActionFrontControllerSetMedia
method of the blockreassurance
module and added a specific method for version 5.1.4
that adds a JS variable.
Details of the Override
Alias
Aliases are annotations that allow creating an alias for a method, making it callable across multiple versions of the module.
#[Ector\Core\EctorOverrideAlias([
'5.1.4' => ['5.1.4']
])]
If the override written for version 5.1.4 is also compatible with version 5.1.5, we can add an alias:
#[Ector\Core\EctorOverrideAlias([
'5.1.4' => ['5.1.4', '5.1.5']
])]
EctorOverrideMethod
The ectorOverrideMethod
allows calling the overridden method without rewriting all the original method code. This method automatically tries to call the corresponding method for the registered version from the alias.
public function hookActionFrontControllerSetMedia()
{
return $this->ectorOverrideMethod(__FUNCTION__, $this->version);
}
If an override is specified for version 5.1.4, ectorOverrideMethod
will try to call the method hookActionFrontControllerSetMedia_5_1_4
if present, otherwise, it will fallback to the original method.
Conclusion
Creating overrides for third-party modules with Ector is a simple and structured process, thanks to the integrated override and versioning system. Using Ector CLI, we can quickly create the necessary overrides to optimize performance and customize the behavior of modules in a safe and efficient manner.