Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions Doctrine/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Snowcap\ImBundle\Doctrine\Driver;

use Metadata\Driver\DriverInterface;
use Metadata\MergeableClassMetadata;
use Doctrine\Common\Annotations\Reader;
use Snowcap\ImBundle\Doctrine\Metadata\MogrifyMetadata;
use Snowcap\ImBundle\Doctrine\Metadata\ConvertMetadata;

class AnnotationDriver implements DriverInterface
{
const MOGRIFY = 'Snowcap\ImBundle\Doctrine\Mapping\Mogrify';

const CONVERT = 'Snowcap\ImBundle\Doctrine\Mapping\Convert';

const CONVERT_MULTIPLE = 'Snowcap\ImBundle\Doctrine\Mapping\ConvertMultiple';

private $reader;

public function __construct(Reader $reader)
{
$this->reader = $reader;
}

private function getMogrifyMetadata(\ReflectionClass $class)
{
$classMetadata = new MergeableClassMetadata($class->getName());

foreach ($class->getProperties() as $property) {
$field = $this->reader->getPropertyAnnotation($property, self::MOGRIFY);
if(!is_null($field)) {
$propertyMetadata = new MogrifyMetadata($class->getName(), $property->getName());
$propertyMetadata->params = $field->params;

$classMetadata->addPropertyMetadata($propertyMetadata);
}
}

return $classMetadata;
}

private function getConvertMetadata(\ReflectionClass $class)
{
$classMetadata = new MergeableClassMetadata($class->getName());

foreach ($class->getProperties() as $property) {
$multiple = $this->reader->getPropertyAnnotation($property, self::CONVERT_MULTIPLE);
if(!is_null($multiple)) {

$propertyMetadata = new ConvertMetadata($class->getName(), $property->getName());
foreach($multiple->value as $convert) {
$propertyMetadata->addConvert($convert->params, $convert->targetProperty);
}

} else {

$convert = $this->reader->getPropertyAnnotation($property, self::CONVERT);
if(!is_null($convert)) {
$propertyMetadata = new ConvertMetadata($class->getName(), $property->getName());
$propertyMetadata->addConvert($convert->params, $convert->targetProperty);
}

}

if(isset($propertyMetadata)) {
$classMetadata->addPropertyMetadata($propertyMetadata);
}
}

return $classMetadata;
}

public function loadMetadataForClass(\ReflectionClass $class)
{
$classMetadata = new MergeableClassMetadata($class->getName());
$classMetadata->merge($this->getMogrifyMetadata($class));
$classMetadata->merge($this->getConvertMetadata($class));

return $classMetadata;
}
}
38 changes: 0 additions & 38 deletions Doctrine/Driver/MogrifyDriver.php

This file was deleted.

40 changes: 40 additions & 0 deletions Doctrine/Mapping/Convert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Snowcap ImBundle package.
*
* (c) Snowcap <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Snowcap\ImBundle\Doctrine\Mapping;

use Doctrine\Common\Annotations\Annotation;

/**
* Annotation definition class
*
* @Annotation
* @Annotation\Target("PROPERTY")
* @codeCoverageIgnore
*/
class Convert extends Annotation
{
/**
* Identfier of a pre-configured format or key-value pairs of IM parameters
*
* @Required
* @var string|array<string>
*/
public $params;

/**
* Property the converted image file is to be injected into
*
* @Required
* @var string
*/
public $targetProperty;
}
30 changes: 30 additions & 0 deletions Doctrine/Mapping/ConvertMultiple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Snowcap ImBundle package.
*
* (c) Snowcap <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Snowcap\ImBundle\Doctrine\Mapping;

use Doctrine\Common\Annotations\Annotation;

/**
* Annotation definition class
*
* @Annotation
* @Annotation\Target("PROPERTY")
* @codeCoverageIgnore
*/
class ConvertMultiple extends Annotation
{
/**
* @Required
* @var array<\Snowcap\ImBundle\Doctrine\Mapping\Convert>
*/
public $value;
}
43 changes: 43 additions & 0 deletions Doctrine/Metadata/ConvertMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Snowcap\ImBundle\Doctrine\Metadata;

use Metadata\PropertyMetadata as BasePropertyMetadata;

class ConvertMetadata extends BasePropertyMetadata
{
/**
* @var array
*/
private $converts;

public function addConvert($params, $targetProperty)
{
$convert = new \stdClass;
$convert->params = $params;
$convert->targetProperty = $targetProperty;
$this->converts[] = $convert;
}

public function getConverts()
{
return $this->converts;
}

public function serialize()
{
return serialize(array(
$this->class,
$this->name,
$this->converts,
));
}

public function unserialize($str)
{
list($this->class, $this->name, $this->converts) = unserialize($str);

$this->reflection = new \ReflectionProperty($this->class, $this->name);
$this->reflection->setAccessible(true);
}
}
112 changes: 112 additions & 0 deletions Listener/ConvertSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/*
* This file is part of the Snowcap ImBundle package.
*
* (c) Snowcap <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Snowcap\ImBundle\Listener;

use Doctrine\Common\EventSubscriber;
use Metadata\MetadataFactoryInterface;
use Snowcap\ImBundle\Manager as ImManager;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Snowcap\ImBundle\Doctrine\Metadata\ConvertMetadata;

/**
* Event listener for Doctrine entities to evualuate and execute Convert and ConvertMultiple annotations
*/
class ConvertSubscriber implements EventSubscriber
{
/**
* @var \Metadata\MetadataFactoryInterface
*/
private $metadataFactory;

/**
* @var \Snowcap\ImBundle\Manager
*/
private $imManager;

/**
* @var \Symfony\Component\PropertyAccess\PropertyAccessor
*/
private $propertyAccessor;

/**
* @param MetadataFactoryInterface $metadataFactory
* @param ImManager $imManager The ImBundle manager instance
*/
public function __construct(MetadataFactoryInterface $metadataFactory, ImManager $imManager)
{
$this->metadataFactory = $metadataFactory;
$this->imManager = $imManager;
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
}

/**
* Returns an array of events this subscriber wants to listen to.
*
* @return array
*/
public function getSubscribedEvents()
{
return array('prePersist', 'preFlush');
}

private function recomputeChangeSet($event)
{
$object = $event->getEntity();

$em = $event->getEntityManager();
$uow = $em->getUnitOfWork();
$metadata = $em->getClassMetadata(get_class($object));
$uow->recomputeSingleEntityChangeSet($metadata, $object);

return $this;
}

/**
* @param PreFlushEventArgs $ea
*/
public function preFlush(PreFlushEventArgs $ea)
{

}

/**
* @param LifecycleEventArgs $ea
*/
public function prePersist(LifecycleEventArgs $ea)
{
$entity = $ea->getEntity();
foreach ($this->metadataFactory->getMetadataForClass(get_class($entity))->propertyMetadata as $propertyMetadata) {
if($propertyMetadata instanceof ConvertMetadata) {
$this->convert($entity, $propertyMetadata);
}
}

}

/**
* @param $entity
* @param $propertyMetadata
*/
private function convert($entity, $propertyMetadata)
{
$file = $this->propertyAccessor->getValue($entity, $propertyMetadata->name);
if ($file instanceof \SplFileInfo) {
foreach ($propertyMetadata->getConverts() as $convert) {
// XXX: convert() only takes paths relative to web root, currently
$this->imManager->convert($convert->params, $file->getPathName());
}
}
return null;
}
}
Loading