YamlDriver.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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\Mapping\Driver;
  20. use Doctrine\ORM\Mapping\ClassMetadataInfo,
  21. Doctrine\ORM\Mapping\MappingException;
  22. /**
  23. * The YamlDriver reads the mapping metadata from yaml schema files.
  24. *
  25. * @since 2.0
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  28. * @author Jonathan H. Wage <jonwage@gmail.com>
  29. * @author Roman Borschel <roman@code-factory.org>
  30. */
  31. class YamlDriver extends AbstractFileDriver
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected $_fileExtension = '.dcm.yml';
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
  41. {
  42. $element = $this->getElement($className);
  43. if ($element['type'] == 'entity') {
  44. $metadata->setCustomRepositoryClass(
  45. isset($element['repositoryClass']) ? $element['repositoryClass'] : null
  46. );
  47. if (isset($element['readOnly']) && $element['readOnly'] == true) {
  48. $metadata->markReadOnly();
  49. }
  50. } else if ($element['type'] == 'mappedSuperclass') {
  51. $metadata->isMappedSuperclass = true;
  52. } else {
  53. throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  54. }
  55. // Evaluate root level properties
  56. $table = array();
  57. if (isset($element['table'])) {
  58. $table['name'] = $element['table'];
  59. }
  60. $metadata->setPrimaryTable($table);
  61. // Evaluate named queries
  62. if (isset($element['namedQueries'])) {
  63. foreach ($element['namedQueries'] as $name => $queryMapping) {
  64. if (is_string($queryMapping)) {
  65. $queryMapping = array('query' => $queryMapping);
  66. }
  67. if ( ! isset($queryMapping['name'])) {
  68. $queryMapping['name'] = $name;
  69. }
  70. $metadata->addNamedQuery($queryMapping);
  71. }
  72. }
  73. /* not implemented specially anyway. use table = schema.table
  74. if (isset($element['schema'])) {
  75. $metadata->table['schema'] = $element['schema'];
  76. }*/
  77. if (isset($element['inheritanceType'])) {
  78. $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType'])));
  79. if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
  80. // Evaluate discriminatorColumn
  81. if (isset($element['discriminatorColumn'])) {
  82. $discrColumn = $element['discriminatorColumn'];
  83. $metadata->setDiscriminatorColumn(array(
  84. 'name' => $discrColumn['name'],
  85. 'type' => $discrColumn['type'],
  86. 'length' => $discrColumn['length']
  87. ));
  88. } else {
  89. $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
  90. }
  91. // Evaluate discriminatorMap
  92. if (isset($element['discriminatorMap'])) {
  93. $metadata->setDiscriminatorMap($element['discriminatorMap']);
  94. }
  95. }
  96. }
  97. // Evaluate changeTrackingPolicy
  98. if (isset($element['changeTrackingPolicy'])) {
  99. $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
  100. . strtoupper($element['changeTrackingPolicy'])));
  101. }
  102. // Evaluate indexes
  103. if (isset($element['indexes'])) {
  104. foreach ($element['indexes'] as $name => $index) {
  105. if ( ! isset($index['name'])) {
  106. $index['name'] = $name;
  107. }
  108. if (is_string($index['columns'])) {
  109. $columns = explode(',', $index['columns']);
  110. } else {
  111. $columns = $index['columns'];
  112. }
  113. $metadata->table['indexes'][$index['name']] = array(
  114. 'columns' => $columns
  115. );
  116. }
  117. }
  118. // Evaluate uniqueConstraints
  119. if (isset($element['uniqueConstraints'])) {
  120. foreach ($element['uniqueConstraints'] as $name => $unique) {
  121. if ( ! isset($unique['name'])) {
  122. $unique['name'] = $name;
  123. }
  124. if (is_string($unique['columns'])) {
  125. $columns = explode(',', $unique['columns']);
  126. } else {
  127. $columns = $unique['columns'];
  128. }
  129. $metadata->table['uniqueConstraints'][$unique['name']] = array(
  130. 'columns' => $columns
  131. );
  132. }
  133. }
  134. $associationIds = array();
  135. if (isset($element['id'])) {
  136. // Evaluate identifier settings
  137. foreach ($element['id'] as $name => $idElement) {
  138. if (isset($idElement['associationKey']) && $idElement['associationKey'] == true) {
  139. $associationIds[$name] = true;
  140. continue;
  141. }
  142. if (!isset($idElement['type'])) {
  143. throw MappingException::propertyTypeIsRequired($className, $name);
  144. }
  145. $mapping = array(
  146. 'id' => true,
  147. 'fieldName' => $name,
  148. 'type' => $idElement['type']
  149. );
  150. if (isset($idElement['column'])) {
  151. $mapping['columnName'] = $idElement['column'];
  152. }
  153. if (isset($idElement['length'])) {
  154. $mapping['length'] = $idElement['length'];
  155. }
  156. $metadata->mapField($mapping);
  157. if (isset($idElement['generator'])) {
  158. $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
  159. . strtoupper($idElement['generator']['strategy'])));
  160. }
  161. // Check for SequenceGenerator/TableGenerator definition
  162. if (isset($idElement['sequenceGenerator'])) {
  163. $metadata->setSequenceGeneratorDefinition($idElement['sequenceGenerator']);
  164. } else if (isset($idElement['tableGenerator'])) {
  165. throw MappingException::tableIdGeneratorNotImplemented($className);
  166. }
  167. }
  168. }
  169. // Evaluate fields
  170. if (isset($element['fields'])) {
  171. foreach ($element['fields'] as $name => $fieldMapping) {
  172. if (!isset($fieldMapping['type'])) {
  173. throw MappingException::propertyTypeIsRequired($className, $name);
  174. }
  175. $e = explode('(', $fieldMapping['type']);
  176. $fieldMapping['type'] = $e[0];
  177. if (isset($e[1])) {
  178. $fieldMapping['length'] = substr($e[1], 0, strlen($e[1]) - 1);
  179. }
  180. $mapping = array(
  181. 'fieldName' => $name,
  182. 'type' => $fieldMapping['type']
  183. );
  184. if (isset($fieldMapping['id'])) {
  185. $mapping['id'] = true;
  186. if (isset($fieldMapping['generator']['strategy'])) {
  187. $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
  188. . strtoupper($fieldMapping['generator']['strategy'])));
  189. }
  190. }
  191. if (isset($fieldMapping['column'])) {
  192. $mapping['columnName'] = $fieldMapping['column'];
  193. }
  194. if (isset($fieldMapping['length'])) {
  195. $mapping['length'] = $fieldMapping['length'];
  196. }
  197. if (isset($fieldMapping['precision'])) {
  198. $mapping['precision'] = $fieldMapping['precision'];
  199. }
  200. if (isset($fieldMapping['scale'])) {
  201. $mapping['scale'] = $fieldMapping['scale'];
  202. }
  203. if (isset($fieldMapping['unique'])) {
  204. $mapping['unique'] = (bool)$fieldMapping['unique'];
  205. }
  206. if (isset($fieldMapping['options'])) {
  207. $mapping['options'] = $fieldMapping['options'];
  208. }
  209. if (isset($fieldMapping['nullable'])) {
  210. $mapping['nullable'] = $fieldMapping['nullable'];
  211. }
  212. if (isset($fieldMapping['version']) && $fieldMapping['version']) {
  213. $metadata->setVersionMapping($mapping);
  214. }
  215. if (isset($fieldMapping['columnDefinition'])) {
  216. $mapping['columnDefinition'] = $fieldMapping['columnDefinition'];
  217. }
  218. $metadata->mapField($mapping);
  219. }
  220. }
  221. // Evaluate oneToOne relationships
  222. if (isset($element['oneToOne'])) {
  223. foreach ($element['oneToOne'] as $name => $oneToOneElement) {
  224. $mapping = array(
  225. 'fieldName' => $name,
  226. 'targetEntity' => $oneToOneElement['targetEntity']
  227. );
  228. if (isset($associationIds[$mapping['fieldName']])) {
  229. $mapping['id'] = true;
  230. }
  231. if (isset($oneToOneElement['fetch'])) {
  232. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToOneElement['fetch']);
  233. }
  234. if (isset($oneToOneElement['mappedBy'])) {
  235. $mapping['mappedBy'] = $oneToOneElement['mappedBy'];
  236. } else {
  237. if (isset($oneToOneElement['inversedBy'])) {
  238. $mapping['inversedBy'] = $oneToOneElement['inversedBy'];
  239. }
  240. $joinColumns = array();
  241. if (isset($oneToOneElement['joinColumn'])) {
  242. $joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement['joinColumn']);
  243. } else if (isset($oneToOneElement['joinColumns'])) {
  244. foreach ($oneToOneElement['joinColumns'] as $name => $joinColumnElement) {
  245. if (!isset($joinColumnElement['name'])) {
  246. $joinColumnElement['name'] = $name;
  247. }
  248. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  249. }
  250. }
  251. $mapping['joinColumns'] = $joinColumns;
  252. }
  253. if (isset($oneToOneElement['cascade'])) {
  254. $mapping['cascade'] = $oneToOneElement['cascade'];
  255. }
  256. if (isset($oneToOneElement['orphanRemoval'])) {
  257. $mapping['orphanRemoval'] = (bool)$oneToOneElement['orphanRemoval'];
  258. }
  259. $metadata->mapOneToOne($mapping);
  260. }
  261. }
  262. // Evaluate oneToMany relationships
  263. if (isset($element['oneToMany'])) {
  264. foreach ($element['oneToMany'] as $name => $oneToManyElement) {
  265. $mapping = array(
  266. 'fieldName' => $name,
  267. 'targetEntity' => $oneToManyElement['targetEntity'],
  268. 'mappedBy' => $oneToManyElement['mappedBy']
  269. );
  270. if (isset($oneToManyElement['fetch'])) {
  271. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $oneToManyElement['fetch']);
  272. }
  273. if (isset($oneToManyElement['cascade'])) {
  274. $mapping['cascade'] = $oneToManyElement['cascade'];
  275. }
  276. if (isset($oneToManyElement['orphanRemoval'])) {
  277. $mapping['orphanRemoval'] = (bool)$oneToManyElement['orphanRemoval'];
  278. }
  279. if (isset($oneToManyElement['orderBy'])) {
  280. $mapping['orderBy'] = $oneToManyElement['orderBy'];
  281. }
  282. if (isset($oneToManyElement['indexBy'])) {
  283. $mapping['indexBy'] = $oneToManyElement['indexBy'];
  284. }
  285. $metadata->mapOneToMany($mapping);
  286. }
  287. }
  288. // Evaluate manyToOne relationships
  289. if (isset($element['manyToOne'])) {
  290. foreach ($element['manyToOne'] as $name => $manyToOneElement) {
  291. $mapping = array(
  292. 'fieldName' => $name,
  293. 'targetEntity' => $manyToOneElement['targetEntity']
  294. );
  295. if (isset($associationIds[$mapping['fieldName']])) {
  296. $mapping['id'] = true;
  297. }
  298. if (isset($manyToOneElement['fetch'])) {
  299. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToOneElement['fetch']);
  300. }
  301. if (isset($manyToOneElement['inversedBy'])) {
  302. $mapping['inversedBy'] = $manyToOneElement['inversedBy'];
  303. }
  304. $joinColumns = array();
  305. if (isset($manyToOneElement['joinColumn'])) {
  306. $joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement['joinColumn']);
  307. } else if (isset($manyToOneElement['joinColumns'])) {
  308. foreach ($manyToOneElement['joinColumns'] as $name => $joinColumnElement) {
  309. if (!isset($joinColumnElement['name'])) {
  310. $joinColumnElement['name'] = $name;
  311. }
  312. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  313. }
  314. }
  315. $mapping['joinColumns'] = $joinColumns;
  316. if (isset($manyToOneElement['cascade'])) {
  317. $mapping['cascade'] = $manyToOneElement['cascade'];
  318. }
  319. if (isset($manyToOneElement['orphanRemoval'])) {
  320. $mapping['orphanRemoval'] = (bool)$manyToOneElement['orphanRemoval'];
  321. }
  322. $metadata->mapManyToOne($mapping);
  323. }
  324. }
  325. // Evaluate manyToMany relationships
  326. if (isset($element['manyToMany'])) {
  327. foreach ($element['manyToMany'] as $name => $manyToManyElement) {
  328. $mapping = array(
  329. 'fieldName' => $name,
  330. 'targetEntity' => $manyToManyElement['targetEntity']
  331. );
  332. if (isset($manyToManyElement['fetch'])) {
  333. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . $manyToManyElement['fetch']);
  334. }
  335. if (isset($manyToManyElement['mappedBy'])) {
  336. $mapping['mappedBy'] = $manyToManyElement['mappedBy'];
  337. } else if (isset($manyToManyElement['joinTable'])) {
  338. if (isset($manyToManyElement['inversedBy'])) {
  339. $mapping['inversedBy'] = $manyToManyElement['inversedBy'];
  340. }
  341. $joinTableElement = $manyToManyElement['joinTable'];
  342. $joinTable = array(
  343. 'name' => $joinTableElement['name']
  344. );
  345. if (isset($joinTableElement['schema'])) {
  346. $joinTable['schema'] = $joinTableElement['schema'];
  347. }
  348. foreach ($joinTableElement['joinColumns'] as $name => $joinColumnElement) {
  349. if (!isset($joinColumnElement['name'])) {
  350. $joinColumnElement['name'] = $name;
  351. }
  352. $joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  353. }
  354. foreach ($joinTableElement['inverseJoinColumns'] as $name => $joinColumnElement) {
  355. if (!isset($joinColumnElement['name'])) {
  356. $joinColumnElement['name'] = $name;
  357. }
  358. $joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  359. }
  360. $mapping['joinTable'] = $joinTable;
  361. }
  362. if (isset($manyToManyElement['cascade'])) {
  363. $mapping['cascade'] = $manyToManyElement['cascade'];
  364. }
  365. if (isset($manyToManyElement['orphanRemoval'])) {
  366. $mapping['orphanRemoval'] = (bool)$manyToManyElement['orphanRemoval'];
  367. }
  368. if (isset($manyToManyElement['orderBy'])) {
  369. $mapping['orderBy'] = $manyToManyElement['orderBy'];
  370. }
  371. if (isset($manyToManyElement['indexBy'])) {
  372. $mapping['indexBy'] = $manyToManyElement['indexBy'];
  373. }
  374. $metadata->mapManyToMany($mapping);
  375. }
  376. }
  377. // Evaluate lifeCycleCallbacks
  378. if (isset($element['lifecycleCallbacks'])) {
  379. foreach ($element['lifecycleCallbacks'] as $type => $methods) {
  380. foreach ($methods as $method) {
  381. $metadata->addLifecycleCallback($method, constant('Doctrine\ORM\Events::' . $type));
  382. }
  383. }
  384. }
  385. }
  386. /**
  387. * Constructs a joinColumn mapping array based on the information
  388. * found in the given join column element.
  389. *
  390. * @param $joinColumnElement The array join column element
  391. * @return array The mapping array.
  392. */
  393. private function _getJoinColumnMapping($joinColumnElement)
  394. {
  395. $joinColumn = array(
  396. 'name' => $joinColumnElement['name'],
  397. 'referencedColumnName' => $joinColumnElement['referencedColumnName']
  398. );
  399. if (isset($joinColumnElement['fieldName'])) {
  400. $joinColumn['fieldName'] = (string) $joinColumnElement['fieldName'];
  401. }
  402. if (isset($joinColumnElement['unique'])) {
  403. $joinColumn['unique'] = (bool) $joinColumnElement['unique'];
  404. }
  405. if (isset($joinColumnElement['nullable'])) {
  406. $joinColumn['nullable'] = (bool) $joinColumnElement['nullable'];
  407. }
  408. if (isset($joinColumnElement['onDelete'])) {
  409. $joinColumn['onDelete'] = $joinColumnElement['onDelete'];
  410. }
  411. if (isset($joinColumnElement['onUpdate'])) {
  412. $joinColumn['onUpdate'] = $joinColumnElement['onUpdate'];
  413. }
  414. if (isset($joinColumnElement['columnDefinition'])) {
  415. $joinColumn['columnDefinition'] = $joinColumnElement['columnDefinition'];
  416. }
  417. return $joinColumn;
  418. }
  419. /**
  420. * {@inheritdoc}
  421. */
  422. protected function _loadMappingFile($file)
  423. {
  424. return \Symfony\Component\Yaml\Yaml::parse($file);
  425. }
  426. }