XmlDriver.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. if (isset($xmlRoot['repository-class'])) {
  51. $metadata->setCustomRepositoryClass((string)$xmlRoot['repository-class']);
  52. }
  53. if (isset($xmlRoot['read-only']) && $xmlRoot['read-only'] == "true") {
  54. $metadata->markReadOnly();
  55. }
  56. } else if ($xmlRoot->getName() == 'mapped-superclass') {
  57. $metadata->setCustomRepositoryClass(
  58. isset($xmlRoot['repository-class']) ? (string)$xmlRoot['repository-class'] : null
  59. );
  60. $metadata->isMappedSuperclass = true;
  61. } else {
  62. throw MappingException::classIsNotAValidEntityOrMappedSuperClass($className);
  63. }
  64. // Evaluate <entity...> attributes
  65. $table = array();
  66. if (isset($xmlRoot['table'])) {
  67. $table['name'] = (string)$xmlRoot['table'];
  68. }
  69. $metadata->setPrimaryTable($table);
  70. // Evaluate named queries
  71. if (isset($xmlRoot['named-queries'])) {
  72. foreach ($xmlRoot->{'named-queries'}->{'named-query'} as $namedQueryElement) {
  73. $metadata->addNamedQuery(array(
  74. 'name' => (string)$namedQueryElement['name'],
  75. 'query' => (string)$namedQueryElement['query']
  76. ));
  77. }
  78. }
  79. /* not implemented specially anyway. use table = schema.table
  80. if (isset($xmlRoot['schema'])) {
  81. $metadata->table['schema'] = (string)$xmlRoot['schema'];
  82. }*/
  83. if (isset($xmlRoot['inheritance-type'])) {
  84. $inheritanceType = (string)$xmlRoot['inheritance-type'];
  85. $metadata->setInheritanceType(constant('Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType));
  86. if ($metadata->inheritanceType != \Doctrine\ORM\Mapping\ClassMetadata::INHERITANCE_TYPE_NONE) {
  87. // Evaluate <discriminator-column...>
  88. if (isset($xmlRoot->{'discriminator-column'})) {
  89. $discrColumn = $xmlRoot->{'discriminator-column'};
  90. $metadata->setDiscriminatorColumn(array(
  91. 'name' => (string)$discrColumn['name'],
  92. 'type' => (string)$discrColumn['type'],
  93. 'length' => (string)$discrColumn['length']
  94. ));
  95. } else {
  96. $metadata->setDiscriminatorColumn(array('name' => 'dtype', 'type' => 'string', 'length' => 255));
  97. }
  98. // Evaluate <discriminator-map...>
  99. if (isset($xmlRoot->{'discriminator-map'})) {
  100. $map = array();
  101. foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) {
  102. $map[(string)$discrMapElement['value']] = (string)$discrMapElement['class'];
  103. }
  104. $metadata->setDiscriminatorMap($map);
  105. }
  106. }
  107. }
  108. // Evaluate <change-tracking-policy...>
  109. if (isset($xmlRoot['change-tracking-policy'])) {
  110. $metadata->setChangeTrackingPolicy(constant('Doctrine\ORM\Mapping\ClassMetadata::CHANGETRACKING_'
  111. . strtoupper((string)$xmlRoot['change-tracking-policy'])));
  112. }
  113. // Evaluate <indexes...>
  114. if (isset($xmlRoot->indexes)) {
  115. $metadata->table['indexes'] = array();
  116. foreach ($xmlRoot->indexes->index as $index) {
  117. $columns = explode(',', (string)$index['columns']);
  118. if (isset($index['name'])) {
  119. $metadata->table['indexes'][(string)$index['name']] = array(
  120. 'columns' => $columns
  121. );
  122. } else {
  123. $metadata->table['indexes'][] = array(
  124. 'columns' => $columns
  125. );
  126. }
  127. }
  128. }
  129. // Evaluate <unique-constraints..>
  130. if (isset($xmlRoot->{'unique-constraints'})) {
  131. $metadata->table['uniqueConstraints'] = array();
  132. foreach ($xmlRoot->{'unique-constraints'}->{'unique-constraint'} as $unique) {
  133. $columns = explode(',', (string)$unique['columns']);
  134. if (isset($unique['name'])) {
  135. $metadata->table['uniqueConstraints'][(string)$unique['name']] = array(
  136. 'columns' => $columns
  137. );
  138. } else {
  139. $metadata->table['uniqueConstraints'][] = array(
  140. 'columns' => $columns
  141. );
  142. }
  143. }
  144. }
  145. // Evaluate <field ...> mappings
  146. if (isset($xmlRoot->field)) {
  147. foreach ($xmlRoot->field as $fieldMapping) {
  148. $mapping = array(
  149. 'fieldName' => (string)$fieldMapping['name'],
  150. );
  151. if (isset($fieldMapping['type'])) {
  152. $mapping['type'] = (string)$fieldMapping['type'];
  153. }
  154. if (isset($fieldMapping['column'])) {
  155. $mapping['columnName'] = (string)$fieldMapping['column'];
  156. }
  157. if (isset($fieldMapping['length'])) {
  158. $mapping['length'] = (int)$fieldMapping['length'];
  159. }
  160. if (isset($fieldMapping['precision'])) {
  161. $mapping['precision'] = (int)$fieldMapping['precision'];
  162. }
  163. if (isset($fieldMapping['scale'])) {
  164. $mapping['scale'] = (int)$fieldMapping['scale'];
  165. }
  166. if (isset($fieldMapping['unique'])) {
  167. $mapping['unique'] = ((string)$fieldMapping['unique'] == "false") ? false : true;
  168. }
  169. if (isset($fieldMapping['options'])) {
  170. $mapping['options'] = (array)$fieldMapping['options'];
  171. }
  172. if (isset($fieldMapping['nullable'])) {
  173. $mapping['nullable'] = ((string)$fieldMapping['nullable'] == "false") ? false : true;
  174. }
  175. if (isset($fieldMapping['version']) && $fieldMapping['version']) {
  176. $metadata->setVersionMapping($mapping);
  177. }
  178. if (isset($fieldMapping['column-definition'])) {
  179. $mapping['columnDefinition'] = (string)$fieldMapping['column-definition'];
  180. }
  181. $metadata->mapField($mapping);
  182. }
  183. }
  184. // Evaluate <id ...> mappings
  185. $associationIds = array();
  186. foreach ($xmlRoot->id as $idElement) {
  187. if ((bool)$idElement['association-key'] == true) {
  188. $associationIds[(string)$idElement['name']] = true;
  189. continue;
  190. }
  191. $mapping = array(
  192. 'id' => true,
  193. 'fieldName' => (string)$idElement['name']
  194. );
  195. if (isset($idElement['type'])) {
  196. $mapping['type'] = (string)$idElement['type'];
  197. }
  198. if (isset($idElement['column'])) {
  199. $mapping['columnName'] = (string)$idElement['column'];
  200. }
  201. if (isset($idElement['column-definition'])) {
  202. $mapping['columnDefinition'] = (string)$idElement['column-definition'];
  203. }
  204. $metadata->mapField($mapping);
  205. if (isset($idElement->generator)) {
  206. $strategy = isset($idElement->generator['strategy']) ?
  207. (string)$idElement->generator['strategy'] : 'AUTO';
  208. $metadata->setIdGeneratorType(constant('Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_'
  209. . $strategy));
  210. }
  211. // Check for SequenceGenerator/TableGenerator definition
  212. if (isset($idElement->{'sequence-generator'})) {
  213. $seqGenerator = $idElement->{'sequence-generator'};
  214. $metadata->setSequenceGeneratorDefinition(array(
  215. 'sequenceName' => (string)$seqGenerator['sequence-name'],
  216. 'allocationSize' => (string)$seqGenerator['allocation-size'],
  217. 'initialValue' => (string)$seqGenerator['initial-value']
  218. ));
  219. } else if (isset($idElement->{'table-generator'})) {
  220. throw MappingException::tableIdGeneratorNotImplemented($className);
  221. }
  222. }
  223. // Evaluate <one-to-one ...> mappings
  224. if (isset($xmlRoot->{'one-to-one'})) {
  225. foreach ($xmlRoot->{'one-to-one'} as $oneToOneElement) {
  226. $mapping = array(
  227. 'fieldName' => (string)$oneToOneElement['field'],
  228. 'targetEntity' => (string)$oneToOneElement['target-entity']
  229. );
  230. if (isset($associationIds[$mapping['fieldName']])) {
  231. $mapping['id'] = true;
  232. }
  233. if (isset($oneToOneElement['fetch'])) {
  234. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$oneToOneElement['fetch']);
  235. }
  236. if (isset($oneToOneElement['mapped-by'])) {
  237. $mapping['mappedBy'] = (string)$oneToOneElement['mapped-by'];
  238. } else {
  239. if (isset($oneToOneElement['inversed-by'])) {
  240. $mapping['inversedBy'] = (string)$oneToOneElement['inversed-by'];
  241. }
  242. $joinColumns = array();
  243. if (isset($oneToOneElement->{'join-column'})) {
  244. $joinColumns[] = $this->_getJoinColumnMapping($oneToOneElement->{'join-column'});
  245. } else if (isset($oneToOneElement->{'join-columns'})) {
  246. foreach ($oneToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  247. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  248. }
  249. }
  250. $mapping['joinColumns'] = $joinColumns;
  251. }
  252. if (isset($oneToOneElement->cascade)) {
  253. $mapping['cascade'] = $this->_getCascadeMappings($oneToOneElement->cascade);
  254. }
  255. if (isset($oneToOneElement['orphan-removal'])) {
  256. $mapping['orphanRemoval'] = (bool)$oneToOneElement['orphan-removal'];
  257. }
  258. $metadata->mapOneToOne($mapping);
  259. }
  260. }
  261. // Evaluate <one-to-many ...> mappings
  262. if (isset($xmlRoot->{'one-to-many'})) {
  263. foreach ($xmlRoot->{'one-to-many'} as $oneToManyElement) {
  264. $mapping = array(
  265. 'fieldName' => (string)$oneToManyElement['field'],
  266. 'targetEntity' => (string)$oneToManyElement['target-entity'],
  267. 'mappedBy' => (string)$oneToManyElement['mapped-by']
  268. );
  269. if (isset($oneToManyElement['fetch'])) {
  270. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$oneToManyElement['fetch']);
  271. }
  272. if (isset($oneToManyElement->cascade)) {
  273. $mapping['cascade'] = $this->_getCascadeMappings($oneToManyElement->cascade);
  274. }
  275. if (isset($oneToManyElement['orphan-removal'])) {
  276. $mapping['orphanRemoval'] = (bool)$oneToManyElement['orphan-removal'];
  277. }
  278. if (isset($oneToManyElement->{'order-by'})) {
  279. $orderBy = array();
  280. foreach ($oneToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) {
  281. $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction'];
  282. }
  283. $mapping['orderBy'] = $orderBy;
  284. }
  285. if (isset($oneToManyElement['index-by'])) {
  286. $mapping['indexBy'] = (string)$oneToManyElement['index-by'];
  287. } else if (isset($oneToManyElement->{'index-by'})) {
  288. throw new \InvalidArgumentException("<index-by /> is not a valid tag");
  289. }
  290. $metadata->mapOneToMany($mapping);
  291. }
  292. }
  293. // Evaluate <many-to-one ...> mappings
  294. if (isset($xmlRoot->{'many-to-one'})) {
  295. foreach ($xmlRoot->{'many-to-one'} as $manyToOneElement) {
  296. $mapping = array(
  297. 'fieldName' => (string)$manyToOneElement['field'],
  298. 'targetEntity' => (string)$manyToOneElement['target-entity']
  299. );
  300. if (isset($associationIds[$mapping['fieldName']])) {
  301. $mapping['id'] = true;
  302. }
  303. if (isset($manyToOneElement['fetch'])) {
  304. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$manyToOneElement['fetch']);
  305. }
  306. if (isset($manyToOneElement['inversed-by'])) {
  307. $mapping['inversedBy'] = (string)$manyToOneElement['inversed-by'];
  308. }
  309. $joinColumns = array();
  310. if (isset($manyToOneElement->{'join-column'})) {
  311. $joinColumns[] = $this->_getJoinColumnMapping($manyToOneElement->{'join-column'});
  312. } else if (isset($manyToOneElement->{'join-columns'})) {
  313. foreach ($manyToOneElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  314. $joinColumns[] = $this->_getJoinColumnMapping($joinColumnElement);
  315. }
  316. }
  317. $mapping['joinColumns'] = $joinColumns;
  318. if (isset($manyToOneElement->cascade)) {
  319. $mapping['cascade'] = $this->_getCascadeMappings($manyToOneElement->cascade);
  320. }
  321. $metadata->mapManyToOne($mapping);
  322. }
  323. }
  324. // Evaluate <many-to-many ...> mappings
  325. if (isset($xmlRoot->{'many-to-many'})) {
  326. foreach ($xmlRoot->{'many-to-many'} as $manyToManyElement) {
  327. $mapping = array(
  328. 'fieldName' => (string)$manyToManyElement['field'],
  329. 'targetEntity' => (string)$manyToManyElement['target-entity']
  330. );
  331. if (isset($manyToManyElement['fetch'])) {
  332. $mapping['fetch'] = constant('Doctrine\ORM\Mapping\ClassMetadata::FETCH_' . (string)$manyToManyElement['fetch']);
  333. }
  334. if (isset($manyToManyElement['orphan-removal'])) {
  335. $mapping['orphanRemoval'] = (bool)$manyToManyElement['orphan-removal'];
  336. }
  337. if (isset($manyToManyElement['mapped-by'])) {
  338. $mapping['mappedBy'] = (string)$manyToManyElement['mapped-by'];
  339. } else if (isset($manyToManyElement->{'join-table'})) {
  340. if (isset($manyToManyElement['inversed-by'])) {
  341. $mapping['inversedBy'] = (string)$manyToManyElement['inversed-by'];
  342. }
  343. $joinTableElement = $manyToManyElement->{'join-table'};
  344. $joinTable = array(
  345. 'name' => (string)$joinTableElement['name']
  346. );
  347. if (isset($joinTableElement['schema'])) {
  348. $joinTable['schema'] = (string)$joinTableElement['schema'];
  349. }
  350. foreach ($joinTableElement->{'join-columns'}->{'join-column'} as $joinColumnElement) {
  351. $joinTable['joinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  352. }
  353. foreach ($joinTableElement->{'inverse-join-columns'}->{'join-column'} as $joinColumnElement) {
  354. $joinTable['inverseJoinColumns'][] = $this->_getJoinColumnMapping($joinColumnElement);
  355. }
  356. $mapping['joinTable'] = $joinTable;
  357. }
  358. if (isset($manyToManyElement->cascade)) {
  359. $mapping['cascade'] = $this->_getCascadeMappings($manyToManyElement->cascade);
  360. }
  361. if (isset($manyToManyElement->{'order-by'})) {
  362. $orderBy = array();
  363. foreach ($manyToManyElement->{'order-by'}->{'order-by-field'} AS $orderByField) {
  364. $orderBy[(string)$orderByField['name']] = (string)$orderByField['direction'];
  365. }
  366. $mapping['orderBy'] = $orderBy;
  367. }
  368. if (isset($manyToManyElement['index-by'])) {
  369. $mapping['indexBy'] = (string)$manyToManyElement['index-by'];
  370. } else if (isset($manyToManyElement->{'index-by'})) {
  371. throw new \InvalidArgumentException("<index-by /> is not a valid tag");
  372. }
  373. $metadata->mapManyToMany($mapping);
  374. }
  375. }
  376. // Evaluate <lifecycle-callbacks...>
  377. if (isset($xmlRoot->{'lifecycle-callbacks'})) {
  378. foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) {
  379. $metadata->addLifecycleCallback((string)$lifecycleCallback['method'], constant('Doctrine\ORM\Events::' . (string)$lifecycleCallback['type']));
  380. }
  381. }
  382. }
  383. /**
  384. * Constructs a joinColumn mapping array based on the information
  385. * found in the given SimpleXMLElement.
  386. *
  387. * @param $joinColumnElement The XML element.
  388. * @return array The mapping array.
  389. */
  390. private function _getJoinColumnMapping(SimpleXMLElement $joinColumnElement)
  391. {
  392. $joinColumn = array(
  393. 'name' => (string)$joinColumnElement['name'],
  394. 'referencedColumnName' => (string)$joinColumnElement['referenced-column-name']
  395. );
  396. if (isset($joinColumnElement['unique'])) {
  397. $joinColumn['unique'] = ((string)$joinColumnElement['unique'] == "false") ? false : true;
  398. }
  399. if (isset($joinColumnElement['nullable'])) {
  400. $joinColumn['nullable'] = ((string)$joinColumnElement['nullable'] == "false") ? false : true;
  401. }
  402. if (isset($joinColumnElement['on-delete'])) {
  403. $joinColumn['onDelete'] = (string)$joinColumnElement['on-delete'];
  404. }
  405. if (isset($joinColumnElement['column-definition'])) {
  406. $joinColumn['columnDefinition'] = (string)$joinColumnElement['column-definition'];
  407. }
  408. return $joinColumn;
  409. }
  410. /**
  411. * Gathers a list of cascade options found in the given cascade element.
  412. *
  413. * @param $cascadeElement The cascade element.
  414. * @return array The list of cascade options.
  415. */
  416. private function _getCascadeMappings($cascadeElement)
  417. {
  418. $cascades = array();
  419. foreach ($cascadeElement->children() as $action) {
  420. // According to the JPA specifications, XML uses "cascade-persist"
  421. // instead of "persist". Here, both variations
  422. // are supported because both YAML and Annotation use "persist"
  423. // and we want to make sure that this driver doesn't need to know
  424. // anything about the supported cascading actions
  425. $cascades[] = str_replace('cascade-', '', $action->getName());
  426. }
  427. return $cascades;
  428. }
  429. /**
  430. * {@inheritdoc}
  431. */
  432. protected function _loadMappingFile($file)
  433. {
  434. $result = array();
  435. $xmlElement = simplexml_load_file($file);
  436. if (isset($xmlElement->entity)) {
  437. foreach ($xmlElement->entity as $entityElement) {
  438. $entityName = (string)$entityElement['name'];
  439. $result[$entityName] = $entityElement;
  440. }
  441. } else if (isset($xmlElement->{'mapped-superclass'})) {
  442. foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) {
  443. $className = (string)$mappedSuperClass['name'];
  444. $result[$className] = $mappedSuperClass;
  445. }
  446. }
  447. return $result;
  448. }
  449. }