Annotation.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Annotation extends AbstractAnnotationDriver
  18. {
  19. /**
  20. * Annotation to define that this object is loggable
  21. */
  22. const UPLOADABLE = 'Gedmo\\Mapping\\Annotation\\Uploadable';
  23. const UPLOADABLE_FILE_MIME_TYPE = 'Gedmo\\Mapping\\Annotation\\UploadableFileMimeType';
  24. const UPLOADABLE_FILE_PATH = 'Gedmo\\Mapping\\Annotation\\UploadableFilePath';
  25. const UPLOADABLE_FILE_SIZE = 'Gedmo\\Mapping\\Annotation\\UploadableFileSize';
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function readExtendedMetadata($meta, array &$config)
  30. {
  31. $class = $this->getMetaReflectionClass($meta);
  32. // class annotations
  33. if ($annot = $this->reader->getClassAnnotation($class, self::UPLOADABLE)) {
  34. $config['uploadable'] = true;
  35. $config['allowOverwrite'] = $annot->allowOverwrite;
  36. $config['appendNumber'] = $annot->appendNumber;
  37. $config['path'] = $annot->path;
  38. $config['pathMethod'] = $annot->pathMethod;
  39. $config['fileMimeTypeField'] = false;
  40. $config['filePathField'] = false;
  41. $config['fileSizeField'] = false;
  42. $config['callback'] = $annot->callback;
  43. $config['filenameGenerator'] = $annot->filenameGenerator;
  44. $config['maxSize'] = (double) $annot->maxSize;
  45. $config['allowedTypes'] = $annot->allowedTypes;
  46. $config['disallowedTypes'] = $annot->disallowedTypes;
  47. foreach ($class->getProperties() as $prop) {
  48. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {
  49. $config['fileMimeTypeField'] = $prop->getName();
  50. }
  51. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_PATH)) {
  52. $config['filePathField'] = $prop->getName();
  53. }
  54. if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_SIZE)) {
  55. $config['fileSizeField'] = $prop->getName();
  56. }
  57. }
  58. Validator::validateConfiguration($meta, $config);
  59. }
  60. /*
  61. // Code in case we need to identify entities which are not Uploadables, but have associations
  62. // with other Uploadable entities
  63. } else {
  64. // We need to check if this class has a relation with Uploadable entities
  65. $associations = $meta->getAssociationMappings();
  66. foreach ($associations as $field => $association) {
  67. $refl = new \ReflectionClass($association['targetEntity']);
  68. if ($annot = $this->reader->getClassAnnotation($refl, self::UPLOADABLE)) {
  69. $config['hasUploadables'] = true;
  70. if (!isset($config['uploadables'])) {
  71. $config['uploadables'] = array();
  72. }
  73. $config['uploadables'][] = array(
  74. 'class' => $association['targetEntity'],
  75. 'property' => $association['fieldName']
  76. );
  77. }
  78. }
  79. }*/
  80. $this->validateFullMetadata($meta, $config);
  81. }
  82. }