ZF2 Progress Module
This module is part of Stagem ecosystem and main goal is logging any change registered in Context
., (*1)
Requirements
-
Popov\ZfcEntity
module
-
Popov\ZfcUser
module
Logging principle is based on execution context. Conditionally realisation can describe follows (for different modules):
- item status is changing in Status Context
;
- grid is changing in Grid Context
;
- mail is sending in Mail Context
;
- item is saving in Saver Context
., (*2)
You can imagine any other Context
and describe this in config or even implement custom realisation., (*3)
Usage
Module has low coupling and develop with Event Driven in mind., (*4)
Base config include three main action names which is listened on all interfaces: edit, change, sync.
You can extend this list simply add your own interface/action name in config:, (*5)
// config/module.config.php
'progress' => [
'listeners' => [
'*' => ['edit', 'change', 'sync']
]
]
Module has ContextInterface
for convenience realisation of custom logging Context., (*6)
// config/module.config.php
'progress' => [
__NAMESPACE__ => [
'context' => Service\Progress\StatusContext::class,
]
],
Advanced usage
Suppose, you need implement logging in Status
context.
For this register new context in Status
module and declare in services, (*7)
namespace Stagem\ZfcStatus;
'progress' => [
__NAMESPACE__ => [
'context' => Service\Progress\StatusContext::class,
]
],
'service_manager' => [
'invokables' => [
Service\Progress\StatusContext::class => Service\Progress\StatusContext::class,
],
'delegators' => [
Service\Progress\StatusContext::class => [
\Stagem\Translator\Service\Factory\TranslatorDelegatorFactory::class
]
],
],
Context realisation, (*8)
namespace Magere\Status\Service\Progress;
use Zend\Mvc\I18n\Translator;
use Zend\I18n\Translator\TranslatorAwareTrait;
use Stagem\ZfcProgress\Service\ContextInterface;
use Stagem\ZfcStatus\Model\Status;
/**
* @method Translator getTranslator()
*/
class StatusContext implements ContextInterface
{
use TranslatorAwareTrait;
protected $event;
public function setEvent($event)
{
$this->event = $event;
}
public function getEvent()
{
return $this->event;
}
public function getItem()
{
return $this->event->getTarget();
}
public function getExtra()
{
return [
'newStatusId' => $this->getEvent()->getParam('newStatus')->getId(),
'oldStatusId' => $this->getEvent()->getParam('oldStatus')->getId(),
];
}
public function getMessage()
{
$translator = $this->getTranslator();
/** @var Status $newStatus */
$newStatus = $this->getEvent()->getParam('newStatus');
/** @var Status $oldStatus */
$oldStatus = $this->getEvent()->getParam('oldStatus');
$prefix = $translator->translate(
'Status change',
$this->getTranslatorTextDomain(),
$translator->getFallbackLocale()
) . ':';
$template = $translator->translate(
'%s from %s to %s',
$this->getTranslatorTextDomain(),
$translator->getFallbackLocale()
);
return sprintf($template, $prefix, $oldStatus->getName(), $newStatus->getName());
}
}
Following execution rely on Progress
module with has registered ProgressListener
and which listen all events ['edit', 'change', 'sync'']
one of which run Status
module., (*9)
Also you simply can register custom event name if pre registered is inopportune:, (*10)
'progress' => [
'listeners' => [
Service\Api\InvoiceSoapService::class => ['syncInvoice'],
]
],