/** * (non-PHPdoc) * @see Gedmo\Mapping.Driver::readExtendedMetadata() */ public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) { require_once __DIR__ . '/../Annotations.php'; $reader = new AnnotationReader(); $reader->setAnnotationNamespaceAlias('Gedmo\\Timestampable\\Mapping\\', 'gedmo'); $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || $meta->isInheritedAssociation($property->name)) { continue; } if ($timestampable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TIMESTAMPABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw MappingException::fieldMustBeMapped($field, $meta->name); } if (!$this->_isValidField($meta, $field)) { throw MappingException::notValidFieldType($field, $meta->name); } if (!in_array($timestampable->on, array('update', 'create', 'change'))) { throw MappingException::triggerTypeInvalid($field, $meta->name); } if ($timestampable->on == 'change') { if (!isset($timestampable->field) || !isset($timestampable->value)) { throw MappingException::parametersMissing($field, $meta->name); } $field = array('field' => $field, 'trackedField' => $timestampable->field, 'value' => $timestampable->value); } // properties are unique and mapper checks that, no risk here $config[$timestampable->on][] = $field; } } }
public function setUp() { if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0RC4-DEV', '>=')) { $this->markTestSkipped('Doctrine common is 2.1.0RC4-DEV version, skipping.'); } else { if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) { $reader = new AnnotationReader(); $reader->setDefaultAnnotationNamespace('Doctrine\\ORM\\Mapping\\'); $reader->setIgnoreNotImportedAnnotations(true); $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo'); $reader->setEnableParsePhpImports(false); $reader->setAutoloadAnnotations(true); $reader = new CachedReader(new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()); } else { $reader = new AnnotationReader(); $reader->setAutoloadAnnotations(true); $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo'); $reader->setDefaultAnnotationNamespace('Doctrine\\ORM\\Mapping\\'); } } $config = new \Doctrine\ORM\Configuration(); $config->setProxyDir(TESTS_TEMP_DIR); $config->setProxyNamespace('Gedmo\\Mapping\\Proxy'); $config->setMetadataDriverImpl(new AnnotationDriver($reader)); $conn = array('driver' => 'pdo_sqlite', 'memory' => true); //$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); $evm = new \Doctrine\Common\EventManager(); $this->timestampable = new \Gedmo\Timestampable\TimestampableListener(); $evm->addEventSubscriber($this->timestampable); $this->em = \Doctrine\ORM\EntityManager::create($conn, $config, $evm); $schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->em); $schemaTool->dropSchema(array()); $schemaTool->createSchema(array($this->em->getClassMetadata(self::ARTICLE))); }
public function readExtendedMetadata(ClassMetadata $meta, array &$config) { // load our available annotations require_once __DIR__ . '/../Annotations.php'; $reader = new AnnotationReader(); // set annotation namespace and alias $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Mock\\Extension\\Encoder\\Mapping\\', 'ext'); $class = $meta->getReflectionClass(); // check only property annotations foreach ($class->getProperties() as $property) { // skip inherited properties if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || isset($meta->associationMappings[$property->name]['inherited'])) { continue; } // now lets check if property has our annotation if ($encode = $reader->getPropertyAnnotation($property, 'Gedmo\\Mapping\\Mock\\Extension\\Encoder\\Mapping\\Encode')) { $field = $property->getName(); // check if field is mapped if (!$meta->hasField($field)) { throw new \Exception("Field is not mapped as object property"); } // allow encoding only strings if (!in_array($encode->type, array('sha1', 'md5'))) { throw new \Exception("Invalid encoding type supplied"); } // validate encoding type $mapping = $meta->getFieldMapping($field); if ($mapping['type'] != 'string') { throw new \Exception("Only strings can be encoded"); } // store the metadata $config['encode'][$field] = array('type' => $encode->type, 'secret' => $encode->secret); } } }
/** * (non-PHPdoc) * @see Gedmo\Mapping.Driver::readExtendedMetadata() */ public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) { require_once __DIR__ . '/../Annotations.php'; $reader = new AnnotationReader(); $reader->setAnnotationNamespaceAlias('Gedmo\\Tree\\Mapping\\', 'gedmo'); $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || $meta->isInheritedAssociation($property->name)) { continue; } // left if ($left = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEFT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw MappingException::fieldMustBeMapped($field, $meta->name); } if (!$this->_isValidField($meta, $field)) { throw MappingException::notValidFieldType($field, $meta->name); } $config['left'] = $field; } // right if ($right = $reader->getPropertyAnnotation($property, self::ANNOTATION_RIGHT)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw MappingException::fieldMustBeMapped($field, $meta->name); } if (!$this->_isValidField($meta, $field)) { throw MappingException::notValidFieldType($field, $meta->name); } $config['right'] = $field; } // ancestor/parent if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_PARENT)) { $field = $property->getName(); if (!$meta->isSingleValuedAssociation($field)) { throw MappingException::parentFieldNotMappedOrRelated($field, $meta->name); } $config['parent'] = $field; } // level if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_LEVEL)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw MappingException::fieldMustBeMapped($field, $meta->name); } if (!$this->_isValidField($meta, $field)) { throw MappingException::notValidFieldType($field, $meta->name); } $config['level'] = $field; } } }
/** * (non-PHPdoc) * @see Gedmo\Mapping.Driver::readExtendedMetadata() */ public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) { require_once __DIR__ . '/../Annotations.php'; $reader = new AnnotationReader(); $reader->setAnnotationNamespaceAlias('Gedmo\\Translatable\\Mapping\\', 'gedmo'); $class = $meta->getReflectionClass(); // class annotations $classAnnotations = $reader->getClassAnnotations($class); if (isset($classAnnotations[self::ANNOTATION_ENTITY_CLASS])) { $annot = $classAnnotations[self::ANNOTATION_ENTITY_CLASS]; if (!class_exists($annot->class)) { throw MappingException::translationClassNotFound($annot->class); } $config['translationClass'] = $annot->class; } // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || $meta->isInheritedAssociation($property->name)) { continue; } // translatable property if ($translatable = $reader->getPropertyAnnotation($property, self::ANNOTATION_TRANSLATABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw MappingException::fieldMustBeMapped($field, $meta->name); } if (!$this->_isValidField($meta, $field)) { throw MappingException::notValidFieldType($field, $meta->name); } // fields cannot be overrided and throws mapping exception $config['fields'][] = $field; } // locale property if ($locale = $reader->getPropertyAnnotation($property, self::ANNOTATION_LOCALE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw MappingException::fieldMustNotBeMapped($field, $meta->name); } $config['locale'] = $field; } elseif ($language = $reader->getPropertyAnnotation($property, self::ANNOTATION_LANGUAGE)) { $field = $property->getName(); if ($meta->hasField($field)) { throw MappingException::fieldMustNotBeMapped($field, $meta->name); } $config['locale'] = $field; } } }
/** * (non-PHPdoc) * @see Gedmo\Mapping.Driver::readExtendedMetadata() */ public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) { require_once __DIR__ . '/../Annotations.php'; $reader = new AnnotationReader(); $reader->setAnnotationNamespaceAlias('Gedmo\\Sluggable\\Mapping\\', 'gedmo'); $class = $meta->getReflectionClass(); // property annotations foreach ($class->getProperties() as $property) { if ($meta->isMappedSuperclass && !$property->isPrivate() || $meta->isInheritedField($property->name) || $meta->isInheritedAssociation($property->name)) { continue; } // sluggable property if ($sluggable = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUGGABLE)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw MappingException::fieldMustBeMapped($field, $meta->name); } if (!$this->_isValidField($meta, $field)) { throw MappingException::notValidFieldType($field, $meta->name); } $config['fields'][] = $field; } // slug property if ($slug = $reader->getPropertyAnnotation($property, self::ANNOTATION_SLUG)) { $field = $property->getName(); if (!$meta->hasField($field)) { throw MappingException::slugFieldMustBeMapped($field, $meta->name); } if (!$this->_isValidField($meta, $field)) { throw MappingException::notValidFieldType($field, $meta->name); } if (isset($config['slug'])) { throw MappingException::slugFieldIsDuplicate($field, $meta->name); } $config['slug'] = $field; $config['style'] = $slug->style; $config['updatable'] = $slug->updatable; $config['unique'] = $slug->unique; $config['separator'] = $slug->separator; } } }
/** * Create default annotation reader for extensions * * @return \Doctrine\Common\Annotations\AnnotationReader */ private function getDefaultAnnotationReader() { if (null === self::$defaultAnnotationReader) { if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) { $reader = new \Doctrine\Common\Annotations\AnnotationReader(); \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace('Gedmo\\Mapping\\Annotation', __DIR__ . '/../../'); $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache()); } else { if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0RC4-DEV', '>=')) { $reader = new \Doctrine\Common\Annotations\AnnotationReader(); \Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace('Gedmo\\Mapping\\Annotation', __DIR__ . '/../../'); $reader->setDefaultAnnotationNamespace('Doctrine\\ORM\\Mapping\\'); $reader = new \Doctrine\Common\Annotations\CachedReader($reader, new ArrayCache()); } else { if (version_compare(\Doctrine\Common\Version::VERSION, '2.1.0-BETA3-DEV', '>=')) { $reader = new \Doctrine\Common\Annotations\AnnotationReader(); $reader->setDefaultAnnotationNamespace('Doctrine\\ORM\\Mapping\\'); $reader->setIgnoreNotImportedAnnotations(true); $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo'); $reader->setEnableParsePhpImports(false); $reader->setAutoloadAnnotations(true); $reader = new \Doctrine\Common\Annotations\CachedReader(new \Doctrine\Common\Annotations\IndexedReader($reader), new ArrayCache()); } else { $reader = new \Doctrine\Common\Annotations\AnnotationReader(); $reader->setAutoloadAnnotations(true); $reader->setAnnotationNamespaceAlias('Gedmo\\Mapping\\Annotation\\', 'gedmo'); $reader->setDefaultAnnotationNamespace('Doctrine\\ORM\\Mapping\\'); } } } self::$defaultAnnotationReader = $reader; } return self::$defaultAnnotationReader; }
public function loadDoctrineConfiguration(Application $app) { $app['doctrine.configuration'] = $app->share(function () use($app) { if (isset($app['doctrine.orm']) and true === $app['doctrine.orm']) { $config = new ORMConfiguration(); $cache = new ApcCache(); $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); $chain = new DriverChain(); foreach ((array) $app['doctrine.orm.entities'] as $entity) { switch ($entity['type']) { case 'annotation': $reader = new AnnotationReader(); $reader->setAnnotationNamespaceAlias('Doctrine\\ORM\\Mapping\\', 'orm'); $driver = new AnnotationDriver($reader, (array) $entity['path']); $chain->addDriver($driver, $entity['namespace']); break; case 'yml': $driver = new YamlDriver((array) $entity['path']); $driver->setFileExtension('.yml'); $chain->addDriver($driver, $entity['namespace']); break; case 'xml': $driver = new XmlDriver((array) $entity['path'], $entity['namespace']); $driver->setFileExtension('.xml'); $chain->addDriver($driver, $entity['namespace']); break; default: throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $type)); break; } } $config->setMetadataDriverImpl($chain); $config->setProxyDir($app['doctrine.orm.proxies_dir']); $config->setProxyNamespace($app['doctrine.orm.proxies_namespace']); $config->setAutoGenerateProxyClasses($app['doctrine.orm.auto_generate_proxies']); } else { $config = new DBALConfiguration(); } return $config; }); }
/** * Build the AnnotationLoader with an AnnotationReader * * @param AnnotationReader $reader */ public function __construct(AnnotationReader $reader) { $this->reader = $reader; $this->reader->setAutoloadAnnotations(true); $this->reader->setAnnotationNamespaceAlias('Hitch\\Mapping\\Annotation\\', 'xml'); }