EntityGenerator.php 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Tools;
  20. use Doctrine\ORM\Mapping\ClassMetadataInfo,
  21. Doctrine\ORM\Mapping\AssociationMapping,
  22. Doctrine\Common\Util\Inflector;
  23. /**
  24. * Generic class used to generate PHP5 entity classes from ClassMetadataInfo instances
  25. *
  26. * [php]
  27. * $classes = $em->getClassMetadataFactory()->getAllMetadata();
  28. *
  29. * $generator = new \Doctrine\ORM\Tools\EntityGenerator();
  30. * $generator->setGenerateAnnotations(true);
  31. * $generator->setGenerateStubMethods(true);
  32. * $generator->setRegenerateEntityIfExists(false);
  33. * $generator->setUpdateEntityIfExists(true);
  34. * $generator->generate($classes, '/path/to/generate/entities');
  35. *
  36. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  37. * @link www.doctrine-project.org
  38. * @since 2.0
  39. * @version $Revision$
  40. * @author Benjamin Eberlei <kontakt@beberlei.de>
  41. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  42. * @author Jonathan Wage <jonwage@gmail.com>
  43. * @author Roman Borschel <roman@code-factory.org>
  44. */
  45. class EntityGenerator
  46. {
  47. /**
  48. * @var bool
  49. */
  50. private $_backupExisting = true;
  51. /** The extension to use for written php files */
  52. private $_extension = '.php';
  53. /** Whether or not the current ClassMetadataInfo instance is new or old */
  54. private $_isNew = true;
  55. private $_staticReflection = array();
  56. /** Number of spaces to use for indention in generated code */
  57. private $_numSpaces = 4;
  58. /** The actual spaces to use for indention */
  59. private $_spaces = ' ';
  60. /** The class all generated entities should extend */
  61. private $_classToExtend;
  62. /** Whether or not to generation annotations */
  63. private $_generateAnnotations = false;
  64. /**
  65. * @var string
  66. */
  67. private $_annotationsPrefix = '';
  68. /** Whether or not to generated sub methods */
  69. private $_generateEntityStubMethods = false;
  70. /** Whether or not to update the entity class if it exists already */
  71. private $_updateEntityIfExists = false;
  72. /** Whether or not to re-generate entity class if it exists already */
  73. private $_regenerateEntityIfExists = false;
  74. private static $_classTemplate =
  75. '<?php
  76. <namespace>
  77. use Doctrine\ORM\Mapping as ORM;
  78. <entityAnnotation>
  79. <entityClassName>
  80. {
  81. <entityBody>
  82. }';
  83. private static $_getMethodTemplate =
  84. '/**
  85. * <description>
  86. *
  87. * @return <variableType>
  88. */
  89. public function <methodName>()
  90. {
  91. <spaces>return $this-><fieldName>;
  92. }';
  93. private static $_setMethodTemplate =
  94. '/**
  95. * <description>
  96. *
  97. * @param <variableType>$<variableName>
  98. */
  99. public function <methodName>(<methodTypeHint>$<variableName>)
  100. {
  101. <spaces>$this-><fieldName> = $<variableName>;
  102. }';
  103. private static $_addMethodTemplate =
  104. '/**
  105. * <description>
  106. *
  107. * @param <variableType>$<variableName>
  108. */
  109. public function <methodName>(<methodTypeHint>$<variableName>)
  110. {
  111. <spaces>$this-><fieldName>[] = $<variableName>;
  112. }';
  113. private static $_lifecycleCallbackMethodTemplate =
  114. '/**
  115. * @<name>
  116. */
  117. public function <methodName>()
  118. {
  119. <spaces>// Add your code here
  120. }';
  121. private static $_constructorMethodTemplate =
  122. 'public function __construct()
  123. {
  124. <spaces><collections>
  125. }
  126. ';
  127. public function __construct()
  128. {
  129. if (version_compare(\Doctrine\Common\Version::VERSION, '3.0.0-DEV', '>=')) {
  130. $this->_annotationsPrefix = 'ORM\\';
  131. }
  132. }
  133. /**
  134. * Generate and write entity classes for the given array of ClassMetadataInfo instances
  135. *
  136. * @param array $metadatas
  137. * @param string $outputDirectory
  138. * @return void
  139. */
  140. public function generate(array $metadatas, $outputDirectory)
  141. {
  142. foreach ($metadatas as $metadata) {
  143. $this->writeEntityClass($metadata, $outputDirectory);
  144. }
  145. }
  146. /**
  147. * Generated and write entity class to disk for the given ClassMetadataInfo instance
  148. *
  149. * @param ClassMetadataInfo $metadata
  150. * @param string $outputDirectory
  151. * @return void
  152. */
  153. public function writeEntityClass(ClassMetadataInfo $metadata, $outputDirectory)
  154. {
  155. $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension;
  156. $dir = dirname($path);
  157. if ( ! is_dir($dir)) {
  158. mkdir($dir, 0777, true);
  159. }
  160. $this->_isNew = !file_exists($path) || (file_exists($path) && $this->_regenerateEntityIfExists);
  161. if ( ! $this->_isNew) {
  162. $this->_parseTokensInEntityFile(file_get_contents($path));
  163. } else {
  164. $this->_staticReflection[$metadata->name] = array('properties' => array(), 'methods' => array());
  165. }
  166. if ($this->_backupExisting && file_exists($path)) {
  167. $backupPath = dirname($path) . DIRECTORY_SEPARATOR . basename($path) . "~";
  168. if (!copy($path, $backupPath)) {
  169. throw new \RuntimeException("Attempt to backup overwritten entitiy file but copy operation failed.");
  170. }
  171. }
  172. // If entity doesn't exist or we're re-generating the entities entirely
  173. if ($this->_isNew) {
  174. file_put_contents($path, $this->generateEntityClass($metadata));
  175. // If entity exists and we're allowed to update the entity class
  176. } else if ( ! $this->_isNew && $this->_updateEntityIfExists) {
  177. file_put_contents($path, $this->generateUpdatedEntityClass($metadata, $path));
  178. }
  179. }
  180. /**
  181. * Generate a PHP5 Doctrine 2 entity class from the given ClassMetadataInfo instance
  182. *
  183. * @param ClassMetadataInfo $metadata
  184. * @return string $code
  185. */
  186. public function generateEntityClass(ClassMetadataInfo $metadata)
  187. {
  188. $placeHolders = array(
  189. '<namespace>',
  190. '<entityAnnotation>',
  191. '<entityClassName>',
  192. '<entityBody>'
  193. );
  194. $replacements = array(
  195. $this->_generateEntityNamespace($metadata),
  196. $this->_generateEntityDocBlock($metadata),
  197. $this->_generateEntityClassName($metadata),
  198. $this->_generateEntityBody($metadata)
  199. );
  200. $code = str_replace($placeHolders, $replacements, self::$_classTemplate);
  201. return str_replace('<spaces>', $this->_spaces, $code);
  202. }
  203. /**
  204. * Generate the updated code for the given ClassMetadataInfo and entity at path
  205. *
  206. * @param ClassMetadataInfo $metadata
  207. * @param string $path
  208. * @return string $code;
  209. */
  210. public function generateUpdatedEntityClass(ClassMetadataInfo $metadata, $path)
  211. {
  212. $currentCode = file_get_contents($path);
  213. $body = $this->_generateEntityBody($metadata);
  214. $body = str_replace('<spaces>', $this->_spaces, $body);
  215. $last = strrpos($currentCode, '}');
  216. return substr($currentCode, 0, $last) . $body . (strlen($body) > 0 ? "\n" : ''). "}";
  217. }
  218. /**
  219. * Set the number of spaces the exported class should have
  220. *
  221. * @param integer $numSpaces
  222. * @return void
  223. */
  224. public function setNumSpaces($numSpaces)
  225. {
  226. $this->_spaces = str_repeat(' ', $numSpaces);
  227. $this->_numSpaces = $numSpaces;
  228. }
  229. /**
  230. * Set the extension to use when writing php files to disk
  231. *
  232. * @param string $extension
  233. * @return void
  234. */
  235. public function setExtension($extension)
  236. {
  237. $this->_extension = $extension;
  238. }
  239. /**
  240. * Set the name of the class the generated classes should extend from
  241. *
  242. * @return void
  243. */
  244. public function setClassToExtend($classToExtend)
  245. {
  246. $this->_classToExtend = $classToExtend;
  247. }
  248. /**
  249. * Set whether or not to generate annotations for the entity
  250. *
  251. * @param bool $bool
  252. * @return void
  253. */
  254. public function setGenerateAnnotations($bool)
  255. {
  256. $this->_generateAnnotations = $bool;
  257. }
  258. /**
  259. * Set an annotation prefix.
  260. *
  261. * @param string $prefix
  262. */
  263. public function setAnnotationPrefix($prefix)
  264. {
  265. if (version_compare(\Doctrine\Common\Version::VERSION, '3.0.0-DEV', '>=')) {
  266. return;
  267. }
  268. $this->_annotationsPrefix = $prefix;
  269. }
  270. /**
  271. * Set whether or not to try and update the entity if it already exists
  272. *
  273. * @param bool $bool
  274. * @return void
  275. */
  276. public function setUpdateEntityIfExists($bool)
  277. {
  278. $this->_updateEntityIfExists = $bool;
  279. }
  280. /**
  281. * Set whether or not to regenerate the entity if it exists
  282. *
  283. * @param bool $bool
  284. * @return void
  285. */
  286. public function setRegenerateEntityIfExists($bool)
  287. {
  288. $this->_regenerateEntityIfExists = $bool;
  289. }
  290. /**
  291. * Set whether or not to generate stub methods for the entity
  292. *
  293. * @param bool $bool
  294. * @return void
  295. */
  296. public function setGenerateStubMethods($bool)
  297. {
  298. $this->_generateEntityStubMethods = $bool;
  299. }
  300. /**
  301. * Should an existing entity be backed up if it already exists?
  302. */
  303. public function setBackupExisting($bool)
  304. {
  305. $this->_backupExisting = $bool;
  306. }
  307. private function _generateEntityNamespace(ClassMetadataInfo $metadata)
  308. {
  309. if ($this->_hasNamespace($metadata)) {
  310. return 'namespace ' . $this->_getNamespace($metadata) .';';
  311. }
  312. }
  313. private function _generateEntityClassName(ClassMetadataInfo $metadata)
  314. {
  315. return 'class ' . $this->_getClassName($metadata) .
  316. ($this->_extendsClass() ? ' extends ' . $this->_getClassToExtendName() : null);
  317. }
  318. private function _generateEntityBody(ClassMetadataInfo $metadata)
  319. {
  320. $fieldMappingProperties = $this->_generateEntityFieldMappingProperties($metadata);
  321. $associationMappingProperties = $this->_generateEntityAssociationMappingProperties($metadata);
  322. $stubMethods = $this->_generateEntityStubMethods ? $this->_generateEntityStubMethods($metadata) : null;
  323. $lifecycleCallbackMethods = $this->_generateEntityLifecycleCallbackMethods($metadata);
  324. $code = array();
  325. if ($fieldMappingProperties) {
  326. $code[] = $fieldMappingProperties;
  327. }
  328. if ($associationMappingProperties) {
  329. $code[] = $associationMappingProperties;
  330. }
  331. $code[] = $this->_generateEntityConstructor($metadata);
  332. if ($stubMethods) {
  333. $code[] = $stubMethods;
  334. }
  335. if ($lifecycleCallbackMethods) {
  336. $code[] = $lifecycleCallbackMethods;
  337. }
  338. return implode("\n", $code);
  339. }
  340. private function _generateEntityConstructor(ClassMetadataInfo $metadata)
  341. {
  342. if ($this->_hasMethod('__construct', $metadata)) {
  343. return '';
  344. }
  345. $collections = array();
  346. foreach ($metadata->associationMappings AS $mapping) {
  347. if ($mapping['type'] & ClassMetadataInfo::TO_MANY) {
  348. $collections[] = '$this->'.$mapping['fieldName'].' = new \Doctrine\Common\Collections\ArrayCollection();';
  349. }
  350. }
  351. if ($collections) {
  352. return $this->_prefixCodeWithSpaces(str_replace("<collections>", implode("\n", $collections), self::$_constructorMethodTemplate));
  353. }
  354. return '';
  355. }
  356. /**
  357. * @todo this won't work if there is a namespace in brackets and a class outside of it.
  358. * @param string $src
  359. */
  360. private function _parseTokensInEntityFile($src)
  361. {
  362. $tokens = token_get_all($src);
  363. $lastSeenNamespace = "";
  364. $lastSeenClass = false;
  365. $inNamespace = false;
  366. $inClass = false;
  367. for ($i = 0; $i < count($tokens); $i++) {
  368. $token = $tokens[$i];
  369. if (in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT))) {
  370. continue;
  371. }
  372. if ($inNamespace) {
  373. if ($token[0] == T_NS_SEPARATOR || $token[0] == T_STRING) {
  374. $lastSeenNamespace .= $token[1];
  375. } else if (is_string($token) && in_array($token, array(';', '{'))) {
  376. $inNamespace = false;
  377. }
  378. }
  379. if ($inClass) {
  380. $inClass = false;
  381. $lastSeenClass = $lastSeenNamespace . ($lastSeenNamespace ? '\\' : '') . $token[1];
  382. $this->_staticReflection[$lastSeenClass]['properties'] = array();
  383. $this->_staticReflection[$lastSeenClass]['methods'] = array();
  384. }
  385. if ($token[0] == T_NAMESPACE) {
  386. $lastSeenNamespace = "";
  387. $inNamespace = true;
  388. } else if ($token[0] == T_CLASS) {
  389. $inClass = true;
  390. } else if ($token[0] == T_FUNCTION) {
  391. if ($tokens[$i+2][0] == T_STRING) {
  392. $this->_staticReflection[$lastSeenClass]['methods'][] = $tokens[$i+2][1];
  393. } else if ($tokens[$i+2] == "&" && $tokens[$i+3][0] == T_STRING) {
  394. $this->_staticReflection[$lastSeenClass]['methods'][] = $tokens[$i+3][1];
  395. }
  396. } else if (in_array($token[0], array(T_VAR, T_PUBLIC, T_PRIVATE, T_PROTECTED)) && $tokens[$i+2][0] != T_FUNCTION) {
  397. $this->_staticReflection[$lastSeenClass]['properties'][] = substr($tokens[$i+2][1], 1);
  398. }
  399. }
  400. }
  401. private function _hasProperty($property, ClassMetadataInfo $metadata)
  402. {
  403. if ($this->_extendsClass()) {
  404. // don't generate property if its already on the base class.
  405. $reflClass = new \ReflectionClass($this->_getClassToExtend());
  406. if ($reflClass->hasProperty($property)) {
  407. return true;
  408. }
  409. }
  410. return (
  411. isset($this->_staticReflection[$metadata->name]) &&
  412. in_array($property, $this->_staticReflection[$metadata->name]['properties'])
  413. );
  414. }
  415. private function _hasMethod($method, ClassMetadataInfo $metadata)
  416. {
  417. if ($this->_extendsClass()) {
  418. // don't generate method if its already on the base class.
  419. $reflClass = new \ReflectionClass($this->_getClassToExtend());
  420. if ($reflClass->hasMethod($method)) {
  421. return true;
  422. }
  423. }
  424. return (
  425. isset($this->_staticReflection[$metadata->name]) &&
  426. in_array($method, $this->_staticReflection[$metadata->name]['methods'])
  427. );
  428. }
  429. private function _hasNamespace(ClassMetadataInfo $metadata)
  430. {
  431. return strpos($metadata->name, '\\') ? true : false;
  432. }
  433. private function _extendsClass()
  434. {
  435. return $this->_classToExtend ? true : false;
  436. }
  437. private function _getClassToExtend()
  438. {
  439. return $this->_classToExtend;
  440. }
  441. private function _getClassToExtendName()
  442. {
  443. $refl = new \ReflectionClass($this->_getClassToExtend());
  444. return '\\' . $refl->getName();
  445. }
  446. private function _getClassName(ClassMetadataInfo $metadata)
  447. {
  448. return ($pos = strrpos($metadata->name, '\\'))
  449. ? substr($metadata->name, $pos + 1, strlen($metadata->name)) : $metadata->name;
  450. }
  451. private function _getNamespace(ClassMetadataInfo $metadata)
  452. {
  453. return substr($metadata->name, 0, strrpos($metadata->name, '\\'));
  454. }
  455. private function _generateEntityDocBlock(ClassMetadataInfo $metadata)
  456. {
  457. $lines = array();
  458. $lines[] = '/**';
  459. $lines[] = ' * '.$metadata->name;
  460. if ($this->_generateAnnotations) {
  461. $lines[] = ' *';
  462. $methods = array(
  463. '_generateTableAnnotation',
  464. '_generateInheritanceAnnotation',
  465. '_generateDiscriminatorColumnAnnotation',
  466. '_generateDiscriminatorMapAnnotation'
  467. );
  468. foreach ($methods as $method) {
  469. if ($code = $this->$method($metadata)) {
  470. $lines[] = ' * ' . $code;
  471. }
  472. }
  473. if ($metadata->isMappedSuperclass) {
  474. $lines[] = ' * @' . $this->_annotationsPrefix . 'MappedSuperClass';
  475. } else {
  476. $lines[] = ' * @' . $this->_annotationsPrefix . 'Entity';
  477. }
  478. if ($metadata->customRepositoryClassName) {
  479. $lines[count($lines) - 1] .= '(repositoryClass="' . $metadata->customRepositoryClassName . '")';
  480. }
  481. if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
  482. $lines[] = ' * @' . $this->_annotationsPrefix . 'HasLifecycleCallbacks';
  483. }
  484. }
  485. $lines[] = ' */';
  486. return implode("\n", $lines);
  487. }
  488. private function _generateTableAnnotation($metadata)
  489. {
  490. $table = array();
  491. if ($metadata->table['name']) {
  492. $table[] = 'name="' . $metadata->table['name'] . '"';
  493. }
  494. return '@' . $this->_annotationsPrefix . 'Table(' . implode(', ', $table) . ')';
  495. }
  496. private function _generateInheritanceAnnotation($metadata)
  497. {
  498. if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
  499. return '@' . $this->_annotationsPrefix . 'InheritanceType("'.$this->_getInheritanceTypeString($metadata->inheritanceType).'")';
  500. }
  501. }
  502. private function _generateDiscriminatorColumnAnnotation($metadata)
  503. {
  504. if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
  505. $discrColumn = $metadata->discriminatorValue;
  506. $columnDefinition = 'name="' . $discrColumn['name']
  507. . '", type="' . $discrColumn['type']
  508. . '", length=' . $discrColumn['length'];
  509. return '@' . $this->_annotationsPrefix . 'DiscriminatorColumn(' . $columnDefinition . ')';
  510. }
  511. }
  512. private function _generateDiscriminatorMapAnnotation($metadata)
  513. {
  514. if ($metadata->inheritanceType != ClassMetadataInfo::INHERITANCE_TYPE_NONE) {
  515. $inheritanceClassMap = array();
  516. foreach ($metadata->discriminatorMap as $type => $class) {
  517. $inheritanceClassMap[] .= '"' . $type . '" = "' . $class . '"';
  518. }
  519. return '@' . $this->_annotationsPrefix . 'DiscriminatorMap({' . implode(', ', $inheritanceClassMap) . '})';
  520. }
  521. }
  522. private function _generateEntityStubMethods(ClassMetadataInfo $metadata)
  523. {
  524. $methods = array();
  525. foreach ($metadata->fieldMappings as $fieldMapping) {
  526. if ( ! isset($fieldMapping['id']) || ! $fieldMapping['id'] || $metadata->generatorType == ClassMetadataInfo::GENERATOR_TYPE_NONE) {
  527. if ($code = $this->_generateEntityStubMethod($metadata, 'set', $fieldMapping['fieldName'], $fieldMapping['type'])) {
  528. $methods[] = $code;
  529. }
  530. }
  531. if ($code = $this->_generateEntityStubMethod($metadata, 'get', $fieldMapping['fieldName'], $fieldMapping['type'])) {
  532. $methods[] = $code;
  533. }
  534. }
  535. foreach ($metadata->associationMappings as $associationMapping) {
  536. if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) {
  537. if ($code = $this->_generateEntityStubMethod($metadata, 'set', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
  538. $methods[] = $code;
  539. }
  540. if ($code = $this->_generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
  541. $methods[] = $code;
  542. }
  543. } else if ($associationMapping['type'] & ClassMetadataInfo::TO_MANY) {
  544. if ($code = $this->_generateEntityStubMethod($metadata, 'add', $associationMapping['fieldName'], $associationMapping['targetEntity'])) {
  545. $methods[] = $code;
  546. }
  547. if ($code = $this->_generateEntityStubMethod($metadata, 'get', $associationMapping['fieldName'], 'Doctrine\Common\Collections\Collection')) {
  548. $methods[] = $code;
  549. }
  550. }
  551. }
  552. return implode("\n\n", $methods);
  553. }
  554. private function _generateEntityLifecycleCallbackMethods(ClassMetadataInfo $metadata)
  555. {
  556. if (isset($metadata->lifecycleCallbacks) && $metadata->lifecycleCallbacks) {
  557. $methods = array();
  558. foreach ($metadata->lifecycleCallbacks as $name => $callbacks) {
  559. foreach ($callbacks as $callback) {
  560. if ($code = $this->_generateLifecycleCallbackMethod($name, $callback, $metadata)) {
  561. $methods[] = $code;
  562. }
  563. }
  564. }
  565. return implode("\n\n", $methods);
  566. }
  567. return "";
  568. }
  569. private function _generateEntityAssociationMappingProperties(ClassMetadataInfo $metadata)
  570. {
  571. $lines = array();
  572. foreach ($metadata->associationMappings as $associationMapping) {
  573. if ($this->_hasProperty($associationMapping['fieldName'], $metadata)) {
  574. continue;
  575. }
  576. $lines[] = $this->_generateAssociationMappingPropertyDocBlock($associationMapping, $metadata);
  577. $lines[] = $this->_spaces . 'private $' . $associationMapping['fieldName']
  578. . ($associationMapping['type'] == 'manyToMany' ? ' = array()' : null) . ";\n";
  579. }
  580. return implode("\n", $lines);
  581. }
  582. private function _generateEntityFieldMappingProperties(ClassMetadataInfo $metadata)
  583. {
  584. $lines = array();
  585. foreach ($metadata->fieldMappings as $fieldMapping) {
  586. if ($this->_hasProperty($fieldMapping['fieldName'], $metadata) ||
  587. $metadata->isInheritedField($fieldMapping['fieldName'])) {
  588. continue;
  589. }
  590. $lines[] = $this->_generateFieldMappingPropertyDocBlock($fieldMapping, $metadata);
  591. $lines[] = $this->_spaces . 'private $' . $fieldMapping['fieldName']
  592. . (isset($fieldMapping['default']) ? ' = ' . var_export($fieldMapping['default'], true) : null) . ";\n";
  593. }
  594. return implode("\n", $lines);
  595. }
  596. private function _generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null)
  597. {
  598. if ($type == "add") {
  599. $addMethod = explode("\\", $typeHint);
  600. $addMethod = end($addMethod);
  601. $methodName = $type . $addMethod;
  602. } else {
  603. $methodName = $type . Inflector::classify($fieldName);
  604. }
  605. if ($this->_hasMethod($methodName, $metadata)) {
  606. return;
  607. }
  608. $this->_staticReflection[$metadata->name]['methods'][] = $methodName;
  609. $var = sprintf('_%sMethodTemplate', $type);
  610. $template = self::$$var;
  611. $variableType = $typeHint ? $typeHint . ' ' : null;
  612. $types = \Doctrine\DBAL\Types\Type::getTypesMap();
  613. $methodTypeHint = $typeHint && ! isset($types[$typeHint]) ? '\\' . $typeHint . ' ' : null;
  614. $replacements = array(
  615. '<description>' => ucfirst($type) . ' ' . $fieldName,
  616. '<methodTypeHint>' => $methodTypeHint,
  617. '<variableType>' => $variableType,
  618. '<variableName>' => Inflector::camelize($fieldName),
  619. '<methodName>' => $methodName,
  620. '<fieldName>' => $fieldName
  621. );
  622. $method = str_replace(
  623. array_keys($replacements),
  624. array_values($replacements),
  625. $template
  626. );
  627. return $this->_prefixCodeWithSpaces($method);
  628. }
  629. private function _generateLifecycleCallbackMethod($name, $methodName, $metadata)
  630. {
  631. if ($this->_hasMethod($methodName, $metadata)) {
  632. return;
  633. }
  634. $this->_staticReflection[$metadata->name]['methods'][] = $methodName;
  635. $replacements = array(
  636. '<name>' => $this->_annotationsPrefix . ucfirst($name),
  637. '<methodName>' => $methodName,
  638. );
  639. $method = str_replace(
  640. array_keys($replacements),
  641. array_values($replacements),
  642. self::$_lifecycleCallbackMethodTemplate
  643. );
  644. return $this->_prefixCodeWithSpaces($method);
  645. }
  646. private function _generateJoinColumnAnnotation(array $joinColumn)
  647. {
  648. $joinColumnAnnot = array();
  649. if (isset($joinColumn['name'])) {
  650. $joinColumnAnnot[] = 'name="' . $joinColumn['name'] . '"';
  651. }
  652. if (isset($joinColumn['referencedColumnName'])) {
  653. $joinColumnAnnot[] = 'referencedColumnName="' . $joinColumn['referencedColumnName'] . '"';
  654. }
  655. if (isset($joinColumn['unique']) && $joinColumn['unique']) {
  656. $joinColumnAnnot[] = 'unique=' . ($joinColumn['unique'] ? 'true' : 'false');
  657. }
  658. if (isset($joinColumn['nullable'])) {
  659. $joinColumnAnnot[] = 'nullable=' . ($joinColumn['nullable'] ? 'true' : 'false');
  660. }
  661. if (isset($joinColumn['onDelete'])) {
  662. $joinColumnAnnot[] = 'onDelete="' . ($joinColumn['onDelete'] . '"');
  663. }
  664. if (isset($joinColumn['onUpdate'])) {
  665. $joinColumnAnnot[] = 'onUpdate=' . ($joinColumn['onUpdate'] ? 'true' : 'false');
  666. }
  667. if (isset($joinColumn['columnDefinition'])) {
  668. $joinColumnAnnot[] = 'columnDefinition="' . $joinColumn['columnDefinition'] . '"';
  669. }
  670. return '@' . $this->_annotationsPrefix . 'JoinColumn(' . implode(', ', $joinColumnAnnot) . ')';
  671. }
  672. private function _generateAssociationMappingPropertyDocBlock(array $associationMapping, ClassMetadataInfo $metadata)
  673. {
  674. $lines = array();
  675. $lines[] = $this->_spaces . '/**';
  676. $lines[] = $this->_spaces . ' * @var ' . $associationMapping['targetEntity'];
  677. if ($this->_generateAnnotations) {
  678. $lines[] = $this->_spaces . ' *';
  679. if (isset($associationMapping['id']) && $associationMapping['id']) {
  680. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Id';
  681. if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
  682. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")';
  683. }
  684. }
  685. $type = null;
  686. switch ($associationMapping['type']) {
  687. case ClassMetadataInfo::ONE_TO_ONE:
  688. $type = 'OneToOne';
  689. break;
  690. case ClassMetadataInfo::MANY_TO_ONE:
  691. $type = 'ManyToOne';
  692. break;
  693. case ClassMetadataInfo::ONE_TO_MANY:
  694. $type = 'OneToMany';
  695. break;
  696. case ClassMetadataInfo::MANY_TO_MANY:
  697. $type = 'ManyToMany';
  698. break;
  699. }
  700. $typeOptions = array();
  701. if (isset($associationMapping['targetEntity'])) {
  702. $typeOptions[] = 'targetEntity="' . $associationMapping['targetEntity'] . '"';
  703. }
  704. if (isset($associationMapping['inversedBy'])) {
  705. $typeOptions[] = 'inversedBy="' . $associationMapping['inversedBy'] . '"';
  706. }
  707. if (isset($associationMapping['mappedBy'])) {
  708. $typeOptions[] = 'mappedBy="' . $associationMapping['mappedBy'] . '"';
  709. }
  710. if ($associationMapping['cascade']) {
  711. $cascades = array();
  712. if ($associationMapping['isCascadePersist']) $cascades[] = '"persist"';
  713. if ($associationMapping['isCascadeRemove']) $cascades[] = '"remove"';
  714. if ($associationMapping['isCascadeDetach']) $cascades[] = '"detach"';
  715. if ($associationMapping['isCascadeMerge']) $cascades[] = '"merge"';
  716. if ($associationMapping['isCascadeRefresh']) $cascades[] = '"refresh"';
  717. $typeOptions[] = 'cascade={' . implode(',', $cascades) . '}';
  718. }
  719. if (isset($associationMapping['orphanRemoval']) && $associationMapping['orphanRemoval']) {
  720. $typeOptions[] = 'orphanRemoval=' . ($associationMapping['orphanRemoval'] ? 'true' : 'false');
  721. }
  722. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . '' . $type . '(' . implode(', ', $typeOptions) . ')';
  723. if (isset($associationMapping['joinColumns']) && $associationMapping['joinColumns']) {
  724. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'JoinColumns({';
  725. $joinColumnsLines = array();
  726. foreach ($associationMapping['joinColumns'] as $joinColumn) {
  727. if ($joinColumnAnnot = $this->_generateJoinColumnAnnotation($joinColumn)) {
  728. $joinColumnsLines[] = $this->_spaces . ' * ' . $joinColumnAnnot;
  729. }
  730. }
  731. $lines[] = implode(",\n", $joinColumnsLines);
  732. $lines[] = $this->_spaces . ' * })';
  733. }
  734. if (isset($associationMapping['joinTable']) && $associationMapping['joinTable']) {
  735. $joinTable = array();
  736. $joinTable[] = 'name="' . $associationMapping['joinTable']['name'] . '"';
  737. if (isset($associationMapping['joinTable']['schema'])) {
  738. $joinTable[] = 'schema="' . $associationMapping['joinTable']['schema'] . '"';
  739. }
  740. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'JoinTable(' . implode(', ', $joinTable) . ',';
  741. $lines[] = $this->_spaces . ' * joinColumns={';
  742. foreach ($associationMapping['joinTable']['joinColumns'] as $joinColumn) {
  743. $lines[] = $this->_spaces . ' * ' . $this->_generateJoinColumnAnnotation($joinColumn);
  744. }
  745. $lines[] = $this->_spaces . ' * },';
  746. $lines[] = $this->_spaces . ' * inverseJoinColumns={';
  747. foreach ($associationMapping['joinTable']['inverseJoinColumns'] as $joinColumn) {
  748. $lines[] = $this->_spaces . ' * ' . $this->_generateJoinColumnAnnotation($joinColumn);
  749. }
  750. $lines[] = $this->_spaces . ' * }';
  751. $lines[] = $this->_spaces . ' * )';
  752. }
  753. if (isset($associationMapping['orderBy'])) {
  754. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'OrderBy({';
  755. foreach ($associationMapping['orderBy'] as $name => $direction) {
  756. $lines[] = $this->_spaces . ' * "' . $name . '"="' . $direction . '",';
  757. }
  758. $lines[count($lines) - 1] = substr($lines[count($lines) - 1], 0, strlen($lines[count($lines) - 1]) - 1);
  759. $lines[] = $this->_spaces . ' * })';
  760. }
  761. }
  762. $lines[] = $this->_spaces . ' */';
  763. return implode("\n", $lines);
  764. }
  765. private function _generateFieldMappingPropertyDocBlock(array $fieldMapping, ClassMetadataInfo $metadata)
  766. {
  767. $lines = array();
  768. $lines[] = $this->_spaces . '/**';
  769. $lines[] = $this->_spaces . ' * @var ' . $fieldMapping['type'] . ' $' . $fieldMapping['fieldName'];
  770. if ($this->_generateAnnotations) {
  771. $lines[] = $this->_spaces . ' *';
  772. $column = array();
  773. if (isset($fieldMapping['columnName'])) {
  774. $column[] = 'name="' . $fieldMapping['columnName'] . '"';
  775. }
  776. if (isset($fieldMapping['type'])) {
  777. $column[] = 'type="' . $fieldMapping['type'] . '"';
  778. }
  779. if (isset($fieldMapping['length'])) {
  780. $column[] = 'length=' . $fieldMapping['length'];
  781. }
  782. if (isset($fieldMapping['precision'])) {
  783. $column[] = 'precision=' . $fieldMapping['precision'];
  784. }
  785. if (isset($fieldMapping['scale'])) {
  786. $column[] = 'scale=' . $fieldMapping['scale'];
  787. }
  788. if (isset($fieldMapping['nullable'])) {
  789. $column[] = 'nullable=' . var_export($fieldMapping['nullable'], true);
  790. }
  791. if (isset($fieldMapping['columnDefinition'])) {
  792. $column[] = 'columnDefinition="' . $fieldMapping['columnDefinition'] . '"';
  793. }
  794. if (isset($fieldMapping['unique'])) {
  795. $column[] = 'unique=' . var_export($fieldMapping['unique'], true);
  796. }
  797. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Column(' . implode(', ', $column) . ')';
  798. if (isset($fieldMapping['id']) && $fieldMapping['id']) {
  799. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Id';
  800. if ($generatorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) {
  801. $lines[] = $this->_spaces.' * @' . $this->_annotationsPrefix . 'GeneratedValue(strategy="' . $generatorType . '")';
  802. }
  803. if ($metadata->sequenceGeneratorDefinition) {
  804. $sequenceGenerator = array();
  805. if (isset($metadata->sequenceGeneratorDefinition['sequenceName'])) {
  806. $sequenceGenerator[] = 'sequenceName="' . $metadata->sequenceGeneratorDefinition['sequenceName'] . '"';
  807. }
  808. if (isset($metadata->sequenceGeneratorDefinition['allocationSize'])) {
  809. $sequenceGenerator[] = 'allocationSize="' . $metadata->sequenceGeneratorDefinition['allocationSize'] . '"';
  810. }
  811. if (isset($metadata->sequenceGeneratorDefinition['initialValue'])) {
  812. $sequenceGenerator[] = 'initialValue="' . $metadata->sequenceGeneratorDefinition['initialValue'] . '"';
  813. }
  814. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'SequenceGenerator(' . implode(', ', $sequenceGenerator) . ')';
  815. }
  816. }
  817. if (isset($fieldMapping['version']) && $fieldMapping['version']) {
  818. $lines[] = $this->_spaces . ' * @' . $this->_annotationsPrefix . 'Version';
  819. }
  820. }
  821. $lines[] = $this->_spaces . ' */';
  822. return implode("\n", $lines);
  823. }
  824. private function _prefixCodeWithSpaces($code, $num = 1)
  825. {
  826. $lines = explode("\n", $code);
  827. foreach ($lines as $key => $value) {
  828. $lines[$key] = str_repeat($this->_spaces, $num) . $lines[$key];
  829. }
  830. return implode("\n", $lines);
  831. }
  832. private function _getInheritanceTypeString($type)
  833. {
  834. switch ($type) {
  835. case ClassMetadataInfo::INHERITANCE_TYPE_NONE:
  836. return 'NONE';
  837. case ClassMetadataInfo::INHERITANCE_TYPE_JOINED:
  838. return 'JOINED';
  839. case ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_TABLE:
  840. return 'SINGLE_TABLE';
  841. case ClassMetadataInfo::INHERITANCE_TYPE_TABLE_PER_CLASS:
  842. return 'PER_CLASS';
  843. default:
  844. throw new \InvalidArgumentException('Invalid provided InheritanceType: ' . $type);
  845. }
  846. }
  847. private function _getChangeTrackingPolicyString($policy)
  848. {
  849. switch ($policy) {
  850. case ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT:
  851. return 'DEFERRED_IMPLICIT';
  852. case ClassMetadataInfo::CHANGETRACKING_DEFERRED_EXPLICIT:
  853. return 'DEFERRED_EXPLICIT';
  854. case ClassMetadataInfo::CHANGETRACKING_NOTIFY:
  855. return 'NOTIFY';
  856. default:
  857. throw new \InvalidArgumentException('Invalid provided ChangeTrackingPolicy: ' . $policy);
  858. }
  859. }
  860. private function _getIdGeneratorTypeString($type)
  861. {
  862. switch ($type) {
  863. case ClassMetadataInfo::GENERATOR_TYPE_AUTO:
  864. return 'AUTO';
  865. case ClassMetadataInfo::GENERATOR_TYPE_SEQUENCE:
  866. return 'SEQUENCE';
  867. case ClassMetadataInfo::GENERATOR_TYPE_TABLE:
  868. return 'TABLE';
  869. case ClassMetadataInfo::GENERATOR_TYPE_IDENTITY:
  870. return 'IDENTITY';
  871. case ClassMetadataInfo::GENERATOR_TYPE_NONE:
  872. return 'NONE';
  873. default:
  874. throw new \InvalidArgumentException('Invalid provided IdGeneratorType: ' . $type);
  875. }
  876. }
  877. }