forked from dustin10/VichUploaderBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
222 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,14 @@ | |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
use Vich\UploaderBundle\Exception\MappingNotFoundException; | ||
|
||
/** | ||
* Register the uploadable models in BazingaPropelEventDispatcherBundle | ||
* | ||
* @author Kévin Gomez <[email protected]> | ||
* @author Konstantin Myakshin <[email protected]> | ||
*/ | ||
class RegisterPropelModelsPass implements CompilerPassInterface | ||
{ | ||
|
@@ -23,9 +25,7 @@ public function process(ContainerBuilder $container) | |
return; | ||
} | ||
|
||
$serviceTypes = array( | ||
'inject', 'clean', 'remove', 'upload', | ||
); | ||
$serviceTypes = array('inject', 'clean', 'remove', 'upload',); | ||
|
||
$metadata = $container->get('vich_uploader.metadata_reader'); | ||
$mappings = $container->getParameter('vich_uploader.mappings'); | ||
|
@@ -43,11 +43,12 @@ public function process(ContainerBuilder $container) | |
} | ||
|
||
foreach ($serviceTypes as $type) { | ||
if (!$container->has(sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping']))) { | ||
continue; | ||
} | ||
$listenerId = sprintf('vich_uploader.listener.%s.propel.%s', $type, $field['mapping']); | ||
|
||
$definition = $container | ||
->setDefinition($listenerId, new DefinitionDecorator(sprintf('vich_uploader.listener.%s.propel', $type))) | ||
->replaceArgument(1, new Reference('vich_uploader.adapter.propel')); | ||
|
||
$definition = $container->getDefinition(sprintf('vich_uploader.listener.%s.%s', $type, $field['mapping'])); | ||
$definition->setClass($container->getDefinition($definition->getParent())->getClass()); | ||
$definition->setPublic(true); | ||
$definition->addTag('propel.event_subscriber', array('class' => $class)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Vich\UploaderBundle\EventListener; | ||
|
||
use Vich\UploaderBundle\Adapter\AdapterInterface; | ||
use Vich\UploaderBundle\Handler\UploadHandler; | ||
use Vich\UploaderBundle\Metadata\MetadataReader; | ||
|
||
/** | ||
* BaseListener | ||
* | ||
* @author Konstantin Myakshin <[email protected]> | ||
*/ | ||
abstract class BaseListener | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $mappings; | ||
|
||
/** | ||
* @var AdapterInterface $adapter | ||
*/ | ||
protected $adapter; | ||
|
||
/** | ||
* @var MetadataReader $metadata | ||
*/ | ||
protected $metadata; | ||
|
||
/** | ||
* @var UploadHandler $handler | ||
*/ | ||
protected $handler; | ||
|
||
/** | ||
* Constructs a new instance of listener. | ||
* | ||
* @param array $mappings The mappings configuration. | ||
* @param AdapterInterface $adapter The adapter. | ||
* @param MetadataReader $metadata The metadata reader. | ||
* @param UploadHandler $handler The upload handler. | ||
*/ | ||
public function __construct($mappings, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) | ||
{ | ||
$this->mappings = $mappings; | ||
$this->adapter = $adapter; | ||
$this->metadata = $metadata; | ||
$this->handler = $handler; | ||
} | ||
|
||
protected function isFlagEnabledForField($flag, array $field) | ||
{ | ||
return true === $this->mappings[$field['mapping']][$flag]; | ||
} | ||
|
||
public function getUploadableFields($object) | ||
{ | ||
return $this->metadata->getUploadableFields(ClassUtils::getClass($object)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,80 +3,14 @@ | |
namespace Vich\UploaderBundle\EventListener\Doctrine; | ||
|
||
use Doctrine\Common\EventSubscriber; | ||
|
||
use Vich\UploaderBundle\Adapter\AdapterInterface; | ||
use Vich\UploaderBundle\Handler\UploadHandler; | ||
use Vich\UploaderBundle\Metadata\MetadataReader; | ||
use Vich\UploaderBundle\Util\ClassUtils; | ||
use Vich\UploaderBundle\EventListener\BaseListener as CommonListener; | ||
|
||
/** | ||
* BaseListener | ||
* | ||
* @author Kévin Gomez <[email protected]> | ||
* @author Konstantin Myakshin <[email protected]> | ||
*/ | ||
abstract class BaseListener implements EventSubscriber | ||
abstract class BaseListener extends CommonListener implements EventSubscriber | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $mapping; | ||
|
||
/** | ||
* @var AdapterInterface $adapter | ||
*/ | ||
protected $adapter; | ||
|
||
/** | ||
* @var MetadataReader $metadata | ||
*/ | ||
protected $metadata; | ||
|
||
/** | ||
* @var UploadHandler $handler | ||
*/ | ||
protected $handler; | ||
|
||
/** | ||
* Constructs a new instance of UploaderListener. | ||
* | ||
* @param string $mapping The mapping name. | ||
* @param AdapterInterface $adapter The adapter. | ||
* @param MetadataReader $metadata The metadata reader. | ||
* @param UploadHandler $handler The upload handler. | ||
*/ | ||
public function __construct($mapping, AdapterInterface $adapter, MetadataReader $metadata, UploadHandler $handler) | ||
{ | ||
$this->mapping = $mapping; | ||
$this->adapter = $adapter; | ||
$this->metadata = $metadata; | ||
$this->handler = $handler; | ||
} | ||
|
||
/** | ||
* Checks if the given object is uploadable using the current mapping. | ||
* | ||
* @param mixed $object The object to test. | ||
* | ||
* @return bool | ||
*/ | ||
protected function isUploadable($object) | ||
{ | ||
return $this->metadata->isUploadable(ClassUtils::getClass($object), $this->mapping); | ||
} | ||
|
||
/** | ||
* Returns a list of uploadable fields for the given object and mapping. | ||
* | ||
* @param mixed $object The object to use. | ||
* | ||
* @return array<string> A list of field names. | ||
*/ | ||
protected function getUploadableFields($object) | ||
{ | ||
$fields = $this->metadata->getUploadableFields(ClassUtils::getClass($object), $this->mapping); | ||
|
||
return array_map(function($data) { | ||
return $data['propertyName']; | ||
}, $fields); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,43 +2,47 @@ | |
|
||
namespace Vich\UploaderBundle\EventListener\Doctrine; | ||
|
||
use Doctrine\Common\EventArgs; | ||
use Doctrine\ORM\Event\PreFlushEventArgs; | ||
use Doctrine\ORM\Events; | ||
|
||
/** | ||
* CleanListener | ||
* | ||
* Listen to the update event to delete old files accordingly. | ||
* | ||
* @author Konstantin Myakshin <[email protected]> | ||
* @author Kévin Gomez <[email protected]> | ||
*/ | ||
class CleanListener extends BaseListener | ||
{ | ||
/** | ||
* The events the listener is subscribed to. | ||
* | ||
* @return array The array of events. | ||
* {@inheritdoc} | ||
*/ | ||
public function getSubscribedEvents() | ||
{ | ||
return array( | ||
'preUpdate', | ||
); | ||
return array(Events::preFlush); | ||
} | ||
|
||
/** | ||
* @param EventArgs $event The event. | ||
* @param PreFlushEventArgs $event | ||
*/ | ||
public function preUpdate(EventArgs $event) | ||
public function preFlush(PreFlushEventArgs $event) | ||
{ | ||
$object = $this->adapter->getObjectFromArgs($event); | ||
$em = $event->getEntityManager(); | ||
$identityMap = $em->getUnitOfWork()->getIdentityMap(); | ||
|
||
if (!$this->isUploadable($object)) { | ||
return; | ||
} | ||
foreach ($identityMap as $class => $entities) { | ||
$fields = $this->metadata->getUploadableFields($class); | ||
|
||
foreach ($fields as $field) { | ||
if (!$this->isFlagEnabledForField('delete_on_update', $field)) { | ||
continue; | ||
} | ||
|
||
foreach ($this->getUploadableFields($object) as $field) { | ||
$this->handler->clean($object, $field); | ||
$this->adapter->recomputeChangeSet($event); | ||
foreach ($entities as $entity) { | ||
$this->handler->clean($entity, $field['propertyName']); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,41 +3,39 @@ | |
namespace Vich\UploaderBundle\EventListener\Doctrine; | ||
|
||
use Doctrine\Common\EventArgs; | ||
use Doctrine\ORM\Events; | ||
|
||
/** | ||
* InjectListener | ||
* | ||
* Listen to the load event in order to inject File objects. | ||
* | ||
* @author Konstantin Myakshin <[email protected]> | ||
* @author Kévin Gomez <[email protected]> | ||
*/ | ||
class InjectListener extends BaseListener | ||
{ | ||
/** | ||
* The events the listener is subscribed to. | ||
* | ||
* @return array The array of events. | ||
* {@inheritdoc} | ||
*/ | ||
public function getSubscribedEvents() | ||
{ | ||
return array( | ||
'postLoad', | ||
); | ||
return array(Events::postLoad); | ||
} | ||
|
||
/** | ||
* @param EventArgs $event The event. | ||
* @param LifecycleEventArgs $event | ||
*/ | ||
public function postLoad(EventArgs $event) | ||
public function postLoad(LifecycleEventArgs $event) | ||
{ | ||
$object = $this->adapter->getObjectFromArgs($event); | ||
|
||
if (!$this->isUploadable($object)) { | ||
return; | ||
} | ||
|
||
foreach ($this->getUploadableFields($object) as $field) { | ||
$this->handler->inject($object, $field); | ||
if (!$this->isFlagEnabledForField('inject_on_load', $field)) { | ||
continue; | ||
} | ||
|
||
$this->handler->inject($object, $field['propertyName']); | ||
} | ||
} | ||
} |
Oops, something went wrong.