Xml.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Gedmo\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. SimpleXMLElement;
  5. /**
  6. * The mapping XmlDriver abstract class, defines the
  7. * metadata extraction function common among all
  8. * all drivers used on these extensions by file based
  9. * drivers.
  10. *
  11. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Common.Mapping
  14. * @subpackage FileDriver
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. abstract class Xml extends File
  19. {
  20. const GEDMO_NAMESPACE_URI = 'http://gediminasm.org/schemas/orm/doctrine-extensions-mapping';
  21. const DOCTRINE_NAMESPACE_URI = 'http://doctrine-project.org/schemas/orm/doctrine-mapping';
  22. /**
  23. * File extension
  24. * @var string
  25. */
  26. protected $_extension = '.dcm.xml';
  27. /**
  28. * Get attribute value.
  29. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  30. *
  31. * @param SimpleXMLElement $node
  32. * @param $attributeName
  33. * @return string
  34. */
  35. protected function _getAttribute(SimpleXmlElement $node, $attributeName)
  36. {
  37. $attributes = $node->attributes();
  38. return (string)$attributes[$attributeName];
  39. }
  40. /**
  41. * does attribute exist under a specific node
  42. * As we are supporting namespaces the only way to get to the attributes under a node is to use attributes function on it
  43. *
  44. * @param SimpleXMLElement $node
  45. * @param $attributeName
  46. * @return string
  47. */
  48. protected function _isAttributeSet(SimpleXmlElement $node, $attributeName)
  49. {
  50. $attributes = $node->attributes();
  51. return isset($attributes[$attributeName]);
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. protected function _loadMappingFile($file)
  57. {
  58. $result = array();
  59. $xmlElement = simplexml_load_file($file);
  60. $xmlElement = $xmlElement->children(self::DOCTRINE_NAMESPACE_URI);
  61. if (isset($xmlElement->entity)) {
  62. foreach ($xmlElement->entity as $entityElement) {
  63. $entityName = $this->_getAttribute($entityElement, 'name');
  64. $result[$entityName] = $entityElement;
  65. }
  66. } else if (isset($xmlElement->{'mapped-superclass'})) {
  67. foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) {
  68. $className = $this->_getAttribute($mappedSuperClass, 'name');
  69. $result[$className] = $mappedSuperClass;
  70. }
  71. }
  72. return $result;
  73. }
  74. }