Annotation.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 Uploadable
  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['callback'] = $annot->callback;
  76. $config['filenameGenerator'] = $annot->filenameGenerator;
  77. $config['maxSize'] = (double) $annot->maxSize;
  78. $config['allowedTypes'] = $annot->allowedTypes;
  79. $config['disallowedTypes'] = $annot->disallowedTypes;
  80. foreach ($class->getProperties() as $prop) {
  81. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {
  82. $config['fileMimeTypeField'] = $prop->getName();
  83. }
  84. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_PATH)) {
  85. $config['filePathField'] = $prop->getName();
  86. }
  87. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_SIZE)) {
  88. $config['fileSizeField'] = $prop->getName();
  89. }
  90. }
  91. Validator::validateConfiguration($meta, $config);
  92. }
  93. /*
  94. // Code in case we need to identify entities which are not Uploadables, but have associations
  95. // with other Uploadable entities
  96. } else {
  97. // We need to check if this class has a relation with Uploadable entities
  98. $associations = $meta->getAssociationMappings();
  99. foreach ($associations as $field => $association) {
  100. $refl = new \ReflectionClass($association['targetEntity']);
  101. if ($annot = $this->reader->getClassAnnotation($refl, self::UPLOADABLE)) {
  102. $config['hasUploadables'] = true;
  103. if (!isset($config['uploadables'])) {
  104. $config['uploadables'] = array();
  105. }
  106. $config['uploadables'][] = array(
  107. 'class' => $association['targetEntity'],
  108. 'property' => $association['fieldName']
  109. );
  110. }
  111. }
  112. }*/
  113. $this->validateFullMetadata($meta, $config);
  114. }
  115. /**
  116. * Passes in the mapping read by original driver
  117. *
  118. * @param $driver
  119. * @return void
  120. */
  121. public function setOriginalDriver($driver)
  122. {
  123. $this->_originalDriver = $driver;
  124. }
  125. }