public function testReturnsAllPublicMethods() { $reflClassA = new \ReflectionClass($this->testSubjectA); $reflClassB = new \ReflectionClass($this->testSubjectB); $this->assertCount(1, ReflectionUtils::getMethods($reflClassA)); $this->assertCount(2, ReflectionUtils::getMethods($reflClassB)); }
/** * {@inheritDoc} */ public function injectResources(SagaInterface $saga) { $reflectionClass = ReflectionUtils::getClass($saga); foreach (ReflectionUtils::getMethods($reflectionClass) as $reflectionMethod) { if (null !== ($annotation = $this->reader->getMethodAnnotation($reflectionMethod, Inject::class))) { $service = $this->container->get($annotation->service); $reflectionMethod->invokeArgs($saga, array($service)); } } }
/** * {@inheritDoc} */ public function injectResources(SagaInterface $saga) { $reflectionClass = ReflectionUtils::getClass($saga); foreach (ReflectionUtils::getMethods($reflectionClass) as $reflectionMethod) { if (null !== ($annotation = $this->reader->getMethodAnnotation($reflectionMethod, Inject::class))) { if (!array_key_exists($annotation->service, $this->resources)) { throw new \RuntimeException(sprintf("Resource id \"%s\" is not registered", $annotation->service)); } $reflectionMethod->invokeArgs($saga, array($this->resources[$annotation->service])); } } }
/** * Runs the inspection on the target class and saves its handlers. */ private function inspect() { foreach (ReflectionUtils::getMethods($this->targetClass) as $method) { $annotation = $this->annotationReader->getMethodAnnotation($method, $this->annotation); if (!$annotation) { continue; } $payloadType = $this->extractPayloadType($method); $methodAnnotations = $this->annotationReader->getMethodAnnotations($method); if ($payloadType) { $this->handlers[] = new AnnotatedHandlerDefinition($this->targetClass, $method, $methodAnnotations, $payloadType); } } }
private function getAnnotatedTargetValue($annotationName, CommandMessageInterface $command, AnnotationReader $reader, \ReflectionClass $reflClass) { foreach (ReflectionUtils::getProperties($reflClass) as $property) { if (null !== ($annot = $reader->getPropertyAnnotation($property, $annotationName))) { $property->setAccessible(true); return $property->getValue($command->getPayload()); } } foreach (ReflectionUtils::getMethods($reflClass) as $method) { if (null !== ($annot = $reader->getMethodAnnotation($method, $annotationName))) { $method->setAccessible(true); return $method->invoke($command->getPayload()); } } return null; }
private function fieldsMatch($aClass, $expectedValue, $actual) { $match = true; $reflClass = new \ReflectionClass($aClass); foreach (ReflectionUtils::getProperties($reflClass) as $property) { $property->setAccessible(true); $expectedFieldValue = $property->getValue($expectedValue); $actualFieldValue = $property->getValue($actual); if ($expectedFieldValue != $actualFieldValue) { $this->failedField = $property->name; $this->failedFieldExpectedValue = $expectedFieldValue; $this->failedFieldActualValue = $actualFieldValue; return false; } } return $match; }
public function getMessageHandlers(EventMessageInterface $event) { $found = array(); $reflectionClass = ReflectionUtils::getClass($this->targetSaga); foreach (ReflectionUtils::getMethods($reflectionClass) as $method) { $annot = $this->reader->getMethodAnnotation($method, SagaEventHandler::class); if (null === $annot) { continue; } if (0 === count($method->getParameters())) { throw new \RuntimeException(sprintf("Invalid method signature detected of %s::%s. " . "Methods annotated with @SagaEventHandler must have exatly one parameter with the type of the message they handle. ", $reflectionClass->name, $method->name)); } $parameter = current($method->getParameters()); if (null !== $parameter->getClass() && $parameter->getClass()->name === $event->getPayloadType()) { $found[] = SagaMethodMessageHandler::getInstance($event, $method, $annot); } } return $found; }
/** * @param DomainEventMessageInterface $event */ public function findAndInvokeEventHandlers(DomainEventMessageInterface $event) { // !!! TODO revisit foreach (ReflectionUtils::getMethods($this->reflectionClass) as $method) { $annotation = $this->reader->getMethodAnnotation($method, EventHandler::class); if (null !== $annotation) { $parameter = current($method->getParameters()); if (null !== $parameter->getClass() && $parameter->getClass()->name === $event->getPayloadType()) { $method->invokeArgs($this->targetObject, array($event->getPayload())); } } } }