* zend eventmanager added

* sms store triggers an event on save
* event subscriber middleware added. place to set up listeners
* koinservice lazylistens on sms save event
This commit is contained in:
Danyi Dávid 2017-09-06 00:19:03 +02:00
parent 9fe07c3a62
commit 4c7e71fdbb
10 changed files with 129 additions and 3 deletions

View File

@ -16,6 +16,7 @@
"roave/security-advisories": "dev-master",
"zendframework/zend-component-installer": "^1.0",
"zendframework/zend-config-aggregator": "^1.0",
"zendframework/zend-eventmanager": "^3.2",
"zendframework/zend-expressive": "^2.0.2",
"zendframework/zend-expressive-fastroute": "^2.0",
"zendframework/zend-expressive-helpers": "^4.0",

56
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"content-hash": "ec3f18dc9b7321742d6862e0193aa1a4",
"content-hash": "0822adb4f3d62dc32db289d0bdd9676f",
"packages": [
{
"name": "container-interop/container-interop",
@ -1668,6 +1668,60 @@
],
"time": "2016-06-30T19:48:38+00:00"
},
{
"name": "zendframework/zend-eventmanager",
"version": "3.2.0",
"source": {
"type": "git",
"url": "https://github.com/zendframework/zend-eventmanager.git",
"reference": "9d72db10ceb6e42fb92350c0cb54460da61bd79c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/zendframework/zend-eventmanager/zipball/9d72db10ceb6e42fb92350c0cb54460da61bd79c",
"reference": "9d72db10ceb6e42fb92350c0cb54460da61bd79c",
"shasum": ""
},
"require": {
"php": "^5.6 || ^7.0"
},
"require-dev": {
"athletic/athletic": "^0.1",
"container-interop/container-interop": "^1.1.0",
"phpunit/phpunit": "^6.0.7 || ^5.7.14",
"zendframework/zend-coding-standard": "~1.0.0",
"zendframework/zend-stdlib": "^2.7.3 || ^3.0"
},
"suggest": {
"container-interop/container-interop": "^1.1.0, to use the lazy listeners feature",
"zendframework/zend-stdlib": "^2.7.3 || ^3.0, to use the FilterChain feature"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.2-dev",
"dev-develop": "3.3-dev"
}
},
"autoload": {
"psr-4": {
"Zend\\EventManager\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"description": "Trigger and listen to events within a PHP application",
"homepage": "https://github.com/zendframework/zend-eventmanager",
"keywords": [
"event",
"eventmanager",
"events",
"zf2"
],
"time": "2017-07-11T19:17:22+00:00"
},
{
"name": "zendframework/zend-expressive",
"version": "2.0.3",

View File

@ -37,6 +37,8 @@ return [
'doctrine.entity_manager.orm_default' => \ContainerInteropDoctrine\EntityManagerFactory::class,
'doctrine.hydrator' => \App\Hydrator\DoctrineObjectFactory::class,
\App\Middleware\EventSubscriberMiddleware::class => \App\Middleware\EventSubscriberMiddlewareFactory::class,
],
],
];

View File

@ -15,6 +15,7 @@ use Zend\Stratigility\Middleware\ErrorHandler;
// all Exceptions.
$app->pipe(ErrorHandler::class);
$app->pipe(App\Middleware\PreFlightMiddleware::class);
$app->pipe(App\Middleware\EventSubscriberMiddleware::class);
//$app->pipe(LosMiddleware\BasePath\BasePath::class);
$app->pipe(ServerUrlMiddleware::class);

View File

@ -2,6 +2,8 @@
namespace App;
use Zend\EventManager\EventManager;
/**
* The configuration provider for the App module
*
@ -35,6 +37,7 @@ class ConfigProvider
return [
'invokables' => [
Action\PingAction::class => Action\PingAction::class,
EventManager::class => EventManager::class,
],
'factories' => [
Action\HomePageAction::class => Action\HomePageFactory::class,

View File

@ -0,0 +1,24 @@
<?php
namespace App\Middleware;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class EventSubscriberMiddleware implements ServerMiddlewareInterface
{
/**
* This actually does nothing, just wraps the next middleware in the pipe.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $delegate
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
{
return $delegate->process($request);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Middleware;
use App\Service\KoinService;
use Interop\Container\ContainerInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\LazyListener;
class EventSubscriberMiddlewareFactory
{
public function __invoke(ContainerInterface $container)
{
$eventManager = $container->get(EventManager::class);
$eventManager->setIdentifiers([
EventSubscriberMiddleware::class,
]);
$lazyListener = new LazyListener([
'listener' => KoinService::class,
'method' => 'onReceiveSmsListener',
], $container);
$eventManager->attach('store.sms.persisted', $lazyListener);
return new EventSubscriberMiddleware();
}
}

View File

@ -7,6 +7,7 @@ namespace App\Service;
use App\Entity\Sms;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Zend\EventManager\Event;
class KoinService
{
@ -60,6 +61,11 @@ class KoinService
$this->httpClient = $httpClient;
}
public function onReceiveSmsListener(Event $event)
{
$this->onReceiveSms($event->getParam('sms'));
}
/**
* @param Sms $sms
* @return int

View File

@ -7,6 +7,7 @@ namespace App\Service;
use App\Entity\Sms;
use App\Entity\User;
use Doctrine\ORM\EntityManager;
use Zend\EventManager\EventManager;
class SmsStoreService
{
@ -15,9 +16,10 @@ class SmsStoreService
*/
private $em;
public function __construct(EntityManager $em)
public function __construct(EntityManager $em, EventManager $eventManager)
{
$this->em = $em;
$this->eventManager = $eventManager;
}
public function storeSms(string $hashKey, int $direction, array $requestData): bool
@ -34,6 +36,10 @@ class SmsStoreService
$this->em->persist($sms);
$this->em->flush();
$this->eventManager->trigger('store.sms.persisted', $this, [
'sms' => $sms,
]);
return true;
}

View File

@ -5,6 +5,7 @@ namespace App\Service;
use Interop\Container\ContainerInterface;
use Zend\EventManager\EventManager;
class SmsStoreServiceFactory
@ -13,7 +14,8 @@ class SmsStoreServiceFactory
public function __invoke(ContainerInterface $container)
{
$em = $container->get('doctrine.entity_manager.orm_default');
$eventManager = $container->get(EventManager::class);
return new SmsStoreService($em);
return new SmsStoreService($em,$eventManager);
}
}