Xml.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 validateFullMetadata(ClassMetadata $meta, array $config)
  26. {
  27. if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
  28. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  29. }
  30. if (isset($config['versioned']) && !isset($config['loggable'])) {
  31. throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  32. }
  33. }
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  38. {
  39. /**
  40. * @var \SimpleXmlElement $xml
  41. */
  42. $xml = $this->_getMapping($meta->name);
  43. $xmlDoctrine = $xml;
  44. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  45. if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
  46. if (isset($xml->loggable)) {
  47. /**
  48. * @var SimpleXMLElement $data;
  49. */
  50. $data = $xml->loggable;
  51. $config['loggable'] = true;
  52. if ($this->_isAttributeSet($data, 'log-entry-class')) {
  53. $class = $this->_getAttribute($data, 'log-entry-class');
  54. if (!class_exists($class)) {
  55. throw new InvalidMappingException("LogEntry class: {$class} does not exist.");
  56. }
  57. $config['logEntryClass'] = $class;
  58. }
  59. }
  60. }
  61. if (isset($xmlDoctrine->field)) {
  62. $this->inspectElementForVersioned($xmlDoctrine->field, $config, $meta);
  63. }
  64. if (isset($xmlDoctrine->{'many-to-one'})) {
  65. $this->inspectElementForVersioned($xmlDoctrine->{'many-to-one'}, $config, $meta);
  66. }
  67. if (isset($xmlDoctrine->{'one-to-one'})) {
  68. $this->inspectElementForVersioned($xmlDoctrine->{'one-to-one'}, $config, $meta);
  69. }
  70. }
  71. /**
  72. * Searches mappings on element for versioned fields
  73. *
  74. * @param SimpleXMLElement $element
  75. * @param array $config
  76. * @param ClassMetadata $meta
  77. */
  78. private function inspectElementForVersioned(\SimpleXMLElement $element, array &$config, ClassMetadata $meta)
  79. {
  80. foreach ($element as $mapping) {
  81. $mappingDoctrine = $mapping;
  82. /**
  83. * @var \SimpleXmlElement $mapping
  84. */
  85. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  86. $isAssoc = $this->_isAttributeSet($mappingDoctrine, 'field');
  87. $field = $this->_getAttribute($mappingDoctrine, $isAssoc ? 'field' : 'name');
  88. if (isset($mapping->versioned)) {
  89. if ($isAssoc && !$meta->associationMappings[$field]['isOwningSide']) {
  90. throw new InvalidMappingException("Cannot version [{$field}] as it is not the owning side in object - {$meta->name}");
  91. }
  92. $config['versioned'][] = $field;
  93. }
  94. }
  95. }
  96. }