Annotation.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AnnotationDriverInterface,
  4. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  5. Gedmo\Exception\InvalidMappingException,
  6. Gedmo\Uploadable\Mapping\Validator;
  7. /**
  8. * This is an annotation mapping driver for Uploadable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from Annotations specificaly for SoftDeleteable
  11. * extension.
  12. *
  13. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  14. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  15. * @package Gedmo.Uploadable.Mapping.Driver
  16. * @subpackage Annotation
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class Annotation implements AnnotationDriverInterface
  21. {
  22. /**
  23. * Annotation to define that this object is loggable
  24. */
  25. const UPLOADABLE = 'Gedmo\\Mapping\\Annotation\\Uploadable';
  26. const UPLOADABLE_FILE_MIME_TYPE = 'Gedmo\\Mapping\\Annotation\\UploadableFileMimeType';
  27. const UPLOADABLE_FILE_PATH = 'Gedmo\\Mapping\\Annotation\\UploadableFilePath';
  28. const UPLOADABLE_FILE_SIZE = 'Gedmo\\Mapping\\Annotation\\UploadableFileSize';
  29. /**
  30. * Annotation reader instance
  31. *
  32. * @var object
  33. */
  34. private $reader;
  35. /**
  36. * original driver if it is available
  37. */
  38. protected $_originalDriver = null;
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function setAnnotationReader($reader)
  43. {
  44. $this->reader = $reader;
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public function validateFullMetadata(ClassMetadata $meta, array $config)
  50. {
  51. // Nothing here for now
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function readExtendedMetadata($meta, array &$config)
  57. {
  58. $class = $meta->getReflectionClass();
  59. if (!$class) {
  60. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  61. // this happens when running annotation driver in combination with
  62. // static reflection services. This is not the nicest fix
  63. $class = new \ReflectionClass($meta->name);
  64. }
  65. // class annotations
  66. if ($annot = $this->reader->getClassAnnotation($class, self::UPLOADABLE)) {
  67. $config['uploadable'] = true;
  68. $config['allowOverwrite'] = $annot->allowOverwrite;
  69. $config['appendNumber'] = $annot->appendNumber;
  70. $config['path'] = $annot->path;
  71. $config['pathMethod'] = $annot->pathMethod;
  72. $config['fileMimeTypeField'] = false;
  73. $config['filePathField'] = false;
  74. $config['fileSizeField'] = false;
  75. $config['fileInfoProperty'] = $annot->fileInfoProperty;
  76. foreach ($class->getProperties() as $prop) {
  77. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {
  78. $config['fileMimeTypeField'] = $prop->getName();
  79. }
  80. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_PATH)) {
  81. $config['filePathField'] = $prop->getName();
  82. }
  83. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_SIZE)) {
  84. $config['fileSizeField'] = $prop->getName();
  85. }
  86. }
  87. Validator::validateConfiguration($meta, $config);
  88. }
  89. /*
  90. // Code in case we need to identify entities which are not Uploadables, but have associations
  91. // with other Uploadable entities
  92. } else {
  93. // We need to check if this class has a relation with Uploadable entities
  94. $associations = $meta->getAssociationMappings();
  95. foreach ($associations as $field => $association) {
  96. $refl = new \ReflectionClass($association['targetEntity']);
  97. if ($annot = $this->reader->getClassAnnotation($refl, self::UPLOADABLE)) {
  98. $config['hasUploadables'] = true;
  99. if (!isset($config['uploadables'])) {
  100. $config['uploadables'] = array();
  101. }
  102. $config['uploadables'][] = array(
  103. 'class' => $association['targetEntity'],
  104. 'property' => $association['fieldName']
  105. );
  106. }
  107. }
  108. }*/
  109. $this->validateFullMetadata($meta, $config);
  110. }
  111. /**
  112. * Passes in the mapping read by original driver
  113. *
  114. * @param $driver
  115. * @return void
  116. */
  117. public function setOriginalDriver($driver)
  118. {
  119. $this->_originalDriver = $driver;
  120. }
  121. }