Wireless Cross-Component Communication


composer require maslosoft/signalsVarious application components must be connected to each other. This should be made in a soft manner, so that when adding or removing component code will not require to change.
Signals provide somthing like wireless connection between components. On of them emit signal, the other receives it. This can be done other way around. That one application component sends signal, and several components listen to that signal.
This component allows for interaction of application components, without prior or explicit assignment.
Use composer to install
composer require maslosoft/signals:"*"Or by hard way, download somewhere in your project and ensure autoloading works for Maslosoft\Signals\* and you include dep too;
Setup signals. After calling init any further instance will be configured same as below $signal.
$signal = newMaslosoft\Signals\Signal();
$signal->runtimePath = RUNTIME_PATH;
$signal->paths = [
MODELS_PATH
];
$signal->init();Generate signals definition, only once, hook it to your build script etc.
$signal = newMaslosoft\Signals\Signal();
(newMaslosoft\Signals\Utility($signal))->generate();Define signal:
namespaceMyNamespace\Signals;
class AccountMenuItems extends AdminMenuItems
{
public$item = [];
}Define class with slot with @SlotFor annotation
namespaceMaslosoft\Ilmatar\Modules;
class MyModule
{
/** * @SlotFor(MyNamespace\Signals\AccountMenuItems) */publicfunctionreactOnAccountMenu(MyNamespace\Signals\AccountMenuItems$signal)
{
$signal->item = [
'url' => '/content/myBlog',
'label' => 'My blog'
];
}
}Emit signal and get results of this call:
$signal = newMaslosoft\Signals\Signal();
$result = $signal->emit(newAdminMenuItems());
echo$result[0]->item[0]['label']; // My blog