Yaml.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 readExtendedMetadata(ClassMetadata $meta, array &$config)
  31. {
  32. $mapping = $this->_getMapping($meta->name);
  33. if (isset($mapping['gedmo'])) {
  34. $classMapping = $mapping['gedmo'];
  35. if (isset($classMapping['loggable'])) {
  36. $config['loggable'] = true;
  37. if (isset ($classMapping['loggable']['logEntryClass'])) {
  38. if (!class_exists($classMapping['loggable']['logEntryClass'])) {
  39. throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist.");
  40. }
  41. $config['logEntryClass'] = $classMapping['loggable']['logEntryClass'];
  42. }
  43. }
  44. }
  45. if (isset($mapping['fields'])) {
  46. foreach ($mapping['fields'] as $field => $fieldMapping) {
  47. if (isset($fieldMapping['gedmo'])) {
  48. if (in_array('versioned', $fieldMapping['gedmo'])) {
  49. if ($meta->isCollectionValuedAssociation($field)) {
  50. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  51. }
  52. // fields cannot be overrided and throws mapping exception
  53. $config['versioned'][] = $field;
  54. }
  55. }
  56. }
  57. }
  58. if (isset($mapping['manyToOne'])) {
  59. foreach ($mapping['manyToOne'] as $field => $fieldMapping) {
  60. if (isset($fieldMapping['gedmo'])) {
  61. if (in_array('versioned', $fieldMapping['gedmo'])) {
  62. if ($meta->isCollectionValuedAssociation($field)) {
  63. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  64. }
  65. // fields cannot be overrided and throws mapping exception
  66. $config['versioned'][] = $field;
  67. }
  68. }
  69. }
  70. }
  71. if (isset($mapping['oneToOne'])) {
  72. foreach ($mapping['oneToOne'] as $field => $fieldMapping) {
  73. if (isset($fieldMapping['gedmo'])) {
  74. if (in_array('versioned', $fieldMapping['gedmo'])) {
  75. if ($meta->isCollectionValuedAssociation($field)) {
  76. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  77. }
  78. // fields cannot be overrided and throws mapping exception
  79. $config['versioned'][] = $field;
  80. }
  81. }
  82. }
  83. }
  84. if (!$meta->isMappedSuperclass && $config) {
  85. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  86. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  87. }
  88. if (isset($config['versioned']) && !isset($config['loggable'])) {
  89. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  90. }
  91. }
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. protected function _loadMappingFile($file)
  97. {
  98. return \Symfony\Component\Yaml\Yaml::load($file);
  99. }
  100. }