Xml.php 3.8 KB

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