Yaml.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File;
  4. use Gedmo\Mapping\Driver;
  5. use Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is a yaml mapping driver for Loggable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from yaml specifically for Loggable
  10. * extension.
  11. *
  12. * @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class Yaml extends File implements Driver
  17. {
  18. /**
  19. * File extension
  20. * @var string
  21. */
  22. protected $_extension = '.dcm.yml';
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function readExtendedMetadata($meta, array &$config)
  27. {
  28. $mapping = $this->_getMapping($meta->name);
  29. if (isset($mapping['gedmo'])) {
  30. $classMapping = $mapping['gedmo'];
  31. if (isset($classMapping['loggable'])) {
  32. $config['loggable'] = true;
  33. if (isset ($classMapping['loggable']['logEntryClass'])) {
  34. if (!$cl = $this->getRelatedClassName($meta, $classMapping['loggable']['logEntryClass'])) {
  35. throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist.");
  36. }
  37. $config['logEntryClass'] = $cl;
  38. }
  39. }
  40. }
  41. if (isset($mapping['fields'])) {
  42. foreach ($mapping['fields'] as $field => $fieldMapping) {
  43. if (isset($fieldMapping['gedmo'])) {
  44. if (in_array('versioned', $fieldMapping['gedmo'])) {
  45. if ($meta->isCollectionValuedAssociation($field)) {
  46. throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
  47. }
  48. // fields cannot be overrided and throws mapping exception
  49. $config['versioned'][] = $field;
  50. }
  51. }
  52. }
  53. }
  54. if (isset($mapping['attributeOverride'])) {
  55. foreach ($mapping['attributeOverride'] as $field => $fieldMapping) {
  56. if (isset($fieldMapping['gedmo'])) {
  57. if (in_array('versioned', $fieldMapping['gedmo'])) {
  58. if ($meta->isCollectionValuedAssociation($field)) {
  59. throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
  60. }
  61. // fields cannot be overrided and throws mapping exception
  62. $config['versioned'][] = $field;
  63. }
  64. }
  65. }
  66. }
  67. if (isset($mapping['manyToOne'])) {
  68. foreach ($mapping['manyToOne'] as $field => $fieldMapping) {
  69. if (isset($fieldMapping['gedmo'])) {
  70. if (in_array('versioned', $fieldMapping['gedmo'])) {
  71. if ($meta->isCollectionValuedAssociation($field)) {
  72. throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
  73. }
  74. // fields cannot be overrided and throws mapping exception
  75. $config['versioned'][] = $field;
  76. }
  77. }
  78. }
  79. }
  80. if (isset($mapping['oneToOne'])) {
  81. foreach ($mapping['oneToOne'] as $field => $fieldMapping) {
  82. if (isset($fieldMapping['gedmo'])) {
  83. if (in_array('versioned', $fieldMapping['gedmo'])) {
  84. if ($meta->isCollectionValuedAssociation($field)) {
  85. throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
  86. }
  87. // fields cannot be overrided and throws mapping exception
  88. $config['versioned'][] = $field;
  89. }
  90. }
  91. }
  92. }
  93. if (isset($mapping['embedded'])) {
  94. foreach ($mapping['embedded'] as $field => $fieldMapping) {
  95. if (isset($fieldMapping['gedmo'])) {
  96. if (in_array('versioned', $fieldMapping['gedmo'])) {
  97. if ($meta->isCollectionValuedAssociation($field)) {
  98. throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
  99. }
  100. // fields cannot be overrided and throws mapping exception
  101. $mapping = $this->_getMapping($fieldMapping['class']);
  102. $this->inspectEmbeddedForVersioned($field, $mapping, $config);
  103. }
  104. }
  105. }
  106. }
  107. if (!$meta->isMappedSuperclass && $config) {
  108. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  109. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  110. }
  111. if (isset($config['versioned']) && !isset($config['loggable'])) {
  112. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  113. }
  114. }
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. protected function _loadMappingFile($file)
  120. {
  121. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  122. }
  123. /**
  124. * @param string $field
  125. * @param array $mapping
  126. * @param array $config
  127. */
  128. private function inspectEmbeddedForVersioned($field, array $mapping, array &$config)
  129. {
  130. if (isset($mapping['fields'])) {
  131. foreach ($mapping['fields'] as $property => $fieldMapping) {
  132. $config['versioned'][] = $field . '.' . $property;
  133. }
  134. }
  135. }
  136. }