Yaml.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * @package Gedmo.Loggable.Mapping.Driver
  15. * @subpackage Yaml
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Yaml extends File implements Driver
  20. {
  21. /**
  22. * File extension
  23. * @var string
  24. */
  25. protected $_extension = '.dcm.yml';
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function readExtendedMetadata($meta, array &$config)
  30. {
  31. $mapping = $this->_getMapping($meta->name);
  32. if (isset($mapping['gedmo'])) {
  33. $classMapping = $mapping['gedmo'];
  34. if (isset($classMapping['loggable'])) {
  35. $config['loggable'] = true;
  36. if (isset ($classMapping['loggable']['logEntryClass'])) {
  37. if (!class_exists($classMapping['loggable']['logEntryClass'])) {
  38. throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist.");
  39. }
  40. $config['logEntryClass'] = $classMapping['loggable']['logEntryClass'];
  41. }
  42. }
  43. }
  44. if (isset($mapping['fields'])) {
  45. foreach ($mapping['fields'] as $field => $fieldMapping) {
  46. if (isset($fieldMapping['gedmo'])) {
  47. if (in_array('versioned', $fieldMapping['gedmo'])) {
  48. if ($meta->isCollectionValuedAssociation($field)) {
  49. throw new InvalidMappingException("Cannot versioned [{$field}] as it is collection in object - {$meta->name}");
  50. }
  51. // fields cannot be overrided and throws mapping exception
  52. $config['versioned'][] = $field;
  53. }
  54. }
  55. }
  56. }
  57. if (isset($mapping['manyToOne'])) {
  58. foreach ($mapping['manyToOne'] 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['oneToOne'])) {
  71. foreach ($mapping['oneToOne'] 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 (!$meta->isMappedSuperclass && $config) {
  84. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  85. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  86. }
  87. if (isset($config['versioned']) && !isset($config['loggable'])) {
  88. throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
  89. }
  90. }
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. protected function _loadMappingFile($file)
  96. {
  97. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  98. }
  99. }