Xml.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  5. Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is a xml mapping driver for Loggable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from xml specificaly for Loggable
  10. * extension.
  11. *
  12. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  15. * @package Gedmo.Loggable.Mapping.Driver
  16. * @subpackage Xml
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class Xml extends BaseXml
  21. {
  22. /**
  23. * {@inheritDoc}
  24. */
  25. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  26. {
  27. /**
  28. * @var \SimpleXmlElement $xml
  29. */
  30. $xml = $this->_getMapping($meta->name);
  31. $xmlDoctrine = $xml;
  32. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  33. if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
  34. if (isset($xml->loggable)) {
  35. /**
  36. * @var SimpleXMLElement $data;
  37. */
  38. $data = $xml->loggable;
  39. $config['loggable'] = true;
  40. if ($this->_isAttributeSet($data, 'log-entry-class')) {
  41. $class = $this->_getAttribute($data, 'log-entry-class');
  42. if (!class_exists($class)) {
  43. throw new InvalidMappingException("LogEntry class: {$class} does not exist.");
  44. }
  45. $config['logEntryClass'] = $class;
  46. }
  47. }
  48. }
  49. if (isset($xmlDoctrine->field)) {
  50. $this->inspectElementForVersioned($xmlDoctrine->field, $config, $meta);
  51. }
  52. if (isset($xmlDoctrine->{'many-to-one'})) {
  53. $this->inspectElementForVersioned($xmlDoctrine->{'many-to-one'}, $config, $meta);
  54. }
  55. if (isset($xmlDoctrine->{'one-to-one'})) {
  56. $this->inspectElementForVersioned($xmlDoctrine->{'one-to-one'}, $config, $meta);
  57. }
  58. if (!$meta->isMappedSuperclass && $config) {
  59. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  60. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  61. }
  62. if (isset($config['versioned']) && !isset($config['loggable'])) {
  63. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  64. }
  65. }
  66. }
  67. /**
  68. * Searches mappings on element for versioned fields
  69. *
  70. * @param SimpleXMLElement $element
  71. * @param array $config
  72. * @param ClassMetadata $meta
  73. */
  74. private function inspectElementForVersioned(\SimpleXMLElement $element, array &$config, ClassMetadata $meta)
  75. {
  76. foreach ($element as $mapping) {
  77. $mappingDoctrine = $mapping;
  78. /**
  79. * @var \SimpleXmlElement $mapping
  80. */
  81. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  82. $isAssoc = $this->_isAttributeSet($mappingDoctrine, 'field');
  83. $field = $this->_getAttribute($mappingDoctrine, $isAssoc ? 'field' : 'name');
  84. if (isset($mapping->versioned)) {
  85. if ($isAssoc && !$meta->associationMappings[$field]['isOwningSide']) {
  86. throw new InvalidMappingException("Cannot version [{$field}] as it is not the owning side in object - {$meta->name}");
  87. }
  88. $config['versioned'][] = $field;
  89. }
  90. }
  91. }
  92. }