Yaml.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  45. $mapping = $yaml[$meta->name];
  46. if (isset($mapping['gedmo'])) {
  47. $classMapping = $mapping['gedmo'];
  48. if (isset($classMapping['loggable'])) {
  49. $config['loggable'] = true;
  50. if (isset ($classMapping['loggable']['logEntryClass'])) {
  51. if (!class_exists($classMapping['loggable']['logEntryClass'])) {
  52. throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist.");
  53. }
  54. $config['logEntryClass'] = $classMapping['loggable']['logEntryClass'];
  55. }
  56. }
  57. }
  58. if (isset($mapping['fields'])) {
  59. foreach ($mapping['fields'] 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. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. protected function _loadMappingFile($file)
  76. {
  77. return \Symfony\Component\Yaml\Yaml::load($file);
  78. }
  79. }