/** * Processes a REST request, returning a Response object. * * @param \BedRest\Rest\Request\Request $request * @throws \BedRest\Rest\Exception * @return \BedRest\Rest\Response\Response */ public function dispatch(Request $request) { // determine resource $resourceParts = explode('/', $request->getResource()); $resourceName = $resourceParts[0]; $subResourceName = false; if (count($resourceParts) > 1) { $subResourceName = $resourceParts[1]; } try { $resourceMetadata = $this->resourceMetadataFactory->getMetadataByResourceName($resourceName); } catch (ResourceMappingException $e) { throw Exception::resourceNotFound($resourceName, 404, $e); } // determine service if ($subResourceName) { if (!$resourceMetadata->hasSubResource($subResourceName)) { throw Exception::resourceNotFound($request->getResource()); } $subResourceMapping = $resourceMetadata->getSubResource($subResourceName); $service = $this->serviceLocator->get($subResourceMapping['service']); } else { $service = $this->serviceLocator->get($resourceMetadata->getService()); } $this->registerListeners($service); $event = new RequestEvent(); $event->setRequest($request); $this->eventManager->dispatch($request->getMethod(), $event); return $event->getData(); }
public function testDispatchClosureListener() { $em = new EventManager(); $listenerCalled = false; $listener = function (Event $event) use(&$listenerCalled) { $listenerCalled = true; }; $em->addListener('event', $listener); $em->dispatch('event', new Event()); $this->assertTrue($listenerCalled); }
public function testStopPropagation() { $em = new EventManager(); $instanceOne = new StopPropagationListener(); $instanceTwo = new StopPropagationListener(); $em->addListeners('stopPropagationEvent', array(array($instanceOne, 'listener'), array($instanceTwo, 'listener'))); $event = new Event(); $this->assertFalse($event->propagationHalted()); $em->dispatch('stopPropagationEvent', $event); $this->assertTrue($instanceOne->listenerCalled); $this->assertFalse($instanceTwo->listenerCalled); $this->assertTrue($event->propagationHalted()); }
public function testDispatchMultipleClassesWithAnnotations() { $reader = new AnnotationDriver(); $em = new EventManager($reader); $instanceOne = new AnnotationListener(); $em->addClassListeners($instanceOne); $instanceTwo = new AnnotationListener(); $em->addClassListeners($instanceTwo); $em->dispatch('eventOne', new Event()); $this->assertTrue($instanceOne->listenerOneCalled); $this->assertTrue($instanceTwo->listenerOneCalled); $this->assertFalse($instanceOne->listenerTwoCalled); $this->assertFalse($instanceTwo->listenerTwoCalled); }