Yaml.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. 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 specificaly 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 (!class_exists($classMapping['loggable']['logEntryClass'])) {
  35. throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist.");
  36. }
  37. $config['logEntryClass'] = $classMapping['loggable']['logEntryClass'];
  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 versioned [{$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['manyToOne'])) {
  55. foreach ($mapping['manyToOne'] 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 versioned [{$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['oneToOne'])) {
  68. foreach ($mapping['oneToOne'] 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 versioned [{$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 (!$meta->isMappedSuperclass && $config) {
  81. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  82. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  83. }
  84. if (isset($config['versioned']) && !isset($config['loggable'])) {
  85. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  86. }
  87. }
  88. }
  89. /**
  90. * {@inheritDoc}
  91. */
  92. protected function _loadMappingFile($file)
  93. {
  94. return \Symfony\Component\Yaml\Yaml::parse($file);
  95. }
  96. }