Annotation.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Gedmo\Loggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver,
  4. Doctrine\Common\Annotations\AnnotationReader,
  5. Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is an annotation mapping driver for Loggable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from Annotations 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 Annotation
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Annotation implements Driver
  20. {
  21. /**
  22. * Annotation to define the tree type
  23. */
  24. const ANNOTATION_LOGGABLE = 'Gedmo\Loggable\Mapping\Loggable';
  25. /**
  26. * List of tree strategies available
  27. *
  28. * @var array
  29. */
  30. private $actions = array(
  31. 'create', 'update', 'delete'
  32. );
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function validateFullMetadata($meta, array $config)
  37. {
  38. if (isset($config['actions']) && is_array($config['actions'])) {
  39. foreach ($config['actions'] as $action) {
  40. if (!in_array($action, $this->actions)) {
  41. throw new InvalidMappingException("Action {$action} for class: {$meta->name} is invalid");
  42. }
  43. }
  44. }
  45. if (isset($config['actions']) && !is_array($config['actions'])) {
  46. throw new InvalidMappingException("Actions for class: {$meta->name} should be an array");
  47. }
  48. if (!method_exists($meta->getReflectionClass(), '__toString')) {
  49. throw new InvalidMappingException("class: {$meta->name} should implement __toString() method");
  50. }
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. public function readExtendedMetadata($meta, array &$config)
  56. {
  57. require_once __DIR__ . '/../Annotations.php';
  58. $reader = new AnnotationReader();
  59. $reader->setAnnotationNamespaceAlias('Gedmo\Loggable\Mapping\\', 'gedmo');
  60. $class = $meta->getReflectionClass();
  61. // class annotations
  62. $classAnnotations = $reader->getClassAnnotations($class);
  63. if (isset($classAnnotations[self::ANNOTATION_LOGGABLE])) {
  64. $annot = $classAnnotations[self::ANNOTATION_LOGGABLE];
  65. $config['actions'] = $annot->actions;
  66. }
  67. }
  68. }