XmlDriver.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.doctrine-project.org>.
  20. */
  21. namespace Doctrine\ORM\Mapping\Driver;
  22. use SimpleXMLElement,
  23. Doctrine\ORM\Mapping\ClassMetadataInfo,
  24. Doctrine\ORM\Mapping\MappingException;
  25. /**
  26. * XmlDriver is a metadata driver that enables mapping through XML files.
  27. *
  28. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  29. * @link www.doctrine-project.org
  30. * @since 2.0
  31. * @version $Revision$
  32. * @author Benjamin Eberlei <kontakt@beberlei.de>
  33. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  34. * @author Jonathan H. Wage <jonwage@gmail.com>
  35. * @author Roman Borschel <roman@code-factory.org>
  36. */
  37. class XmlDriver extends AbstractFileDriver
  38. {
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected $_fileExtension = '.dcm.xml';
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function loadMetadataForClass($className, ClassMetadataInfo $metadata)
  47. {
  48. $xmlRoot = $this->getElement($className);
  49. if ($xmlRoot->getName() == 'entity') {
  50. $metadata->setCustomRepositoryClass(
  51. isset($xmlRoot['repository-class']) ? (string)$xmlRoot['repository-class'] : null
  52. );
  53. if (isset($xmlRoot['read-only']) && $xmlRoot['read-only'] == "true") {
  54. $metadata->markReadOnly();
  55. }
  56. } else if ($xmlRoot->getName() == 'mapped-superclass') {
  57. $metadata->isMappedSuperclass = true;
  58. } else {
  59. throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  60. }
  61. // Evaluate <entity...> attributes
  62. $table = array();
  63. if (isset($xmlRoot['table'])) {
  64. $table['name'] = (string)$xmlRoot['table'];
  65. }
  66. $metadata->setPrimaryTable($table);
  67. // Evaluate named queries
  68. if (isset($xmlRoot['named-queries'])) {
  69. foreach ($xmlRoot->{'named-queries'}->{'named-query'} as $namedQueryElement) {
  70. $metadata->addNamedQuery(array(
  71. 'name' => (string)$namedQueryElement['name'],
  72. 'query' => (string)$namedQueryElement['query']
  73. ));
  74. }
  75. }
  76. /* not implemented specially anyway. use table = schema.table
  77. if (isset($xmlRoot['schema'])) {
  78. $metadata->table['schema'] = (string)$xmlRoot['schema'];
  79. }*/
  80. if (isset($xmlRoot['inheritance-type'])) {
  81. $inheritanceType = (string)$xmlRoot['inheritance-type'];
  82. $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType));
  83. if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
  84. // Evaluate <discriminator-column...>
  85. if (isset($xmlRoot->{'discriminator-column'})) {
  86. $discrColumn = $xmlRoot->{'discriminator-column'};
  87. $metadata->setDiscriminatorColumn(array(
  88. 'name' => (string)$discrColumn['name'],
  89. 'type' => (string)$discrColumn['type'],
  90. 'length' => (string)$discrColumn['length']
  91. ));
  92. } else {
  93. $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
  94. }
  95. // Evaluate <discriminator-map...>
  96. if (isset($xmlRoot->{'discriminator-map'})) {
  97. $map = array();
  98. foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) {
  99. $map[(string)$discrMapElement['value']] = (string)$discrMapElement['class'];
  100. }
  101. $metadata->setDiscriminatorMap($map);
  102. }
  103. }
  104. }
  105. // Evaluate <change-tracking-policy...>
  106. if (isset($xmlRoot['change-tracking-policy'])) {
  107. $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
  108. . strtoupper((string)$xmlRoot['change-tracking-policy'])));
  109. }
  110. // Evaluate <indexes...>
  111. if (isset($xmlRoot->indexes)) {
  112. $metadata->table['indexes'] = array();
  113. foreach ($xmlRoot->indexes->index as $index) {
  114. $columns = explode(',', (string)$index['columns']);
  115. if (isset($index['name'])) {
  116. $metadata->table['indexes'][(string)$index['name']] = array(
  117. 'columns' => $columns
  118. );
  119. } else {
  120. $metadata->table['indexes'][] = array(
  121. 'columns' => $columns
  122. );
  123. }
  124. }
  125. }
  126. // Evaluate <unique-constraints..>
  127. if (isset($xmlRoot->{'unique-constraints'})) {
  128. $metadata->table['uniqueConstraints'] = array();
  129. foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) {
  130. $columns = explode(',', (string)$unique['columns']);
  131. if (isset($unique['name'])) {
  132. $metadata->table['uniqueConstraints'][(string)$unique['name']] = array(
  133. 'columns' => $columns
  134. );
  135. } else {
  136. $metadata->table['uniqueConstraints'][] = array(
  137. 'columns' => $columns
  138. );
  139. }
  140. }
  141. }
  142. // Evaluate <field ...> mappings
  143. if (isset($xmlRoot->field)) {
  144. foreach ($xmlRoot->field as $fieldMapping) {
  145. $mapping = array(
  146. 'fieldName' => (string)$fieldMapping['name'],
  147. 'type' => (string)$fieldMapping['type']
  148. );
  149. if (isset($fieldMapping['column'])) {
  150. $mapping['columnName'] = (string)$fieldMapping['column'];
  151. }
  152. if (isset($fieldMapping['length'])) {
  153. $mapping['length'] = (int)$fieldMapping['length'];
  154. }
  155. if (isset($fieldMapping['precision'])) {
  156. $mapping['precision'] = (int)$fieldMapping['precision'];
  157. }
  158. if (isset($fieldMapping['scale'])) {
  159. $mapping['scale'] = (int)$fieldMapping['scale'];
  160. }
  161. if (isset($fieldMapping['unique'])) {
  162. $mapping['unique'] = ((string)$fieldMapping['unique'] == "false") ? false : true;
  163. }
  164. if (isset($fieldMapping['options'])) {
  165. $mapping['options'] = (array)$fieldMapping['options'];
  166. }
  167. if (isset($fieldMapping['nullable'])) {
  168. $mapping['nullable'] = ((string)$fieldMapping['nullable'] == "false") ? false : true;
  169. }
  170. if (isset($fieldMapping['version']) && $fieldMapping['version']) {
  171. $metadata->setVersionMapping($mapping);
  172. }
  173. if (isset($fieldMapping['column-definition'])) {
  174. $mapping['columnDefinition'] = (string)$fieldMapping['column-definition'];
  175. }
  176. $metadata->mapField($mapping);
  177. }
  178. }
  179. // Evaluate <id ...> mappings
  180. $associationIds = array();
  181. foreach ($xmlRoot->id as $idElement) {
  182. if ((bool)$idElement['association-key'] == true) {
  183. $associationIds[(string)$idElement['name']] = true;
  184. continue;
  185. }
  186. $mapping = array(
  187. 'id' => true,
  188. 'fieldName' => (string)$idElement['name'],
  189. 'type' => (string)$idElement['type']
  190. );
  191. if (isset($idElement['column'])) {
  192. $mapping['columnName'] = (string)$idElement['column'];
  193. }
  194. $metadata->mapField($mapping);
  195. if (isset($idElement->generator)) {
  196. $strategy = isset($idElement->generator['strategy']) ?
  197. (string)$idElement->generator['strategy'] : 'AUTO';
  198. $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
  199. . $strategy));
  200. }
  201. // Check for SequenceGenerator/TableGenerator definition
  202. if (isset($idElement->{'sequence-generator'})) {
  203. $seqGenerator = $idElement->{'sequence-generator'};
  204. $metadata->setSequenceGeneratorDefinition(array(
  205. 'sequenceName' => (string)$seqGenerator['sequence-name'],
  206. 'allocationSize' => (string)$seqGenerator['allocation-size'],
  207. 'initialValue' => (string)$seqGenerator['initial-value']
  208. ));
  209. } else if (isset($idElement->{'table-generator'})) {
  210. throw MappingException::tableIdGeneratorNotImplemented($className);
  211. }
  212. }
  213. // Evaluate <one-to-one ...> mappings
  214. if (isset($xmlRoot->{'one-to-one'})) {
  215. foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) {
  216. $mapping = array(
  217. 'fieldName' => (string)$oneToOneElement['field'],
  218. 'targetEntity' => (string)$oneToOneElement['target-entity']
  219. );
  220. if (isset($associationIds[$mapping['fieldName']])) {
  221. $mapping['id'] = true;
  222. }
  223. if (isset($oneToOneElement['fetch'])) {
  224. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$oneToOneElement['fetch']);
  225. }
  226. if (isset($oneToOneElement['mapped-by'])) {
  227. $mapping['mappedBy'] = (string)$oneToOneElement['mapped-by'];
  228. } else {
  229. if (isset($oneToOneElement['inversed-by'])) {
  230. $mapping['inversedBy'] = (string)$oneToOneElement['inversed-by'];
  231. }
  232. $joinColumns = array();
  233. if (isset($oneToOneElement->{'join-column'})) {
  234. $joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement->{'join-column'});
  235. } else if (isset($oneToOneElement->{'join-columns'})) {
  236. foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  237. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  238. }
  239. }
  240. $mapping['joinColumns'] = $joinColumns;
  241. }
  242. if (isset($oneToOneElement->cascade)) {
  243. $mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
  244. }
  245. if (isset($oneToOneElement['orphan-removal'])) {
  246. $mapping['orphanRemoval'] = (bool)$oneToOneElement['orphan-removal'];
  247. }
  248. $metadata->mapOneToOne($mapping);
  249. }
  250. }
  251. // Evaluate <one-to-many ...> mappings
  252. if (isset($xmlRoot->{'one-to-many'})) {
  253. foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) {
  254. $mapping = array(
  255. 'fieldName' => (string)$oneToManyElement['field'],
  256. 'targetEntity' => (string)$oneToManyElement['target-entity'],
  257. 'mappedBy' => (string)$oneToManyElement['mapped-by']
  258. );
  259. if (isset($oneToManyElement['fetch'])) {
  260. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$oneToManyElement['fetch']);
  261. }
  262. if (isset($oneToManyElement->cascade)) {
  263. $mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade);
  264. }
  265. if (isset($oneToManyElement['orphan-removal'])) {
  266. $mapping['orphanRemoval'] = (bool)$oneToManyElement['orphan-removal'];
  267. }
  268. if (isset($oneToManyElement->{'order-by'})) {
  269. $orderBy = array();
  270. foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) {
  271. $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction'];
  272. }
  273. $mapping['orderBy'] = $orderBy;
  274. }
  275. if (isset($oneToManyElement['index-by'])) {
  276. $mapping['indexBy'] = (string)$oneToManyElement['index-by'];
  277. } else if (isset($oneToManyElement->{'index-by'})) {
  278. throw new \InvalidArgumentException("<index-by /> is not a valid tag");
  279. }
  280. $metadata->mapOneToMany($mapping);
  281. }
  282. }
  283. // Evaluate <many-to-one ...> mappings
  284. if (isset($xmlRoot->{'many-to-one'})) {
  285. foreach ($xmlRoot->{'many-to-one'} as $manyToOneElement) {
  286. $mapping = array(
  287. 'fieldName' => (string)$manyToOneElement['field'],
  288. 'targetEntity' => (string)$manyToOneElement['target-entity']
  289. );
  290. if (isset($associationIds[$mapping['fieldName']])) {
  291. $mapping['id'] = true;
  292. }
  293. if (isset($manyToOneElement['fetch'])) {
  294. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$manyToOneElement['fetch']);
  295. }
  296. if (isset($manyToOneElement['inversed-by'])) {
  297. $mapping['inversedBy'] = (string)$manyToOneElement['inversed-by'];
  298. }
  299. $joinColumns = array();
  300. if (isset($manyToOneElement->{'join-column'})) {
  301. $joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement->{'join-column'});
  302. } else if (isset($manyToOneElement->{'join-columns'})) {
  303. foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  304. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  305. }
  306. }
  307. $mapping['joinColumns'] = $joinColumns;
  308. if (isset($manyToOneElement->cascade)) {
  309. $mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade);
  310. }
  311. if (isset($manyToOneElement->{'orphan-removal'})) {
  312. $mapping['orphanRemoval'] = (bool)$manyToOneElement->{'orphan-removal'};
  313. }
  314. $metadata->mapManyToOne($mapping);
  315. }
  316. }
  317. // Evaluate <many-to-many ...> mappings
  318. if (isset($xmlRoot->{'many-to-many'})) {
  319. foreach ($xmlRoot->{'many-to-many'} as $manyToManyElement) {
  320. $mapping = array(
  321. 'fieldName' => (string)$manyToManyElement['field'],
  322. 'targetEntity' => (string)$manyToManyElement['target-entity']
  323. );
  324. if (isset($manyToManyElement['fetch'])) {
  325. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$manyToManyElement['fetch']);
  326. }
  327. if (isset($manyToManyElement['mapped-by'])) {
  328. $mapping['mappedBy'] = (string)$manyToManyElement['mapped-by'];
  329. } else if (isset($manyToManyElement->{'join-table'})) {
  330. if (isset($manyToManyElement['inversed-by'])) {
  331. $mapping['inversedBy'] = (string)$manyToManyElement['inversed-by'];
  332. }
  333. $joinTableElement = $manyToManyElement->{'join-table'};
  334. $joinTable = array(
  335. 'name' => (string)$joinTableElement['name']
  336. );
  337. if (isset($joinTableElement['schema'])) {
  338. $joinTable['schema'] = (string)$joinTableElement['schema'];
  339. }
  340. foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  341. $joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  342. }
  343. foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) {
  344. $joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  345. }
  346. $mapping['joinTable'] = $joinTable;
  347. }
  348. if (isset($manyToManyElement->cascade)) {
  349. $mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
  350. }
  351. if (isset($manyToManyElement->{'orphan-removal'})) {
  352. $mapping['orphanRemoval'] = (bool)$manyToManyElement->{'orphan-removal'};
  353. }
  354. if (isset($manyToManyElement->{'order-by'})) {
  355. $orderBy = array();
  356. foreach ($manyToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) {
  357. $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction'];
  358. }
  359. $mapping['orderBy'] = $orderBy;
  360. }
  361. if (isset($manyToManyElement['index-by'])) {
  362. $mapping['indexBy'] = (string)$manyToManyElement['index-by'];
  363. } else if (isset($manyToManyElement->{'index-by'})) {
  364. throw new \InvalidArgumentException("<index-by /> is not a valid tag");
  365. }
  366. $metadata->mapManyToMany($mapping);
  367. }
  368. }
  369. // Evaluate <lifecycle-callbacks...>
  370. if (isset($xmlRoot->{'lifecycle-callbacks'})) {
  371. foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
  372. $metadata->addLifecycleCallback((string)$lifecycleCallback['method'], constant('Doctrine\ORM\Events::' . (string)$lifecycleCallback['type']));
  373. }
  374. }
  375. }
  376. /**
  377. * Constructs a joinColumn mapping array based on the information
  378. * found in the given SimpleXMLElement.
  379. *
  380. * @param $joinColumnElement The XML element.
  381. * @return array The mapping array.
  382. */
  383. private function _getJoinColumnMapping(SimpleXMLElement $joinColumnElement)
  384. {
  385. $joinColumn = array(
  386. 'name' => (string)$joinColumnElement['name'],
  387. 'referencedColumnName' => (string)$joinColumnElement['referenced-column-name']
  388. );
  389. if (isset($joinColumnElement['unique'])) {
  390. $joinColumn['unique'] = ((string)$joinColumnElement['unique'] == "false") ? false : true;
  391. }
  392. if (isset($joinColumnElement['nullable'])) {
  393. $joinColumn['nullable'] = ((string)$joinColumnElement['nullable'] == "false") ? false : true;
  394. }
  395. if (isset($joinColumnElement['on-delete'])) {
  396. $joinColumn['onDelete'] = (string)$joinColumnElement['on-delete'];
  397. }
  398. if (isset($joinColumnElement['on-update'])) {
  399. $joinColumn['onUpdate'] = (string)$joinColumnElement['on-update'];
  400. }
  401. if (isset($joinColumnElement['column-definition'])) {
  402. $joinColumn['columnDefinition'] = (string)$joinColumnElement['column-definition'];
  403. }
  404. return $joinColumn;
  405. }
  406. /**
  407. * Gathers a list of cascade options found in the given cascade element.
  408. *
  409. * @param $cascadeElement The cascade element.
  410. * @return array The list of cascade options.
  411. */
  412. private function _getCascadeMappings($cascadeElement)
  413. {
  414. $cascades = array();
  415. foreach ($cascadeElement->children() as $action) {
  416. // According to the JPA specifications, XML uses "cascade-persist"
  417. // instead of "persist". Here, both variations
  418. // are supported because both YAML and Annotation use "persist"
  419. // and we want to make sure that this driver doesn't need to know
  420. // anything about the supported cascading actions
  421. $cascades[] = str_replace('cascade-', '', $action->getName());
  422. }
  423. return $cascades;
  424. }
  425. /**
  426. * {@inheritdoc}
  427. */
  428. protected function _loadMappingFile($file)
  429. {
  430. $result = array();
  431. $xmlElement = simplexml_load_file($file);
  432. if (isset($xmlElement->entity)) {
  433. foreach ($xmlElement->entity as $entityElement) {
  434. $entityName = (string)$entityElement['name'];
  435. $result[$entityName] = $entityElement;
  436. }
  437. } else if (isset($xmlElement->{'mapped-superclass'})) {
  438. foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) {
  439. $className = (string)$mappedSuperClass['name'];
  440. $result[$className] = $mappedSuperClass;
  441. }
  442. }
  443. return $result;
  444. }
  445. }