public function testGetMetadataReturnsNullIfNoMetadataIsFound()
 {
     $driver = $this->getMock('Metadata\\Driver\\DriverInterface');
     $driver->expects($this->once())->method('loadMetadataForClass')->will($this->returnValue(null));
     $factory = new MetadataFactory($driver);
     $this->assertNull($factory->getMetadataForClass('stdClass'));
 }
Пример #2
0
 function it_generates_a_validation(MetadataFactory $metadataFactory)
 {
     $classMetadata = $this->getClassMetadata();
     $classMetadata->propertyMetadata['prop']->rules = ['required'];
     $data = $this->getData();
     $data['prop']['rules'] = ['required'];
     $metadataFactory->getMetadataForClass('Indigo\\Fieldset\\Stubs\\Entity')->willReturn($classMetadata);
     $this->generateValidation('Indigo\\Fieldset\\Stubs\\Entity')->shouldHaveType('Fuel\\Validation\\Validator');
 }
Пример #3
0
 /**
  * @param $classes
  * @return array
  */
 public function guessRdfClassFiliation($classes)
 {
     foreach ($classes as $class) {
         $metadata = $this->metadataFactory->getMetadataForClass($class);
         $types = $metadata->getTypes();
         if (!empty($types)) {
             $parentClasses = $metadata->getParentClasses();
             $this->addParentClass($types, $parentClasses);
         }
     }
     return $this->rdfFiliation;
 }
 /**
  * @param LoggerInterface $logger
  * @param bool            $dryRun
  *
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function doExecute(LoggerInterface $logger, $dryRun = false)
 {
     $classNames = $this->getAllConfigurableEntities($logger);
     foreach ($classNames as $className) {
         if (!class_exists($className)) {
             // skip if a class no longer exists
             continue;
         }
         $fieldConfigs = $this->loadFieldConfigs($logger, $className);
         $fieldsChangedByUser = $this->loadFieldsChangedByUser($logger, $className);
         /** @var EntityMetadata $metadata */
         $metadata = $this->metadataFactory->getMetadataForClass($className);
         foreach ($fieldConfigs as $fieldName => $fieldConfig) {
             if (isset($fieldsChangedByUser[$fieldName])) {
                 // skip because the value is changed by an user
                 continue;
             }
             if (!isset(self::$allowedTypes[$fieldConfig['type']])) {
                 // skip because this data type should not be available in email templates
                 continue;
             }
             if ($metadata && $metadata->configurable && isset($metadata->propertyMetadata[$fieldName])) {
                 /** @var FieldMetadata $fieldMetadata */
                 $fieldMetadata = $metadata->propertyMetadata[$fieldName];
                 if (isset($fieldMetadata->defaultValues['email']) && array_key_exists('available_in_template', $fieldMetadata->defaultValues['email'])) {
                     // skip because this field has @ConfigField annotations with email.available_in_template
                     continue;
                 }
             }
             $data = $fieldConfig['data'];
             if (isset($data['email']['available_in_template']) && $data['email']['available_in_template']) {
                 // skip because the value is already TRUE
                 continue;
             }
             if (!isset($data['email'])) {
                 $data['email'] = [];
             }
             $data['email']['available_in_template'] = true;
             $query = 'UPDATE oro_entity_config_field SET data = :data WHERE id = :id';
             $params = ['data' => $data, 'id' => $fieldConfig['id']];
             $types = ['data' => 'array', 'id' => 'integer'];
             $this->logQuery($logger, $query, $params, $types);
             if (!$dryRun) {
                 $this->connection->executeUpdate($query, $params, $types);
             }
         }
     }
 }
Пример #5
0
 /**
  * @param object $object
  * @param object|null $entity
  */
 protected function processExclusions($object, $entity)
 {
     $class = get_class($object);
     if (!$entity) {
         return;
     }
     $reflection = new \ReflectionClass($class);
     /** Backfill data that is ignored or read only from the serializer */
     $metadata = $this->metadataFactory->getMetadataForClass($class);
     foreach ($reflection->getProperties() as $property) {
         $name = $property->getName();
         if (!isset($metadata->propertyMetadata[$name]) || $metadata->propertyMetadata[$name]->readOnly) {
             $property->setAccessible(true);
             $property->setValue($object, $property->getValue($entity));
         }
     }
     $em = $this->doctrine->getManagerForClass($class);
     /** @var \Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata */
     $metadata = $em->getMetadataFactory()->getMetadataFor($class);
     // Look for relationships, compare against preloaded entity
     foreach ($metadata->getAssociationNames() as $fieldName) {
         if ($metadata->isCollectionValuedAssociation($fieldName)) {
             $property = $reflection->getProperty($fieldName);
             $property->setAccessible(true);
             if ($property->getValue($object)) {
                 foreach ($property->getValue($object) as $i => $value) {
                     $v = $property->getValue($entity);
                     $this->processExclusions($value, $v[$i]);
                 }
             }
         }
     }
 }
Пример #6
0
 /**
  * @param string $className
  *
  * @return ClassMetadata|MergeableClassMetadata|null
  */
 public function getMetadataForClass($className)
 {
     $classMetadata = parent::getMetadataForClass($className);
     if ($classMetadata !== null && $classMetadata instanceof ClassHierarchyMetadata) {
         return $classMetadata->getOutsideClassMetadata();
     }
     return $classMetadata;
 }
Пример #7
0
 /**
  * @param $type
  *
  * @return \Metadata\ClassHierarchyMetadata|\Metadata\MergeableClassMetadata|null
  */
 public function getMetadata($type)
 {
     $class = TypeMapper::get($type);
     if ($class) {
         return $this->metadataFactory->getMetadataForClass($class);
     }
     return;
 }
Пример #8
0
 private function getParentObjectInlining($object, SerializationContext $context)
 {
     $metadataStack = $context->getMetadataStack();
     $visitingStack = $context->getVisitingStack();
     $parentObject = null;
     if (count($visitingStack) > 0) {
         $parentObject = $visitingStack[0];
     }
     if ($parentObject === $object && count($visitingStack) > 1) {
         $parentObject = $visitingStack[1];
         // $object is inlined inside $parentObject
     }
     if ($metadataStack->count() > 0 && isset($metadataStack[0]->inline) && $metadataStack[0]->inline && $this->serializerMetadataFactory->getMetadataForClass(get_class($parentObject)) === $metadataStack[1]) {
         return $parentObject;
     }
     return null;
 }
 /**
  * Get translatable metadata
  *
  * @param string $className
  * @return TranslatableMetadata|TranslationMetadata
  */
 public function getTranslatableMetadata($className)
 {
     if (array_key_exists($className, $this->cache)) {
         return $this->cache[$className];
     }
     if ($metadata = $this->metadataFactory->getMetadataForClass($className)) {
         $metadata->validate();
     }
     $this->cache[$className] = $metadata;
     return $metadata;
 }
Пример #10
0
 /**
  * @param mixed $controller
  *
  * @return \BackBee\Rest\Mapping\ActionMetadata
  */
 protected function getControllerActionMetadata($controller)
 {
     $controllerClass = get_class($controller[0]);
     $metadata = $this->metadataFactory->getMetadataForClass($controllerClass);
     $controllerMetadata = $metadata->getOutsideClassMetadata();
     $action_metadatas = null;
     if (array_key_exists($controller[1], $controllerMetadata->methodMetadata)) {
         $action_metadatas = $controllerMetadata->methodMetadata[$controller[1]];
     }
     return $action_metadatas;
 }
 /**
  * Create open graph object
  */
 public function build()
 {
     $annotationReader = $this->annotationReader;
     if (null === $annotationReader) {
         $annotationReader = new AnnotationReader();
         if (null !== $this->cacheDirectory) {
             $cacheDirectory = sprintf('%s/annotations', $this->cacheDirectory);
             $this->createDirectory($cacheDirectory);
             $annotationReader = new CachedReader($annotationReader, new FilesystemCache($cacheDirectory));
         }
     }
     $metadataDriver = $this->driverFactory->createDriver($this->metadataDirectories, $annotationReader);
     $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
     $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
     if (null !== $this->cacheDirectory) {
         $directory = sprintf('%s/metadata', $this->cacheDirectory);
         $this->createDirectory($directory);
         $metadataFactory->setCache(new FileCache($directory));
     }
     return new OpenGraphGenerator($metadataFactory);
 }
Пример #12
0
 /**
  * Builds validation for a class
  *
  * @param string    $class
  * @param Validator $validator
  */
 public function buildValidation($class, Validator $validator)
 {
     $classMetadata = $this->metadataFactory->getMetadataForClass($class);
     foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
         $validator->addField($propertyMetadata->name, $propertyMetadata->label);
         foreach ($propertyMetadata->rules as $name => $arguments) {
             if (is_int($name)) {
                 $name = $arguments;
                 $arguments = [];
             }
             $rule = $validator->createRuleInstance($name, [$arguments]);
             $validator->addRule($propertyMetadata->name, $rule);
         }
     }
 }
Пример #13
0
 /**
  * @param FilterControllerEvent $event
  */
 public function onKernelController(FilterControllerEvent $event)
 {
     list($controller, $method) = $event->getController();
     $classMetadata = $this->metadataFactory->getMetadataForClass(get_class($controller));
     $methodMetadata = $classMetadata->methodMetadata[$method];
     if ($methodMetadata->frame) {
         $this->frame = $methodMetadata->frame;
     } elseif ($classMetadata->frame) {
         $this->frame = $classMetadata->frame;
     }
     if ($methodMetadata->options) {
         $this->options = $methodMetadata->options;
     } elseif ($classMetadata->options) {
         $this->options = $classMetadata->options;
     }
 }
Пример #14
0
 /**
  * Accepts an objectpair and going through its properties for calling the visitor on each
  * comparable property.
  *
  * @param object $mergeFrom Sourceobject
  * @param object $mergeTo   Targetobject
  *
  * @throws Exception\MergeException
  */
 public function accept($mergeFrom, $mergeTo)
 {
     // No object is passed either for mergeFrom or mergeTo.
     if (!is_object($mergeFrom) || !is_object($mergeTo)) {
         return;
     }
     $class = get_class($mergeFrom);
     if (!$mergeTo instanceof $class) {
         throw new MergeException('Not able to merge objects with different types.');
     }
     // If the object is not visited.
     if (isset($this->visitedObjects[spl_object_hash($mergeFrom)])) {
         return;
     }
     $classMetadata = $this->metadataFactory->getMetadataForClass(get_class($mergeFrom));
     $this->walkProperties($mergeFrom, $mergeTo, $classMetadata);
 }
Пример #15
0
 /**
  * Search parent frame pathes
  * @param $parentClass
  * @param array $parentFrames
  * @return array
  */
 public function getParentFrames($type)
 {
     $parentClasses = $this->filiationBuilder->getParentTypes($type);
     $parentFrames = array();
     if (!$parentClasses || empty($parentClasses)) {
         return $parentFrames;
     }
     foreach ($parentClasses as $parentClass) {
         $phpClass = TypeMapper::get($parentClass);
         if ($phpClass) {
             $metadata = $this->metadataFactory->getMetadataForClass($phpClass);
             $frame = $metadata->getFrame();
             if ($frame) {
                 $parentFrames[] = $metadata->getFrame();
             }
         }
     }
     return $parentFrames;
 }
 public function testGetMetadataWithInterfaces()
 {
     $driver = $this->getMock('Metadata\\Driver\\DriverInterface');
     $driver->expects($this->at(3))->method('loadMetadataForClass')->with($this->equalTo(new \ReflectionClass('Metadata\\Tests\\Fixtures\\ComplexHierarchy\\SubClassA')));
     $driver->expects($this->at(2))->method('loadMetadataForClass')->with($this->equalTo(new \ReflectionClass('Metadata\\Tests\\Fixtures\\ComplexHierarchy\\InterfaceB')));
     $driver->expects($this->at(1))->method('loadMetadataForClass')->with($this->equalTo(new \ReflectionClass('Metadata\\Tests\\Fixtures\\ComplexHierarchy\\BaseClass')));
     $driver->expects($this->at(0))->method('loadMetadataForClass')->with($this->equalTo(new \ReflectionClass('Metadata\\Tests\\Fixtures\\ComplexHierarchy\\InterfaceA')));
     $factory = new MetadataFactory($driver);
     $factory->setIncludeInterfaces(true);
     $factory->getMetadataForClass('Metadata\\Tests\\Fixtures\\ComplexHierarchy\\SubClassA');
 }
Пример #17
0
 public function testGetAllClassNamesThrowsException()
 {
     $this->setExpectedException('RuntimeException');
     $factory = new MetadataFactory($this->getMock('Metadata\Driver\DriverInterface'));
     $factory->getAllClassNames();
 }
Пример #18
0
 public function build()
 {
     $annotationReader = $this->annotationReader;
     if (null === $annotationReader) {
         $annotationReader = new AnnotationReader();
         if (null !== $this->cacheDir) {
             $this->createDir($this->cacheDir . '/annotations');
             $annotationReader = new FileCacheReader($annotationReader, $this->cacheDir . '/annotations', $this->debug);
         }
     }
     $metadataDriver = $this->driverFactory->createDriver($this->metadataDirs, $annotationReader);
     $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
     $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
     if (null !== $this->cacheDir) {
         $this->createDir($this->cacheDir . '/metadata');
         $metadataFactory->setCache(new FileCache($this->cacheDir . '/metadata'));
     }
     if (!$this->handlersConfigured) {
         $this->addDefaultHandlers();
     }
     if (!$this->listenersConfigured) {
         $this->addDefaultListeners();
     }
     if (!$this->visitorsAdded) {
         $this->addDefaultSerializationVisitors();
         $this->addDefaultDeserializationVisitors();
     }
     return new Serializer($metadataFactory, $this->handlerRegistry, $this->objectConstructor ?: new UnserializeObjectConstructor(), $this->serializationVisitors, $this->deserializationVisitors, $this->eventDispatcher);
 }
Пример #19
0
 /**
  * @param string $class
  * @return ClassMetadata|null
  */
 public function getMetadataFor($class)
 {
     return $this->metadataFactory->getMetadataForClass($class);
 }
Пример #20
0
 public function build()
 {
     $annotationReader = $this->annotationReader;
     if (null === $annotationReader) {
         $annotationReader = new AnnotationReader();
         if (null !== $this->cacheDir) {
             $this->createDir($this->cacheDir . '/annotations');
             $annotationReader = new FileCacheReader($annotationReader, $this->cacheDir . '/annotations', $this->debug);
         }
     }
     if (!empty($this->metadataDirs)) {
         $fileLocator = new FileLocator($this->metadataDirs);
         $metadataDriver = new DriverChain(array(new YamlDriver($fileLocator), new XmlDriver($fileLocator), new AnnotationDriver($annotationReader)));
     } else {
         $metadataDriver = new AnnotationDriver($annotationReader);
     }
     $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
     if (null !== $this->cacheDir) {
         $this->createDir($this->cacheDir . '/metadata');
         $metadataFactory->setCache(new FileCache($this->cacheDir . '/metadata'));
     }
     if (!$this->handlersConfigured) {
         $this->addDefaultHandlers();
     }
     if (!$this->listenersConfigured) {
         $this->addDefaultListeners();
     }
     if (!$this->visitorsAdded) {
         $this->addDefaultSerializationVisitors();
         $this->addDefaultDeserializationVisitors();
     }
     return new Serializer($metadataFactory, $this->handlerRegistry, $this->objectConstructor ?: new UnserializeObjectConstructor(), $this->serializationVisitors, $this->deserializationVisitors, $this->eventDispatcher);
 }
Пример #21
0
 private function buildMetadataFactory()
 {
     $annotationReader = $this->annotationReader;
     if (null === $annotationReader) {
         $annotationReader = new AnnotationReader();
         if (null !== $this->cacheDir) {
             $this->createDir($this->cacheDir . '/annotations');
             $annotationReader = new FileCacheReader($annotationReader, $this->cacheDir . '/annotations', $this->debug);
         }
     }
     if (!empty($this->metadataDirs)) {
         $fileLocator = new FileLocator($this->metadataDirs);
         $metadataDriver = new DriverChain(array(new YamlDriver($fileLocator), new XmlDriver($fileLocator), new AnnotationDriver($annotationReader)));
     } else {
         $metadataDriver = new AnnotationDriver($annotationReader);
     }
     $metadataDriver = new ExtensionDriver($metadataDriver, $this->configurationExtensions);
     $metadataFactory = new MetadataFactory($metadataDriver, null, $this->debug);
     $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
     if (null !== $this->cacheDir) {
         $this->createDir($this->cacheDir . '/metadata');
         $metadataFactory->setCache(new FileCache($this->cacheDir . '/metadata'));
     }
     return $metadataFactory;
 }
Пример #22
0
 /**
  * Builds and return a Manager instance.
  *
  * @return Manager
  */
 public function build()
 {
     $annotationReader = $this->annotationReader;
     if (null === $annotationReader) {
         $annotationReader = new AnnotationReader();
         if (null !== $this->cacheDir) {
             $this->createDir($this->cacheDir . '/annotations');
             $annotationReader = new FileCacheReader($annotationReader, $this->cacheDir . '/annotations', $this->debug);
         }
     }
     $metadataDriver = $this->driverFactory->createDriver($this->metadataDirs, $annotationReader);
     $metadataFactory = new MetadataFactory($metadataDriver);
     $metadataFactory->setIncludeInterfaces($this->includeInterfaceMetadata);
     if (null !== $this->cacheDir) {
         $this->createDir($this->cacheDir . '/metadata');
         $metadataFactory->setCache(new FileCache($this->cacheDir . '/metadata'));
     }
     $schemaRegistry = new SchemaRegistry($this->schemaDirs);
     // TODO registry caching
     return new Manager($metadataFactory, $schemaRegistry);
 }
 private function getFactory()
 {
     $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
     $factory->setIncludeInterfaces(true);
     return $factory;
 }
Пример #24
0
 /**
  * @param string $className
  *
  * @return EntityMetadata|null
  */
 public function getEntityMetadata($className)
 {
     return class_exists($className) ? $this->metadataFactory->getMetadataForClass($className) : null;
 }
Пример #25
0
 public function testNotFoundMetadataIsNotCachedInDebug()
 {
     $driver = $this->getMock('Metadata\\Driver\\DriverInterface');
     $driver->expects($this->exactly(2))->method('loadMetadataForClass')->will($this->returnValue(null));
     $cachedMetadata = null;
     $cache = $this->getMock('Metadata\\Cache\\CacheInterface');
     $cache->expects($this->any())->method('loadClassMetadataFromCache')->with($this->equalTo(new \ReflectionClass('Metadata\\Tests\\Fixtures\\TestObject')))->will($this->returnValue(null));
     $cache->expects($this->never())->method('putClassMetadataInCache');
     $factory = new MetadataFactory($driver, 'Metadata\\ClassHierarchyMetadata', true);
     $factory->setCache($cache);
     $factory->getMetadataForClass('Metadata\\Tests\\Fixtures\\TestObject');
     $this->assertNull($factory->getMetadataForClass('Metadata\\Tests\\Fixtures\\TestObject'));
     // We use another factory with the same cache, to simulate another request and skip the in memory
     $factory = new MetadataFactory($driver, 'Metadata\\ClassHierarchyMetadata', true);
     $factory->setCache($cache);
     $factory->getMetadataForClass('Metadata\\Tests\\Fixtures\\TestObject');
     $this->assertNull($factory->getMetadataForClass('Metadata\\Tests\\Fixtures\\TestObject'));
 }