/** * @param $path * @return \Timiki\Bundle\RpcServerBundle\Server\Mapper * @throws \Timiki\Bundle\RpcServerBundle\Server\Exceptions\InvalidMappingException */ public static function getMapper($path = null) { AnnotationRegistry::registerFile(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Mapping' . DIRECTORY_SEPARATOR . 'Cache.php'); AnnotationRegistry::registerFile(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Mapping' . DIRECTORY_SEPARATOR . 'Execute.php'); AnnotationRegistry::registerFile(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Mapping' . DIRECTORY_SEPARATOR . 'Method.php'); AnnotationRegistry::registerFile(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Mapping' . DIRECTORY_SEPARATOR . 'Param.php'); AnnotationRegistry::registerFile(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Mapping' . DIRECTORY_SEPARATOR . 'Roles.php'); $mapper = new Mapper(); if ($path) { $mapper->addPath($path); } return $mapper; }
/** * Execute json request. * * @param $object * @param JsonRequest $jsonRequest * * @return mixed * @throws \Timiki\Bundle\RpcServerBundle\Server\Exceptions\InvalidMappingException */ protected function executeJsonRequest($object, JsonRequest $jsonRequest) { $metadata = $this->mapper->loadObjectMetadata($object); // Inject container if ($object instanceof ContainerAwareInterface && $this->container) { $object->setContainer($this->container); } // Get params if (array_keys($jsonRequest->getParams()) === range(0, count($jsonRequest->getParams()) - 1)) { // Given only values $values = $jsonRequest->getParams(); $params = []; foreach (array_keys($metadata['params']) as $id => $key) { if (isset($values[$id])) { $params[$key] = $values[$id]; } } } else { // Given name => value $params = $jsonRequest->getParams(); } // Inject params $reflection = new \ReflectionObject($object); foreach ($params as $name => $value) { if (!$reflection->hasProperty($name)) { throw new Exceptions\InvalidParamsException(null, $jsonRequest->getId()); } $reflectionProperty = $reflection->getProperty($name); $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($object, $value); } // Validate if ($this->container && $this->container->has('validator')) { $validator = $this->container->get('validator'); $result = $validator->validate($object); if ($result->count() > 0) { $data = []; /* @var ConstraintViolation $constraintViolation */ foreach ($result as $constraintViolation) { $name = $constraintViolation->getPropertyPath() ? $constraintViolation->getPropertyPath() : 'violations'; if (!isset($data[$name])) { $data[$name] = []; } $data[$name][] = $constraintViolation->getMessage(); } throw new Exceptions\InvalidParamsException($data); } } // Roles grant if ($this->container && $this->container->has('security.authorization_checker') && !empty($metadata['roles'])) { if (!$this->container->get('security.authorization_checker')->isGranted((array) $metadata['roles']->value)) { throw new Exceptions\MethodNotGrantedException(); } } // Execute return $object->{$metadata['executeMethod']}(); }