Events silex中的事件调度器
我需要用Symphony的事件调度器替换我的Events silex中的事件调度器,events,dispatcher,silex,Events,Dispatcher,Silex,我需要用Symphony的事件调度器替换我的notifyService 以下是原始服务: <?php class VmService { /** * @var VmManager|null */ protected $vmManager = null; /** * @var ProvisionerInterface[] */ protected $provisionners = array(); /**
notifyService
以下是原始服务:
<?php
class VmService
{
/**
* @var VmManager|null
*/
protected $vmManager = null;
/**
* @var ProvisionerInterface[]
*/
protected $provisionners = array();
/**
* @var NotifyService|null
*/
protected $notifyService = null;
public function setVmManager(VmManager $vmManager)
{
$this->vmManager = $vmManager;
}
public function getVmManager()
{
return $this->vmManager;
}
/**
* @param $type
* @param ProvisionerInterface $provisionner
*/
public function setProvisionner($type, ProvisionerInterface $provisionner)
{
$this->provisionners[$type] = $provisionner;
}
/**
* @param $type
* @return ProvisionerInterface
*/
public function getProvisionner(Vm $vm)
{
return $this->provisionners[$vm->getType()];
}
/**
* @param NotifyService $notifyService
*/
public function setNotifyService(NotifyService $notifyService)
{
$this->notifyService = $notifyService;
}
/**
* @return NotifyService|null
*/
public function getNotifyService()
{
return $this->notifyService;
}
public function initialise(Vm $vm)
{
$vmManager = $this->getVmManager();
$provisioner = $this->getProvisionner($vm);
$provisioner->initialise($vm);
$vm->setStatus(VM::STOPPED);
$vmManager->flush($vm);
}
public function delete(Vm $vm, $force = false)
{
$now = new \DateTime();
$day = $now->format('w');
if ( ($day == 0 || $day == 6) && ! $force) {
throw new \Exception('Cannot delete a VM on weekend unless you force it');
}
$vmManager = $this->getVmManager();
$provisioner = $this->getProvisionner($vm);
$provisioner->delete($vm);
$vm->setStatus(Vm::STOPPED);
$vmManager->flush($vm);
}
private function deleteLogFile(Vm $vm)
{
$filename = VmLogger::getLogFile($vm->getIdVm());
if (file_exists($filename)) {
@unlink("$filename");
}
}
public function prepare(Vm $vm)
{
/**
* @var VM $vm
*/
$provisionner = $this->getProvisionner($vm);
//$provisionner->start($vm, true, 'integ.lafourchette.local');
$provisionner->stop($vm);
}
public function start(Vm $vm, $provisionEnable = true)
{
$vmManager = $this->getVmManager();
$notify = $this->getNotifyService();
/**
* @var VM $vm
*/
$provisionner = $this->getProvisionner($vm);
$vm->setStatus(VM::STARTED);
$vmManager->flush($vm);
try {
$provisionner->start($vm, $provisionEnable);
$vm->setStatus(VM::RUNNING);
$vmManager->flush($vm);
$notify->send('ready', $vm);
} catch (UnableToStartException $e) {
$vm->setStatus(VM::STOPPED);
$vmManager->flush($vm);
$notify->send('unable_to_start', $vm);
throw $e;
}
}
public function getStatus(Vm $vm)
{
return $this->getProvisionner($vm)->getStatus($vm);
}
public function stop(Vm $vm)
{
$vmManager = $this->getVmManager();
/**
* @var VM $vm
*/
$provisionner = $this->getProvisionner($vm);
$vm->setStatus(Vm::STOPPED);
$vmManager->flush($vm);
$provisionner->stop($vm);
}
public function archived(Vm $vm)
{
$vmManager = $this->getVmManager();
$this->delete($vm);
$vm->setStatus(VM::EXPIRED);
$this->deleteLogFile($vm);
$vmManager->flush($vm);
$this->prepare($vm);
}
}
您需要重构代码,为了调度事件,必须创建事件并在EventDispatcher实例上调用dispatch
方法,因此在代码中而不是在当前执行的操作中:
<?php
// this is the start method of your service
// ...
$event = new NotifyEvent($vm);
$dispatcher->addListener('notify.action', $event);
$dispatcher->dispatch('notify.action');
// ...
请注意,$callable1
和$callable2
为您提供了一个想法,再次检查以了解如何正确添加侦听器(您可以在处理事件的类中创建clousure或方法,完全由您决定)
现在,您已经基于事件调度器定义了notifyService(另一个与Silex使用的EventDispatcher完全不同的服务,因此您的域事件有一个干净的事件调度器),您可以将其用作类上的notify服务。您可以像以前一样执行此操作:使用setNotifyService
方法,在代码中,您只需要创建事件并调用dispatch方法(假设您已经调用了setNotifyService):
您需要重构代码,为了调度事件,必须创建事件并在EventDispatcher实例上调用dispatch
方法,因此在代码中而不是在当前执行的操作中:
<?php
// this is the start method of your service
// ...
$event = new NotifyEvent($vm);
$dispatcher->addListener('notify.action', $event);
$dispatcher->dispatch('notify.action');
// ...
请注意,$callable1
和$callable2
为您提供了一个想法,再次检查以了解如何正确添加侦听器(您可以在处理事件的类中创建clousure或方法,完全由您决定)
现在,您已经基于事件调度器定义了notifyService(另一个与Silex使用的EventDispatcher完全不同的服务,因此您的域事件有一个干净的事件调度器),您可以将其用作类上的notify服务。您可以像以前一样执行此操作:使用setNotifyService
方法,在代码中,您只需要创建事件并调用dispatch方法(假设您已经调用了setNotifyService):
<?php
// this is the start method of your service
// ...
$event = new NotifyEvent($vm);
$dispatcher->dispatch('notify.action', $event);
// ...
<?php
// somewhere in your config file
// ...
$app['notifyService'] = $app->share(function() use ($app) {
$dispatcher = new Symfony\Component\EventDispatcher\EventDispatcher();
$dispatcher->addListener('notify.success', $callable1);
$dispatcher->addListener('notify.unable_to_start', $callable2);
return $dispatcher;
});
<?php
// class VmService
// method start
// when you've to dispatch the success event
$event = new NotifySuccessEvent($vm);
$this->notifyService->dispatch('notify.success', $event);
// if you have to dispatch the notify.unable_to_start event
$event = new NotifyUnableToStartEvent($vm);
$this->notifyService->dispatch('notify.unable_to_start', $event);