123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170 |
- <?php
- /**
- * Modifications of the entity generator class from Doctrine (Doctrine\ORM\Tools\EntityGenerator)
- * to account for chamilo.
- *
- * Most functions being private it is not possible to inherit from it.
- *
- * Original work, credits, licence see Docrine
- *
- *
- */
- namespace Tools;
- use Doctrine\ORM\Mapping\ClassMetadataInfo,
- Doctrine\ORM\Mapping\AssociationMapping,
- Doctrine\Common\Util\Inflector;
- /**
- * Generic class used to generate PHP5 entity classes from ClassMetadataInfo instances
- *
- * [php]
- * $classes = $em->getClassMetadataFactory()->getAllMetadata();
- *
- * $generator = new \Doctrine\ORM\Tools\EntityGenerator();
- * $generator->setGenerateAnnotations(true);
- * $generator->setGenerateStubMethods(true);
- * $generator->setRegenerateEntityIfExists(false);
- * $generator->setUpdateEntityIfExists(true);
- * $generator->generate($classes, '/path/to/generate/entities');
- *
- * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
- * @link www.doctrine-project.org
- * @since 2.0
- * @version $Revision$
- * @author Benjamin Eberlei <kontakt@beberlei.de>
- * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
- * @author Jonathan Wage <jonwage@gmail.com>
- * @author Roman Borschel <roman@code-factory.org>
- */
- class EntityGenerator
- {
- /**
- * @var bool
- */
- private $_backupExisting = true;
- /** The extension to use for written php files */
- private $_extension = '.php';
- /** Whether or not the current ClassMetadataInfo instance is new or old */
- private $_isNew = true;
- private $_staticReflection = array();
- /** Number of spaces to use for indention in generated code */
- private $_numSpaces = 4;
- /** The actual spaces to use for indention */
- private $_spaces = ' ';
- /** The class all generated entities should extend */
- private $_classToExtend;
- /** Whether or not to generation annotations */
- private $_generateAnnotations = false;
- /**
- * @var string
- */
- private $_annotationsPrefix = '';
- /** Whether or not to generated sub methods */
- private $_generateEntityStubMethods = false;
- /** Whether or not to update the entity class if it exists already */
- private $_updateEntityIfExists = false;
- /** Whether or not to re-generate entity class if it exists already */
- private $_regenerateEntityIfExists = false;
- private static $_classTemplate =
- '<?php
- namespace Entity;
- use Doctrine\ORM\Mapping as ORM;
- /**
- *
- * @license see /license.txt
- * @author autogenerated
- */
- <entityClassName>
- {
- <spaces>/**
- <spaces> * @return \Entity\Repository\<entityName>Repository
- <spaces> */
- <spaces> public static function repository(){
- <spaces><spaces>return \Entity\Repository\<entityName>Repository::instance();
- <spaces>}
- <spaces>/**
- <spaces> * @return \Entity\<entityName>
- <spaces> */
- <spaces> public static function create(){
- <spaces><spaces>return new self();
- <spaces>}
- <entityBody>
- }';
- private static $_getMethodTemplate =
- '/**
- * <description>
- *
- * @return <variableType>
- */
- public function <methodName>()
- {
- <spaces>return $this-><fieldName>;
- }';
- private static $_setMethodTemplate =
- '/**
- * <description>
- *
- * @param <variableType>$<variableName>
- * @return <entity>
- */
- public function <methodName>(<methodTypeHint>$<variableName><variableDefault>)
- {
- <spaces>$this-><fieldName> = $<variableName>;
- <spaces>return $this;
- }';
- private static $_addMethodTemplate =
- '/**
- * <description>
- *
- * @param <variableType>$<variableName>
- * @return <entity>
- */
- public function <methodName>(<methodTypeHint>$<variableName>)
- {
- <spaces>$this-><fieldName>[] = $<variableName>;
- <spaces>return $this;
- }';
- private static $_lifecycleCallbackMethodTemplate =
- '/**
- * @<name>
- */
- public function <methodName>()
- {
- <spaces>// Add your code here
- }';
- private static $_constructorMethodTemplate =
- 'public function __construct()
- {
- <spaces><collections>
- }
- ';
- public function __construct()
- {
- if (version_compare(\Doctrine\Common\Version::VERSION, '2.2.0-DEV', '>=')) {
- $this->_annotationsPrefix = 'ORM\\';
- }
- }
- /**
- * Generate and write entity classes for the given array of ClassMetadataInfo instances
- *
- * @param array $metadatas
- * @param string $outputDirectory
- * @return void
- */
- public function generate(array $metadatas, $outputDirectory)
- {
- foreach ($metadatas as $metadata) {
- $this->writeEntityClass($metadata, $outputDirectory);
- }
- }
- /**
- * Generated and write entity class to disk for the given ClassMetadataInfo instance
- *
- * @param ClassMetadataInfo $metadata
- * @param string $outputDirectory
- * @return void
- */
- public function writeEntityClass(ClassMetadataInfo $metadata, $outputDirectory)
- {
- //change
- $name = $metadata->name;
- $name = explode('\\', $name);
- $name = end($name);
- $name = Inflector::tableize($name);
- $is_course_table = (strpos($name, 'c_') === 0);
- if ($is_course_table) {
- $name = substr($name, 2, strlen($name) - 2);
- }
- $name = Inflector::tableize($name);
- //
- $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $name) . $this->_extension;
- $dir = dirname($path);
- if (!is_dir($dir)) {
- mkdir($dir, 0777, true);
- }
- $this->_isNew = !file_exists($path) || (file_exists($path) && $this->_regenerateEntityIfExists);
- if (!$this->_isNew) {
- $this->_parseTokensInEntityFile(file_get_contents($path));
- } else {
- $this->_staticReflection[$metadata->name] = array('properties' => array(), 'methods' => array());
- }
- if ($this->_backupExisting && file_exists($path)) {
- $backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . "~";
- if (!copy($path, $backupPath)) {
- throw new \RuntimeException("Attempt to backup overwritten entity file but copy operation failed.");
- }
- }
- // If entity doesn't exist or we're re-generating the entities entirely
- if ($this->_isNew) {
- file_put_contents($path, $this->generateEntityClass($metadata));
- // If entity exists and we're allowed to update the entity class
- } else if (!$this->_isNew && $this->_updateEntityIfExists) {
- file_put_contents($path, $this->generateUpdatedEntityClass($metadata, $path));
- }
- }
- /**
- * Generate a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance
- *
- * @param ClassMetadataInfo $metadata
- * @return string $code
- */
- public function generateEntityClass(ClassMetadataInfo $metadata)
- {
- $placeHolders = array(
- '<namespace>',
- '<entityAnnotation>',
- '<entityClassName>',
- '<entityBody>'
- );
- $replacements = array(
- $this->_generateEntityNamespace($metadata),
- $this->_generateEntityDocBlock($metadata),
- $this->_generateEntityClassName($metadata),
- $this->_generateEntityBody($metadata)
- );
-
- $code = str_replace($placeHolders, $replacements, self::$_classTemplate);
- $result = str_replace('<spaces>', $this->_spaces, $code);
- $result = str_replace('<entityName>', $this->_getClassName($metadata), $result);
- return $result;
- }
- /**
- * Generate the updated code for the given ClassMetadataInfo and entity at path
- *
- * @param ClassMetadataInfo $metadata
- * @param string $path
- * @return string $code;
- */
- public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path)
- {
- $currentCode = file_get_contents($path);
- $body = $this->_generateEntityBody($metadata);
- $body = str_replace('<spaces>', $this->_spaces, $body);
- $last = strrpos($currentCode, '}');
- return substr($currentCode, 0, $last) . $body . (strlen($body) > 0 ? "\n" : '') . "}";
- }
- /**
- * Set the number of spaces the exported class should have
- *
- * @param integer $numSpaces
- * @return void
- */
- public function setNumSpaces($numSpaces)
- {
- $this->_spaces = str_repeat(' ', $numSpaces);
- $this->_numSpaces = $numSpaces;
- }
- /**
- * Set the extension to use when writing php files to disk
- *
- * @param string $extension
- * @return void
- */
- public function setExtension($extension)
- {
- $this->_extension = $extension;
- }
- /**
- * Set the name of the class the generated classes should extend from
- *
- * @return void
- */
- public function setClassToExtend($classToExtend)
- {
- $this->_classToExtend = $classToExtend;
- }
- /**
- * Set whether or not to generate annotations for the entity
- *
- * @param bool $bool
- * @return void
- */
- public function setGenerateAnnotations($bool)
- {
- $this->_generateAnnotations = $bool;
- }
- /**
- * Set an annotation prefix.
- *
- * @param string $prefix
- */
- public function setAnnotationPrefix($prefix)
- {
- $this->_annotationsPrefix = $prefix;
- }
- /**
- * Set whether or not to try and update the entity if it already exists
- *
- * @param bool $bool
- * @return void
- */
- public function setUpdateEntityIfExists($bool)
- {
- $this->_updateEntityIfExists = $bool;
- }
- /**
- * Set whether or not to regenerate the entity if it exists
- *
- * @param bool $bool
- * @return void
- */
- public function setRegenerateEntityIfExists($bool)
- {
- $this->_regenerateEntityIfExists = $bool;
- }
- /**
- * Set whether or not to generate stub methods for the entity
- *
- * @param bool $bool
- * @return void
- */
- public function setGenerateStubMethods($bool)
- {
- $this->_generateEntityStubMethods = $bool;
- }
- /**
- * Should an existing entity be backed up if it already exists?
- */
- public function setBackupExisting($bool)
- {
- $this->_backupExisting = $bool;
- }
- private function _generateEntityNamespace(ClassMetadataInfo $metadata)
- {
- if ($this->_hasNamespace($metadata)) {
- return 'namespace ' . $this->_getNamespace($metadata) . ';';
- }
- }
- private function _generateEntityClassName(ClassMetadataInfo $metadata)
- {
- return 'class ' . $this->_getClassName($metadata) .
- ($this->_extendsClass() ? ' extends ' . $this->_getClassToExtendName($metadata) : null);
- }
- private function _generateEntityBody(ClassMetadataInfo $metadata)
- {
- $fieldMappingProperties = $this->_generateEntityFieldMappingProperties($metadata);
- $associationMappingProperties = $this->_generateEntityAssociationMappingProperties($metadata);
- $stubMethods = $this->_generateEntityStubMethods ? $this->_generateEntityStubMethods($metadata) : null;
- $lifecycleCallbackMethods = $this->_generateEntityLifecycleCallbackMethods($metadata);
- $code = array();
- if ($fieldMappingProperties) {
- $code[] = $fieldMappingProperties;
- }
- if ($associationMappingProperties) {
- $code[] = $associationMappingProperties;
- }
- $code[] = $this->_generateEntityConstructor($metadata);
- if ($stubMethods) {
- $code[] = $stubMethods;
- }
- if ($lifecycleCallbackMethods) {
- $code[] = $lifecycleCallbackMethods;
- }
- return implode("\n", $code);
- }
- private function _generateEntityConstructor(ClassMetadataInfo $metadata)
- {
- if ($this->_hasMethod('__construct', $metadata)) {
- return '';
- }
- $collections = array();
- foreach ($metadata->associationMappings AS $mapping) {
- if ($mapping['type'] & ClassMetadataInfo::TO_MANY) {
- $collections[] = '$this->' . $mapping['fieldName'] . ' = new \Doctrine\Common\Collections\ArrayCollection();';
- }
- }
- if ($collections) {
- return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n" . $this->_spaces, $collections), self::$_constructorMethodTemplate));
- }
- return '';
- }
- /**
- * @todo this won't work if there is a namespace in brackets and a class outside of it.
- * @param string $src
- */
- private function _parseTokensInEntityFile($src)
- {
- $tokens = token_get_all($src);
- $lastSeenNamespace = "";
- $lastSeenClass = false;
- $inNamespace = false;
- $inClass = false;
- for ($i = 0; $i < count($tokens); $i++) {
- $token = $tokens[$i];
- if (in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT))) {
- continue;
- }
- if ($inNamespace) {
- if ($token[0] == T_NS_SEPARATOR || $token[0] == T_STRING) {
- $lastSeenNamespace .= $token[1];
- } else if (is_string($token) && in_array($token, array(';', '{'))) {
- $inNamespace = false;
- }
- }
- if ($inClass) {
- $inClass = false;
- $lastSeenClass = $lastSeenNamespace . ($lastSeenNamespace ? '\\' : '') . $token[1];
- $this->_staticReflection[$lastSeenClass]['properties'] = array();
- $this->_staticReflection[$lastSeenClass]['methods'] = array();
- }
- if ($token[0] == T_NAMESPACE) {
- $lastSeenNamespace = "";
- $inNamespace = true;
- } else if ($token[0] == T_CLASS) {
- $inClass = true;
- } else if ($token[0] == T_FUNCTION) {
- if ($tokens[$i + 2][0] == T_STRING) {
- $this->_staticReflection[$lastSeenClass]['methods'][] = $tokens[$i + 2][1];
- } else if ($tokens[$i + 2] == "&" && $tokens[$i + 3][0] == T_STRING) {
- $this->_staticReflection[$lastSeenClass]['methods'][] = $tokens[$i + 3][1];
- }
- } else if (in_array($token[0], array(T_VAR, T_PUBLIC, T_PRIVATE, T_PROTECTED)) && $tokens[$i + 2][0] != T_FUNCTION) {
- $this->_staticReflection[$lastSeenClass]['properties'][] = substr($tokens[$i + 2][1], 1);
- }
- }
- }
- private function _hasProperty($property, ClassMetadataInfo $metadata)
- {
- if ($this->_extendsClass()) {
- // don't generate property if its already on the base class.
- $reflClass = new \ReflectionClass($this->_getClassToExtend($metadata));
- if ($reflClass->hasProperty($property)) {
- return true;
- }
- }
- return (
- isset($this->_staticReflection[$metadata->name]) &&
- in_array($property, $this->_staticReflection[$metadata->name]['properties'])
- );
- }
- private function _hasMethod($method, ClassMetadataInfo $metadata)
- {
- if ($this->_extendsClass()) {
- // don't generate method if its already on the base class.
- $reflClass = new \ReflectionClass($this->_getClassToExtend($metadata));
- if ($reflClass->hasMethod($method)) {
- return true;
- }
- }
- return (
- isset($this->_staticReflection[$metadata->name]) &&
- in_array($method, $this->_staticReflection[$metadata->name]['methods'])
- );
- }
- private function _hasNamespace(ClassMetadataInfo $metadata)
- {
- return strpos($metadata->name, '\\') ? true : false;
- }
- private function _extendsClass()
- {
- return $this->_classToExtend ? true : false;
- }
- private function _getClassToExtend($metadata)
- {
- return isset($metadata->class_to_extend) ? $metadata->class_to_extend : $this->_classToExtend;
- }
- private function _getClassToExtendName($metadata)
- {
- $refl = new \ReflectionClass($this->_getClassToExtend($metadata));
- return '\\' . $refl->getName();
- }
- private function _getClassName(ClassMetadataInfo $metadata)
- {
- //changed
- $name = $metadata->name;
- $name = explode('\\', $name);
- $name = end($name);
- $name = Inflector::tableize($name);
- $is_course_table = (strpos($name, 'c_') === 0);
- if ($is_course_table) {
- $name = substr($name, 2, strlen($name) - 2);
- }
- $name = Inflector::classify($name);
- $result = ($pos = strrpos($name, '\\')) ? substr($name, $pos + 1, strlen($name)) : $name;
- return $result;
- }
- private function _getNamespace(ClassMetadataInfo $metadata)
- {
- return substr($metadata->name, 0, strrpos($metadata->name, '\\'));
- }
- private function _generateEntityDocBlock(ClassMetadataInfo $metadata)
- {
- $lines = array();
- $lines[] = '/**';
- $lines[] = ' * ' . $metadata->name;
- if ($this->_generateAnnotations) {
- $lines[] = ' *';
- $methods = array(
- '_generateTableAnnotation',
- '_generateInheritanceAnnotation',
- '_generateDiscriminatorColumnAnnotation',
- '_generateDiscriminatorMapAnnotation'
- );
- foreach ($methods as $method) {
- if ($code = $this->$method($metadata)) {
- $lines[] = ' * ' . $code;
- }
- }
- if ($metadata->isMappedSuperclass) {
- $lines[] = ' * @' . $this->_annotationsPrefix . 'MappedSuperClass';
- } else {
- $lines[] = ' * @' . $this->_annotationsPrefix . 'Entity';
- }
- if ($metadata->customRepositoryClassName) {
- $lines[count($lines) - 1] .= '(repositoryClass="' . $metadata->customRepositoryClassName . '")';
- }
- if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
- $lines[] = ' * @' . $this->_annotationsPrefix . 'HasLifecycleCallbacks';
- }
- }
- $lines[] = ' */';
- return implode("\n", $lines);
- }
- private function _generateTableAnnotation($metadata)
- {
- $table = array();
- if (isset($metadata->table['schema'])) {
- $table[] = 'schema="' . $metadata->table['schema'] . '"';
- }
- if (isset($metadata->table['name'])) {
- $table[] = 'name="' . $metadata->table['name'] . '"';
- }
- if (isset($metadata->table['uniqueConstraints']) && $metadata->table['uniqueConstraints']) {
- $constraints = $this->_generateTableConstraints('UniqueConstraint', $metadata->table['uniqueConstraints']);
- $table[] = 'uniqueConstraints={' . $constraints . '}';
- }
- if (isset($metadata->table['indexes']) && $metadata->table['indexes']) {
- $constraints = $this->_generateTableConstraints('Index', $metadata->table['indexes']);
- $table[] = 'indexes={' . $constraints . '}';
- }
- return '@' . $this->_annotationsPrefix . 'Table(' . implode(', ', $table) . ')';
- }
- private function _generateTableConstraints($constraintName, $constraints)
- {
- $annotations = array();
- foreach ($constraints as $name => $constraint) {
- $columns = array();
- foreach ($constraint['columns'] as $column) {
- $columns[] = '"' . $column . '"';
- }
- $annotations[] = '@' . $this->_annotationsPrefix . $constraintName . '(name="' . $name . '", columns={' . implode(', ', $columns) . '})';
- }
- return implode(', ', $annotations);
- }
- private function _generateInheritanceAnnotation($metadata)
- {
- if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
- return '@' . $this->_annotationsPrefix . 'InheritanceType("' . $this->_getInheritanceTypeString($metadata->inheritanceType) . '")';
- }
- }
- private function _generateDiscriminatorColumnAnnotation($metadata)
- {
- if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
- $discrColumn = $metadata->discriminatorValue;
- $columnDefinition = 'name="' . $discrColumn['name']
- . '", type="' . $discrColumn['type']
- . '", length=' . $discrColumn['length'];
- return '@' . $this->_annotationsPrefix . 'DiscriminatorColumn(' . $columnDefinition . ')';
- }
- }
- private function _generateDiscriminatorMapAnnotation($metadata)
- {
- if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
- $inheritanceClassMap = array();
- foreach ($metadata->discriminatorMap as $type => $class) {
- $inheritanceClassMap[] .= '"' . $type . '" = "' . $class . '"';
- }
- return '@' . $this->_annotationsPrefix . 'DiscriminatorMap({' . implode(', ', $inheritanceClassMap) . '})';
- }
- }
- private function _generateEntityStubMethods(ClassMetadataInfo $metadata)
- {
- $methods = array();
- foreach ($metadata->fieldMappings as $fieldMapping) {
- if (!isset($fieldMapping['id']) || !$fieldMapping['id'] || $metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE) {
- if ($code = $this->_generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) {
- $methods[] = $code;
- }
- }
- if ($code = $this->_generateEntityStubMethod($metadata, 'get', $fieldMapping['fieldName'], $fieldMapping['type'])) {
- $methods[] = $code;
- }
- }
- foreach ($metadata->associationMappings as $associationMapping) {
- if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
- $nullable = $this->_isAssociationIsNullable($associationMapping) ? 'null' : null;
- if ($code = $this->_generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'], $nullable)) {
- $methods[] = $code;
- }
- if ($code = $this->_generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
- $methods[] = $code;
- }
- } else if ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
- if ($code = $this->_generateEntityStubMethod($metadata, 'add', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
- $methods[] = $code;
- }
- if ($code = $this->_generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], 'Doctrine\Common\Collections\Collection')) {
- $methods[] = $code;
- }
- }
- }
-
- return implode("\n\n", $methods);
- }
- private function _isAssociationIsNullable($associationMapping)
- {
- if (isset($associationMapping['id']) && $associationMapping['id']) {
- return false;
- }
- if (isset($associationMapping['joinColumns'])) {
- $joinColumns = $associationMapping['joinColumns'];
- } else {
- //@todo thereis no way to retreive targetEntity metadata
- $joinColumns = array();
- }
- foreach ($joinColumns as $joinColumn) {
- if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
- return false;
- }
- }
- return true;
- }
- private function _generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata)
- {
- if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
- $methods = array();
- foreach ($metadata->lifecycleCallbacks as $name => $callbacks) {
- foreach ($callbacks as $callback) {
- if ($code = $this->_generateLifecycleCallbackMethod($name, $callback, $metadata)) {
- $methods[] = $code;
- }
- }
- }
- return implode("\n\n", $methods);
- }
- return "";
- }
- private function _generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata)
- {
- $lines = array();
- foreach ($metadata->associationMappings as $associationMapping) {
- if ($this->_hasProperty($associationMapping['fieldName'], $metadata)) {
- continue;
- }
- $lines[] = $this->_generateAssociationMappingPropertyDocBlock($associationMapping, $metadata);
- $lines[] = $this->_spaces . 'protected $' . $associationMapping['fieldName']
- . ($associationMapping['type'] == 'manyToMany' ? ' = array()' : null) . ";\n";
- }
- return implode("\n", $lines);
- }
- private function _generateEntityFieldMappingProperties(ClassMetadataInfo $metadata)
- {
- $lines = array();
- foreach ($metadata->fieldMappings as $fieldMapping) {
- if ($this->_hasProperty($fieldMapping['fieldName'], $metadata) ||
- $metadata->isInheritedField($fieldMapping['fieldName'])) {
- continue;
- }
- $fieldMapping['fieldName'] = Inflector::tableize($fieldMapping['fieldName']);
- $lines[] = $this->_generateFieldMappingPropertyDocBlock($fieldMapping, $metadata);
- $lines[] = $this->_spaces . 'protected $' . $fieldMapping['fieldName']
- . (isset($fieldMapping['default']) ? ' = ' . var_export($fieldMapping['default'], true) : null) . ";\n";
- }
- return implode("\n", $lines);
- }
- private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null)
- {
- $fieldName = Inflector::tableize($fieldName);
- if ($type == "add") {
- $addMethod = explode("\\", $typeHint);
- $addMethod = end($addMethod);
- $methodName = $type . $addMethod;
- } else {
- //change
- $methodName = $type . '_' . Inflector::tableize($fieldName);
- //
- }
- if ($this->_hasMethod($methodName, $metadata)) {
- return;
- }
- $this->_staticReflection[$metadata->name]['methods'][] = $methodName;
- $var = sprintf('_%sMethodTemplate', $type);
- $template = self::$$var;
- $variableType = $typeHint ? $typeHint . ' ' : null;
- $types = \Doctrine\DBAL\Types\Type::getTypesMap();
- $methodTypeHint = $typeHint && !isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null;
- $replacements = array(
- '<description>' => ucfirst($type) . ' ' . $fieldName,
- '<methodTypeHint>' => $methodTypeHint,
- '<variableType>' => $variableType,
- '<variableName>' => 'value',
- '<methodName>' => $methodName,
- '<fieldName>' => $fieldName,
- '<variableDefault>' => ($defaultValue !== null ) ? (' = ' . $defaultValue) : '',
- '<entity>' => $this->_getClassName($metadata)
- );
- $method = str_replace(
- array_keys($replacements), array_values($replacements), $template
- );
- return $this->_prefixCodeWithSpaces($method);
- }
- private function _generateLifecycleCallbackMethod($name, $methodName, $metadata)
- {
- if ($this->_hasMethod($methodName, $metadata)) {
- return;
- }
- $this->_staticReflection[$metadata->name]['methods'][] = $methodName;
- $replacements = array(
- '<name>' => $this->_annotationsPrefix . $name,
- '<methodName>' => $methodName,
- );
- $method = str_replace(
- array_keys($replacements), array_values($replacements), self::$_lifecycleCallbackMethodTemplate
- );
- return $this->_prefixCodeWithSpaces($method);
- }
- private function _generateJoinColumnAnnotation(array $joinColumn)
- {
- $joinColumnAnnot = array();
- if (isset($joinColumn['name'])) {
- $joinColumnAnnot[] = 'name="' . $joinColumn['name'] . '"';
- }
- if (isset($joinColumn['referencedColumnName'])) {
- $joinColumnAnnot[] = 'referencedColumnName="' . $joinColumn['referencedColumnName'] . '"';
- }
- if (isset($joinColumn['unique']) && $joinColumn['unique']) {
- $joinColumnAnnot[] = 'unique=' . ($joinColumn['unique'] ? 'true' : 'false');
- }
- if (isset($joinColumn['nullable'])) {
- $joinColumnAnnot[] = 'nullable=' . ($joinColumn['nullable'] ? 'true' : 'false');
- }
- if (isset($joinColumn['onDelete'])) {
- $joinColumnAnnot[] = 'onDelete="' . ($joinColumn['onDelete'] . '"');
- }
- if (isset($joinColumn['columnDefinition'])) {
- $joinColumnAnnot[] = 'columnDefinition="' . $joinColumn['columnDefinition'] . '"';
- }
- return '@' . $this->_annotationsPrefix . 'JoinColumn(' . implode(', ', $joinColumnAnnot) . ')';
- }
- private function _generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata)
- {
- $lines = array();
- $lines[] = $this->_spaces . '/**';
- if ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
- $lines[] = $this->_spaces . ' * @var \Doctrine\Common\Collections\ArrayCollection';
- } else {
- $lines[] = $this->_spaces . ' * @var ' . $associationMapping['targetEntity'];
- }
- if ($this->_generateAnnotations) {
- $lines[] = $this->_spaces . ' *';
- if (isset($associationMapping['id']) && $associationMapping['id']) {
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Id';
- if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")';
- }
- }
- $type = null;
- switch ($associationMapping['type']) {
- case ClassMetadataInfo::ONE_TO_ONE:
- $type = 'OneToOne';
- break;
- case ClassMetadataInfo::MANY_TO_ONE:
- $type = 'ManyToOne';
- break;
- case ClassMetadataInfo::ONE_TO_MANY:
- $type = 'OneToMany';
- break;
- case ClassMetadataInfo::MANY_TO_MANY:
- $type = 'ManyToMany';
- break;
- }
- $typeOptions = array();
- if (isset($associationMapping['targetEntity'])) {
- $typeOptions[] = 'targetEntity="' . $associationMapping['targetEntity'] . '"';
- }
- if (isset($associationMapping['inversedBy'])) {
- $typeOptions[] = 'inversedBy="' . $associationMapping['inversedBy'] . '"';
- }
- if (isset($associationMapping['mappedBy'])) {
- $typeOptions[] = 'mappedBy="' . $associationMapping['mappedBy'] . '"';
- }
- if ($associationMapping['cascade']) {
- $cascades = array();
- if ($associationMapping['isCascadePersist'])
- $cascades[] = '"persist"';
- if ($associationMapping['isCascadeRemove'])
- $cascades[] = '"remove"';
- if ($associationMapping['isCascadeDetach'])
- $cascades[] = '"detach"';
- if ($associationMapping['isCascadeMerge'])
- $cascades[] = '"merge"';
- if ($associationMapping['isCascadeRefresh'])
- $cascades[] = '"refresh"';
- $typeOptions[] = 'cascade={' . implode(',', $cascades) . '}';
- }
- if (isset($associationMapping['orphanRemoval']) && $associationMapping['orphanRemoval']) {
- $typeOptions[] = 'orphanRemoval=' . ($associationMapping['orphanRemoval'] ? 'true' : 'false');
- }
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . '' . $type . '(' . implode(', ', $typeOptions) . ')';
- if (isset($associationMapping['joinColumns']) && $associationMapping['joinColumns']) {
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'JoinColumns({';
- $joinColumnsLines = array();
- foreach ($associationMapping['joinColumns'] as $joinColumn) {
- if ($joinColumnAnnot = $this->_generateJoinColumnAnnotation($joinColumn)) {
- $joinColumnsLines[] = $this->_spaces . ' * ' . $joinColumnAnnot;
- }
- }
- $lines[] = implode(",\n", $joinColumnsLines);
- $lines[] = $this->_spaces . ' * })';
- }
- if (isset($associationMapping['joinTable']) && $associationMapping['joinTable']) {
- $joinTable = array();
- $joinTable[] = 'name="' . $associationMapping['joinTable']['name'] . '"';
- if (isset($associationMapping['joinTable']['schema'])) {
- $joinTable[] = 'schema="' . $associationMapping['joinTable']['schema'] . '"';
- }
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'JoinTable(' . implode(', ', $joinTable) . ',';
- $lines[] = $this->_spaces . ' * joinColumns={';
- foreach ($associationMapping['joinTable']['joinColumns'] as $joinColumn) {
- $lines[] = $this->_spaces . ' * ' . $this->_generateJoinColumnAnnotation($joinColumn);
- }
- $lines[] = $this->_spaces . ' * },';
- $lines[] = $this->_spaces . ' * inverseJoinColumns={';
- foreach ($associationMapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
- $lines[] = $this->_spaces . ' * ' . $this->_generateJoinColumnAnnotation($joinColumn);
- }
- $lines[] = $this->_spaces . ' * }';
- $lines[] = $this->_spaces . ' * )';
- }
- if (isset($associationMapping['orderBy'])) {
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'OrderBy({';
- foreach ($associationMapping['orderBy'] as $name => $direction) {
- $lines[] = $this->_spaces . ' * "' . $name . '"="' . $direction . '",';
- }
- $lines[count($lines) - 1] = substr($lines[count($lines) - 1], 0, strlen($lines[count($lines) - 1]) - 1);
- $lines[] = $this->_spaces . ' * })';
- }
- }
- $lines[] = $this->_spaces . ' */';
- return implode("\n", $lines);
- }
- private function _generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata)
- {
- $lines = array();
- $lines[] = $this->_spaces . '/**';
- $lines[] = $this->_spaces . ' * @var ' . $fieldMapping['type'] . ' $' . $fieldMapping['fieldName'];
- if ($this->_generateAnnotations) {
- $lines[] = $this->_spaces . ' *';
- $column = array();
- if (isset($fieldMapping['columnName'])) {
- $column[] = 'name="' . $fieldMapping['columnName'] . '"';
- }
- if (isset($fieldMapping['type'])) {
- $column[] = 'type="' . $fieldMapping['type'] . '"';
- }
- if (isset($fieldMapping['length'])) {
- $column[] = 'length=' . $fieldMapping['length'];
- }
- if (isset($fieldMapping['precision'])) {
- $column[] = 'precision=' . $fieldMapping['precision'];
- }
- if (isset($fieldMapping['scale'])) {
- $column[] = 'scale=' . $fieldMapping['scale'];
- }
- if (isset($fieldMapping['nullable'])) {
- $column[] = 'nullable=' . var_export($fieldMapping['nullable'], true);
- }
- if (isset($fieldMapping['columnDefinition'])) {
- $column[] = 'columnDefinition="' . $fieldMapping['columnDefinition'] . '"';
- }
- if (isset($fieldMapping['unique'])) {
- $column[] = 'unique=' . var_export($fieldMapping['unique'], true);
- }
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Column(' . implode(', ', $column) . ')';
- if (isset($fieldMapping['id']) && $fieldMapping['id']) {
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Id';
- if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")';
- }
- if ($metadata->sequenceGeneratorDefinition) {
- $sequenceGenerator = array();
- if (isset($metadata->sequenceGeneratorDefinition['sequenceName'])) {
- $sequenceGenerator[] = 'sequenceName="' . $metadata->sequenceGeneratorDefinition['sequenceName'] . '"';
- }
- if (isset($metadata->sequenceGeneratorDefinition['allocationSize'])) {
- $sequenceGenerator[] = 'allocationSize="' . $metadata->sequenceGeneratorDefinition['allocationSize'] . '"';
- }
- if (isset($metadata->sequenceGeneratorDefinition['initialValue'])) {
- $sequenceGenerator[] = 'initialValue="' . $metadata->sequenceGeneratorDefinition['initialValue'] . '"';
- }
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'SequenceGenerator(' . implode(', ', $sequenceGenerator) . ')';
- }
- }
- if (isset($fieldMapping['version']) && $fieldMapping['version']) {
- $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Version';
- }
- }
- $lines[] = $this->_spaces . ' */';
- return implode("\n", $lines);
- }
- private function _prefixCodeWithSpaces($code, $num = 1)
- {
- $lines = explode("\n", $code);
- foreach ($lines as $key => $value) {
- $lines[$key] = str_repeat($this->_spaces, $num) . $lines[$key];
- }
- return implode("\n", $lines);
- }
- private function _getInheritanceTypeString($type)
- {
- switch ($type) {
- case ClassMetadataInfo::INHERITANCE_TYPE_NONE:
- return 'NONE';
- case ClassMetadataInfo::INHERITANCE_TYPE_JOINED:
- return 'JOINED';
- case ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE:
- return 'SINGLE_TABLE';
- case ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS:
- return 'PER_CLASS';
- default:
- throw new \InvalidArgumentException('Invalid provided InheritanceType: ' . $type);
- }
- }
- private function _getChangeTrackingPolicyString($policy)
- {
- switch ($policy) {
- case ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT:
- return 'DEFERRED_IMPLICIT';
- case ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT:
- return 'DEFERRED_EXPLICIT';
- case ClassMetadataInfo::CHANGETRACKING_NOTIFY:
- return 'NOTIFY';
- default:
- throw new \InvalidArgumentException('Invalid provided ChangeTrackingPolicy: ' . $policy);
- }
- }
- private function _getIdGeneratorTypeString($type)
- {
- switch ($type) {
- case ClassMetadataInfo::GENERATOR_TYPE_AUTO:
- return 'AUTO';
- case ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE:
- return 'SEQUENCE';
- case ClassMetadataInfo::GENERATOR_TYPE_TABLE:
- return 'TABLE';
- case ClassMetadataInfo::GENERATOR_TYPE_IDENTITY:
- return 'IDENTITY';
- case ClassMetadataInfo::GENERATOR_TYPE_NONE:
- return 'NONE';
- default:
- throw new \InvalidArgumentException('Invalid provided IdGeneratorType: ' . $type);
- }
- }
- }
|