Yaml.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. protected function _loadMappingFile($file)
  75. {
  76. return \Symfony\Component\Yaml\Yaml::load($file);
  77. }
  78. }