コード例 #1
0
 /**
  * {@inheritDoc}
  */
 public function injectFile($obj, PropertyMapping $mapping)
 {
     $path = $this->storage->resolvePath($obj, $mapping->getFilePropertyName());
     if ($path !== null) {
         $mapping->setFile($obj, new File($path, false));
     }
 }
コード例 #2
0
ファイル: UploadHandler.php プロジェクト: jbouhier/2SN
 public function remove($obj, $mapping)
 {
     $mapping = $this->factory->fromName($obj, $mapping);
     $this->dispatch(Events::PRE_REMOVE, new Event($obj, $mapping));
     $this->storage->remove($obj, $mapping);
     $mapping->setFileName($obj, null);
     $this->dispatch(Events::POST_REMOVE, new Event($obj, $mapping));
 }
コード例 #3
0
 /**
  * {@inheritDoc}
  */
 public function injectFiles($obj)
 {
     $mappings = $this->factory->fromObject($obj);
     foreach ($mappings as $mapping) {
         if ($mapping->getInjectOnLoad()) {
             $field = $mapping->getProperty()->getName();
             try {
                 $path = $this->storage->resolvePath($obj, $field);
             } catch (\InvalidArgumentException $e) {
                 continue;
             }
             $mapping->getProperty()->setValue($obj, new File($path, false));
         }
     }
 }
コード例 #4
0
 /**
  * Removes the file if necessary.
  *
  * @param EventArgs $args The event arguments.
  */
 public function postRemove(EventArgs $args)
 {
     $obj = $this->adapter->getObjectFromArgs($args);
     if ($this->isUploadable($obj)) {
         $this->storage->remove($obj);
     }
 }
コード例 #5
0
 public function remove($obj, $fieldName)
 {
     $mapping = $this->getMapping($obj, $fieldName);
     $this->dispatch(Events::PRE_REMOVE, new Event($obj, $mapping));
     $this->storage->remove($obj, $mapping);
     $mapping->setFileName($obj, null);
     $this->dispatch(Events::POST_REMOVE, new Event($obj, $mapping));
 }
コード例 #6
0
 public function addEncodedResumeProperty(TransformEvent $event)
 {
     /** @var Document $document */
     $document = $event->getDocument();
     $resume = $event->getObject();
     if (!$resume) {
         return;
     }
     if (!$resume instanceof StudentResume) {
         return;
     }
     try {
         $filePath = $this->storage->resolveUri($resume, "file");
         if ($resume->getFileName()) {
             $document->set('encodedFile', base64_encode(file_get_contents($filePath)));
         } else {
             $document->set('encodedFile', '');
         }
     } catch (\Exception $ex) {
     }
 }
コード例 #7
0
 /**
  * Test that postRemove skips non uploadable entity.
  */
 public function testPostRemoveSkipsNonUploadable()
 {
     $obj = new DummyEntity();
     $class = new \ReflectionClass($obj);
     $args = $this->getMockBuilder('Doctrine\\Common\\EventArgs')->disableOriginalConstructor()->getMock();
     $this->adapter->expects($this->once())->method('getObjectFromArgs')->will($this->returnValue($obj));
     $this->adapter->expects($this->once())->method('getReflectionClass')->will($this->returnValue($class));
     $this->driver->expects($this->once())->method('readUploadable')->with($class)->will($this->returnValue(null));
     $this->storage->expects($this->never())->method('remove');
     $listener = new UploaderListener($this->adapter, $this->driver, $this->storage, $this->injector);
     $listener->postRemove($args);
 }
 /**
  * On pre serialize
  *
  * @param ObjectEvent $event Event
  */
 public function onPreSerialize(ObjectEvent $event)
 {
     $object = $event->getObject();
     if ($object instanceof Proxy && !$object->__isInitialized()) {
         $object->__load();
     }
     $objectUid = spl_object_hash($object);
     if (in_array($objectUid, $this->serializedObjects)) {
         return;
     }
     $classAnnotation = $this->annotationReader->getClassAnnotation(new \ReflectionClass(ClassUtils::getClass($object)), VichSerializableClass::class);
     if ($classAnnotation instanceof VichSerializableClass) {
         $reflectionClass = ClassUtils::newReflectionClass(get_class($object));
         $this->logger->debug(sprintf('Found @VichSerializableClass annotation for the class "%s"', $reflectionClass->getName()));
         foreach ($reflectionClass->getProperties() as $property) {
             $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class);
             if ($vichSerializableAnnotation instanceof VichSerializableField) {
                 $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class);
                 if ($vichUploadableFileAnnotation instanceof UploadableField) {
                     throw new IncompatibleUploadableAndSerializableFieldAnnotationException(sprintf('The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.', $property->getName(), $reflectionClass->getName()));
                 }
                 $this->logger->debug(sprintf('Found @VichSerializableField annotation for the field "%s" in the class "%s"', $property->getName(), $reflectionClass->getName()));
                 $uri = null;
                 $property->setAccessible(true);
                 if ($property->getValue($event->getObject())) {
                     $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField());
                     if ($vichSerializableAnnotation->isIncludeHost()) {
                         $uri = $this->getHostUrl() . $uri;
                     }
                 }
                 $property->setValue($object, $uri);
             }
         }
         $this->serializedObjects[$objectUid] = $objectUid;
     }
 }
コード例 #9
0
 /**
  * Gets the public path for the file associated with the
  * object.
  *
  * @param object $obj       The object.
  * @param string $fieldName The field name.
  * @param string $className The object's class. Mandatory if $obj can't be used to determine it.
  *
  * @return string The public asset path.
  */
 public function asset($obj, $fieldName, $className = null)
 {
     return $this->storage->resolveUri($obj, $fieldName, $className);
 }
コード例 #10
0
 /**
  * Gets the public path for the file associated with the
  * object.
  * 
  * @param object $obj The object.
  * @param string $field The field.
  * @return string The public asset path.
  */
 public function asset($obj, $field)
 {
     $path = $this->storage->resolvePath($obj, $field);
     $index = strpos($path, $this->webDirName);
     return substr($path, $index + strlen($this->webDirName));
 }
コード例 #11
0
 /**
  * Gets the public path for the file associated with the
  * object.
  *
  * @param  object $obj   The object.
  * @param  string $field The field.
  * @return string The public asset path.
  */
 public function asset($obj, $field)
 {
     return $this->storage->resolveUri($obj, $field);
 }