Annotation.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. const UPLOADABLE_FILE_INFO = 'Gedmo\\Mapping\\Annotation\\UploadableFileInfo';
  30. const UPLOADABLE_PATH = 'Gedmo\\Mapping\\Annotation\\UploadablePath';
  31. /**
  32. * Annotation reader instance
  33. *
  34. * @var object
  35. */
  36. private $reader;
  37. /**
  38. * original driver if it is available
  39. */
  40. protected $_originalDriver = null;
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function setAnnotationReader($reader)
  45. {
  46. $this->reader = $reader;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function validateFullMetadata(ClassMetadata $meta, array $config)
  52. {
  53. // Nothing here for now
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function readExtendedMetadata($meta, array &$config)
  59. {
  60. $class = $meta->getReflectionClass();
  61. if (!$class) {
  62. // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
  63. // this happens when running annotation driver in combination with
  64. // static reflection services. This is not the nicest fix
  65. $class = new \ReflectionClass($meta->name);
  66. }
  67. // class annotations
  68. if ($annot = $this->reader->getClassAnnotation($class, self::UPLOADABLE)) {
  69. $config['uploadable'] = true;
  70. $config['allowOverwrite'] = $annot->allowOverwrite;
  71. $config['appendNumber'] = $annot->appendNumber;
  72. $config['path'] = $annot->path;
  73. $config['pathMethod'] = '';
  74. $config['fileMimeTypeField'] = false;
  75. $config['filePathField'] = false;
  76. $config['fileSizeField'] = false;
  77. foreach ($class->getProperties() as $prop) {
  78. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {
  79. $config['fileMimeTypeField'] = $prop->getName();
  80. Validator::validateFileMimeTypeField($meta, $config['fileMimeTypeField']);
  81. }
  82. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_PATH)) {
  83. $config['filePathField'] = $prop->getName();
  84. Validator::validateFilePathField($meta, $config['filePathField']);
  85. }
  86. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_SIZE)) {
  87. $config['fileSizeField'] = $prop->getName();
  88. Validator::validateFileSizeField($meta, $config['fileSizeField']);
  89. }
  90. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_INFO)) {
  91. $config['fileInfoField'] = $prop->getName();
  92. }
  93. }
  94. if (!$config['filePathField']) {
  95. throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFilePath field.',
  96. $class->getName()
  97. ));
  98. }
  99. if (!$config['fileInfoField']) {
  100. throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFileInfo field.',
  101. $class->getName()
  102. ));
  103. }
  104. foreach ($class->getMethods() as $method) {
  105. if ($this->reader->getMethodAnnotation($method, self::UPLOADABLE_PATH)) {
  106. $config['pathMethod'] = $method->getName();
  107. }
  108. }
  109. if ($config['path'] && $config['pathMethod'] === '') {
  110. $msg = 'You need to define the path in the %s annotation, or add a method with %s annotation.';
  111. throw new InvalidMappingException(sprintf($msg,
  112. self::UPLOADABLE,
  113. self::UPLOADABLE_PATH
  114. ));
  115. }
  116. } else {
  117. // We need to check if this class has a relation with Uploadable entities
  118. $associations = $meta->getAssociationMappings();
  119. foreach ($associations as $field => $association) {
  120. $refl = new \ReflectionClass($association['targetEntity']);
  121. if ($annot = $this->reader->getClassAnnotation($refl, self::UPLOADABLE)) {
  122. $config['hasUploadables'] = true;
  123. if (!isset($config['uploadables'])) {
  124. $config['uploadables'] = array();
  125. }
  126. $config['uploadables'][] = array(
  127. 'class' => $association['targetEntity'],
  128. 'property' => $association['fieldName']
  129. );
  130. }
  131. }
  132. }
  133. $this->validateFullMetadata($meta, $config);
  134. }
  135. /**
  136. * Passes in the mapping read by original driver
  137. *
  138. * @param $driver
  139. * @return void
  140. */
  141. public function setOriginalDriver($driver)
  142. {
  143. $this->_originalDriver = $driver;
  144. }
  145. }