Annotation.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\AbstractAnnotationDriver,
  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 extends AbstractAnnotationDriver
  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. * {@inheritDoc}
  31. */
  32. public function readExtendedMetadata($meta, array &$config)
  33. {
  34. $class = $this->getMetaReflectionClass($meta);
  35. // class annotations
  36. if ($annot = $this->reader->getClassAnnotation($class, self::UPLOADABLE)) {
  37. $config['uploadable'] = true;
  38. $config['allowOverwrite'] = $annot->allowOverwrite;
  39. $config['appendNumber'] = $annot->appendNumber;
  40. $config['path'] = $annot->path;
  41. $config['pathMethod'] = $annot->pathMethod;
  42. $config['fileMimeTypeField'] = false;
  43. $config['filePathField'] = false;
  44. $config['fileSizeField'] = false;
  45. $config['callback'] = $annot->callback;
  46. $config['filenameGenerator'] = $annot->filenameGenerator;
  47. $config['maxSize'] = (double) $annot->maxSize;
  48. $config['allowedTypes'] = $annot->allowedTypes;
  49. $config['disallowedTypes'] = $annot->disallowedTypes;
  50. foreach ($class->getProperties() as $prop) {
  51. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {
  52. $config['fileMimeTypeField'] = $prop->getName();
  53. }
  54. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_PATH)) {
  55. $config['filePathField'] = $prop->getName();
  56. }
  57. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_SIZE)) {
  58. $config['fileSizeField'] = $prop->getName();
  59. }
  60. }
  61. Validator::validateConfiguration($meta, $config);
  62. }
  63. /*
  64. // Code in case we need to identify entities which are not Uploadables, but have associations
  65. // with other Uploadable entities
  66. } else {
  67. // We need to check if this class has a relation with Uploadable entities
  68. $associations = $meta->getAssociationMappings();
  69. foreach ($associations as $field => $association) {
  70. $refl = new \ReflectionClass($association['targetEntity']);
  71. if ($annot = $this->reader->getClassAnnotation($refl, self::UPLOADABLE)) {
  72. $config['hasUploadables'] = true;
  73. if (!isset($config['uploadables'])) {
  74. $config['uploadables'] = array();
  75. }
  76. $config['uploadables'][] = array(
  77. 'class' => $association['targetEntity'],
  78. 'property' => $association['fieldName']
  79. );
  80. }
  81. }
  82. }*/
  83. $this->validateFullMetadata($meta, $config);
  84. }
  85. }