Skip to content

Commit

Permalink
Merge pull request #26 from foaly-nr1/patch-cacheable
Browse files Browse the repository at this point in the history
Cache metadata
  • Loading branch information
Jérôme Poskin committed Dec 1, 2015
2 parents e0c77fa + 79c2996 commit ae12eca
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 76 deletions.
10 changes: 10 additions & 0 deletions DependencyInjection/SnowcapImExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,15 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('snowcap_im.cache_path', $config['cache_path']);
$container->setParameter('snowcap_im.timeout', $config['timeout']);
$container->setParameter('snowcap_im.binary_path', $config['binary_path']);

$metadataCache = '%kernel.cache_dir%/snowcap_im';
$metadataCache = $container->getParameterBag()->resolveValue($metadataCache);
if (!is_dir($metadataCache)) {
mkdir($metadataCache, 0777, true);
}
$container
->getDefinition('snowcap_im.metadata.cache')
->replaceArgument(0, $metadataCache)
;
}
}
38 changes: 38 additions & 0 deletions Doctrine/Driver/MogrifyDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Snowcap\ImBundle\Doctrine\Driver;

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

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

private $reader;

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

public function loadMetadataForClass(\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;
}
}
8 changes: 7 additions & 1 deletion Doctrine/Mapping/Mogrify.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
* Annotation definition class
*
* @Annotation
* @Annotation\Target("PROPERTY")
* @codeCoverageIgnore
*/
class Mogrify extends Annotation
{
/** @var array */
/**
* Identfier of a pre-configured format or key-value pairs of IM parameters
*
* @Required
* @var string|array<string>
*/
public $params;
}
30 changes: 30 additions & 0 deletions Doctrine/Metadata/MogrifyMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Snowcap\ImBundle\Doctrine\Metadata;

use Metadata\PropertyMetadata as BasePropertyMetadata;

class MogrifyMetadata extends BasePropertyMetadata
{
/**
* @var string|array<string>
*/
public $params;

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

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

$this->reflection = new \ReflectionProperty($this->class, $this->name);
$this->reflection->setAccessible(true);
}
}
98 changes: 24 additions & 74 deletions Listener/MogrifySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,43 @@

namespace Snowcap\ImBundle\Listener;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Metadata\MetadataFactoryInterface;
use Snowcap\ImBundle\Manager as ImManager;

use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Event listener for Doctrine entities to evualuate and execute ImBundle annotations
*/
class MogrifySubscriber implements EventSubscriber
{
private $config = array();

/**
* @var string
* @var \Metadata\MetadataFactoryInterface
*/
private $rootDir;
private $metadataFactory;

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

/**
* @param string $rootDir The dir to generate files
* @param ImManager $imManager The ImBundle mamager instance
* @var \Symfony\Component\PropertyAccess\PropertyAccessor
*/
public function __construct($rootDir, ImManager $imManager)
private $propertyAccessor;

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

/**
Expand All @@ -54,50 +57,20 @@ public function __construct($rootDir, ImManager $imManager)
*/
public function getSubscribedEvents()
{
return array('loadClassMetadata', 'prePersist', 'preFlush');
}

/**
* @param LoadClassMetadataEventArgs $eventArgs
*/
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
$reader = new AnnotationReader();
$meta = $eventArgs->getClassMetadata();
$reflexionClass = $meta->getReflectionClass();
if (null !== $reflexionClass) {
foreach ($reflexionClass->getProperties() as $property) {
if ($meta->isMappedSuperclass && !$property->isPrivate() ||
$meta->isInheritedField($property->name) ||
isset($meta->associationMappings[$property->name]['inherited'])
) {
continue;
}
/** @var $annotation \Snowcap\ImBundle\Doctrine\Mapping\Mogrify */
if ($annotation = $reader->getPropertyAnnotation($property, 'Snowcap\\ImBundle\\Doctrine\\Mapping\\Mogrify')) {
$field = $property->getName();
$this->config[$meta->getTableName()]['fields'][$field] = array(
'property' => $property,
'params' => $annotation->params,
);
}
}
}
return array('prePersist', 'preFlush');
}

/**
* @param PreFlushEventArgs $ea
*/
public function preFlush(PreFlushEventArgs $ea)
{
$entityManager = $ea->getEntityManager();

$unitOfWork = $entityManager->getUnitOfWork();

$unitOfWork = $ea->getEntityManager()->getUnitOfWork();
$entityMaps = $unitOfWork->getIdentityMap();

foreach ($entityMaps as $entities) {
foreach ($entities as $entity) {
foreach ($this->getFiles($entity, $ea->getEntityManager()) as $file) {
foreach ($this->metadataFactory->getMetadataForClass(get_class($entity))->propertyMetadata as $file) {
$this->mogrify($entity, $file);
}
}
Expand All @@ -110,43 +83,20 @@ public function preFlush(PreFlushEventArgs $ea)
public function prePersist(LifecycleEventArgs $ea)
{
$entity = $ea->getEntity();
foreach ($this->getFiles($entity, $ea->getEntityManager()) as $file) {
foreach ($this->metadataFactory->getMetadataForClass(get_class($entity))->propertyMetadata as $file) {
$this->mogrify($entity, $file);
}
}

/**
* @param $entity
* @param EntityManager $entityManager
* @return array
*/
private function getFiles($entity, EntityManager $entityManager)
{
$classMetaData = $entityManager->getClassMetaData(get_class($entity));
$tableName = $classMetaData->getTableName();

if (array_key_exists($tableName, $this->config)) {
return $this->config[$tableName]['fields'];
} else {
return array();
}
}

/**
* @param $entity
* @param $file
*/
private function mogrify($entity, $file)
{
$propertyName = $file['property']->name;

$getter = 'get' . ucFirst($propertyName);
if (method_exists($entity, $getter)) {
/** @var $uploadedFile \Symfony\Component\HttpFoundation\File\UploadedFile */
$uploadedFile = $entity->$getter();
if (null !== $uploadedFile) {
$this->imManager->mogrify($file['params'], $uploadedFile->getPathName());
}
$uploadedFile = $this->propertyAccessor->getValue($entity, $file->name);
if ($uploadedFile instanceof UploadedFile) {
$this->imManager->mogrify($file->params, $uploadedFile->getPathName());
}
}
}
}
20 changes: 19 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,27 @@ services:
tags:
- { name: twig.extension }

snowcap_im.metadata.mogrify_driver:
class: Snowcap\ImBundle\Doctrine\Driver\MogrifyDriver
public: false
arguments: [@annotation_reader]

snowcap_im.metadata.cache:
class: Metadata\Cache\FileCache
public: false
arguments:
- {}

snowcap_im.metadata.mogrify_factory:
class: Metadata\MetadataFactory
public: false
arguments: [@snowcap_im.metadata.mogrify_driver]
calls:
- [setCache, ["@snowcap_im.metadata.cache"]]

snowcap_im.mogrify_subscriber:
class: Snowcap\ImBundle\Listener\MogrifySubscriber
arguments: [%kernel.root_dir%, @snowcap_im.manager]
arguments: [@snowcap_im.metadata.mogrify_factory, @snowcap_im.manager]
tags:
- { name: doctrine.event_subscriber, priority: 1 }

Expand Down

0 comments on commit ae12eca

Please sign in to comment.