Yaml.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 (is_array($meta->identifier) && count($meta->identifier) > 1) {
  33. throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
  34. }
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  40. {
  41. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  42. $mapping = $yaml[$meta->name];
  43. if (isset($mapping['gedmo'])) {
  44. $classMapping = $mapping['gedmo'];
  45. if (isset($classMapping['loggable'])) {
  46. $config['loggable'] = true;
  47. if (isset ($classMapping['loggable']['logEntryClass'])) {
  48. if (!class_exists($classMapping['loggable']['logEntryClass'])) {
  49. throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist.");
  50. }
  51. $config['logEntryClass'] = $classMapping['loggable']['logEntryClass'];
  52. }
  53. }
  54. }
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. protected function _loadMappingFile($file)
  60. {
  61. return \Symfony\Component\Yaml\Yaml::load($file);
  62. }
  63. }