YamlDriver.php 20 KB

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