Yaml.php 4.3 KB

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