Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring/listener #21

Merged
merged 8 commits into from
Feb 15, 2013
Merged
Show file tree
Hide file tree
Changes from 4 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
71 changes: 71 additions & 0 deletions src/Knp/DoctrineBehaviors/ORM/AbstractListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* This file is part of the KnpDoctrineBehaviors package.
*
* (c) KnpLabs <http://knplabs.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Knp\DoctrineBehaviors\ORM;

use Doctrine\Common\EventSubscriber;

abstract class AbstractListener implements EventSubscriber
{

abstract public function getSubscribedEvents();

/**
* Return TRUE if the given entity use the given trait, FALSE if not
* @param ReflectionClass $class
* @param string $traitName
* @param boolean $isRecursive
*/
protected function isEntityUseTrait(\ReflectionClass $class, $traitName, $isRecursive = false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isEntityUsingTrait makes more sense, doesn't it?

Anyway, isn't the goal of this PR to abstract wether this listener applies on Document or Entity?
In this case, this method could be called isObjectUsingTrait

Last thing, as I believe these 3 methods will be usefull in many other places, you might want to move it to its own class and inject it in the listener (something like Knp\DoctrineBehaviors\Reflection\ClassAnalyzer)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmmm ok....
About the method name, the goal of DoctrineBehaviors is to add features to Doctrine Entities. Is that a real problem if methods names talk about entities.

The class Analyser is a good idea.

{
if (in_array($traitName, $class->getTraitNames())) {
return true;
}

$parentClass = $class->getParentClass();

if ((false === $isRecursive) || (false === $parentClass)) {
return false;
}

return $this->isEntityUseTrait($parentClass, $traitName, $isRecursive);
}

/**
* Return TRUE if the given entity has the given method, FALSE if not
* @param ReflectionClass $class
* @param string $methodName
*/
protected function isEntityHasMethod(\ReflectionClass $class, $methodName)
{
return $class->hasMethod($methodName);
}

/**
* Return TRUE if the given entity has the given property, FALSE if not
* @param ReflectionClass $class
* @param string $propertyName
*/
protected function isEntityHasProperty(\ReflectionClass $class, $propertyName, $isRecursive = false)
{
if ($class->hasProperty($propertyName)) {
return true;
}

$parentClass = $class->getParentClass();

if ((false === $isRecursive) || (false === $parentClass)) {
return false;
}

return $this->isEntityHasProperty($parentClass, $propertyName, $isRecursive);
}
}
15 changes: 5 additions & 10 deletions src/Knp/DoctrineBehaviors/ORM/Blameable/BlameableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Knp\DoctrineBehaviors\ORM\Blameable;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Event\LoadClassMetadataEventArgs;
use Doctrine\ORM\Event\LifecycleEventArgs;
Expand All @@ -24,7 +26,7 @@
* Adds class metadata depending of user type (entity or string)
* Listens to prePersist and PreUpdate lifecycle events
*/
class BlameableListener implements EventSubscriber
class BlameableListener extends AbstractListener
{
/**
* @var callable
Expand Down Expand Up @@ -237,16 +239,9 @@ public function getUser()
*/
private function isEntitySupported(\ReflectionClass $reflClass, $isRecursive = false)
{
$isSupported = in_array('Knp\DoctrineBehaviors\Model\Blameable\Blameable', $reflClass->getTraitNames())
|| in_array('Knp\DoctrineBehaviors\Model\Blameable\BlameableMethods', $reflClass->getTraitNames())
return $this->isEntityUseTrait($reflClass, 'Knp\DoctrineBehaviors\Model\Blameable\Blameable', $isRecursive)
|| $this->isEntityUseTrait($reflClass, 'Knp\DoctrineBehaviors\Model\Blameable\BlameableMethods', $isRecursive)
;

while ($isRecursive and !$isSupported and $reflClass->getParentClass()) {
$reflClass = $reflClass->getParentClass();
$isSupported = $this->isEntitySupported($reflClass, true);
}

return $isSupported;
}

public function getSubscribedEvents()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Knp\DoctrineBehaviors\ORM\Geocodable;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Knp\DoctrineBehaviors\ORM\Geocodable\Type\Point;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
Expand All @@ -27,7 +29,7 @@
* GeocodableListener handle Geocodable entites
* Adds doctrine point type
*/
class GeocodableListener implements EventSubscriber
class GeocodableListener extends AbstractListener
{
/**
* @var callable
Expand Down Expand Up @@ -140,7 +142,7 @@ public function getLocation($entity)
*/
private function isEntitySupported(\ReflectionClass $reflClass)
{
return $reflClass->hasMethod('getLocation');
return $this->isEntityHasMethod($reflClass, 'getLocation');
}

public function getSubscribedEvents()
Expand Down
6 changes: 4 additions & 2 deletions src/Knp/DoctrineBehaviors/ORM/Loggable/LoggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Knp\DoctrineBehaviors\ORM\Loggable;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\ORM\Event\LifecycleEventArgs;

use Doctrine\Common\EventSubscriber,
Expand All @@ -21,7 +23,7 @@
* LoggableListener handle Loggable entites
* Listens to lifecycle events
*/
class LoggableListener implements EventSubscriber
class LoggableListener extends AbstractListener
{
/**
* @var callable
Expand Down Expand Up @@ -96,7 +98,7 @@ public function setLoggerCallable(callable $callable)
*/
private function isEntitySupported(\ReflectionClass $reflClass)
{
return in_array('Knp\DoctrineBehaviors\Model\Loggable\Loggable', $reflClass->getTraitNames());
return $this->isEntityUseTrait($reflClass, 'Knp\DoctrineBehaviors\Model\Loggable\Loggable');
}

public function getSubscribedEvents()
Expand Down
10 changes: 5 additions & 5 deletions src/Knp/DoctrineBehaviors/ORM/Sluggable/SluggableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Knp\DoctrineBehaviors\ORM\Sluggable;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs,
Doctrine\Common\EventSubscriber,
Doctrine\ORM\Events,
Expand All @@ -16,7 +18,7 @@
*
* Adds mapping to sluggable entities.
*/
class SluggableListener implements EventSubscriber
class SluggableListener extends AbstractListener
{
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
Expand Down Expand Up @@ -48,10 +50,8 @@ public function getSubscribedEvents()
*
* @return Boolean
*/
private function isEntitySupported(ClassMetadata $classMetadata)
protected function isEntitySupported(ClassMetadata $classMetadata)
{
$traitNames = $classMetadata->reflClass->getTraitNames();

return in_array('Knp\DoctrineBehaviors\Model\Sluggable\Sluggable', $traitNames);
return $this->isEntityUseTrait($classMetadata->reflClass, 'Knp\DoctrineBehaviors\Model\Sluggable\Sluggable');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Knp\DoctrineBehaviors\ORM\SoftDeletable;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\Common\Persistence\Mapping\ClassMetadata,
Doctrine\Common\EventSubscriber,
Doctrine\ORM\Event\OnFlushEventArgs,
Expand All @@ -22,7 +24,7 @@
* Listens to onFlush event and marks SoftDeletable entities
* as deleted instead of really removing them.
*/
class SoftDeletableListener implements EventSubscriber
class SoftDeletableListener extends AbstractListener
{
/**
* Listens to onFlush event.
Expand Down Expand Up @@ -59,9 +61,7 @@ public function onFlush(OnFlushEventArgs $args)
*/
private function isEntitySupported(ClassMetadata $classMetadata)
{
$traitNames = $classMetadata->reflClass->getTraitNames();

return in_array('Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletable', $traitNames);
return $this->isEntityUseTrait($classMetadata->reflClass, 'Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletable');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Knp\DoctrineBehaviors\ORM\Timestampable;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs,
Doctrine\Common\EventSubscriber,
Doctrine\ORM\Events,
Expand All @@ -21,7 +23,7 @@
*
* Adds mapping to the timestampable entites.
*/
class TimestampableListener implements EventSubscriber
class TimestampableListener extends AbstractListener
{
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
Expand Down Expand Up @@ -53,8 +55,6 @@ public function getSubscribedEvents()
*/
private function isEntitySupported(ClassMetadata $classMetadata)
{
$traitNames = $classMetadata->reflClass->getTraitNames();

return in_array('Knp\DoctrineBehaviors\Model\Timestampable\Timestampable', $traitNames);
return $this->isEntityUseTrait($classMetadata->reflClass, 'Knp\DoctrineBehaviors\Model\Timestampable\Timestampable');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Knp\DoctrineBehaviors\ORM\Translatable;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\Common\EventSubscriber,
Doctrine\ORM\Mapping\ClassMetadata,
Doctrine\ORM\Event\LoadClassMetadataEventArgs,
Expand All @@ -22,7 +24,7 @@
*
* Provides mapping for translatable entities and their translations.
*/
class TranslatableListener implements EventSubscriber
class TranslatableListener extends AbstractListener
{
private $currentLocaleCallable;

Expand All @@ -44,7 +46,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
return;
}

if ($this->isTranslatable($classMetadata->reflClass)) {
if ($this->isTranslatable($classMetadata)) {
$this->mapTranslatable($classMetadata);
}

Expand Down Expand Up @@ -113,16 +115,9 @@ private function hasUniqueTranslationConstraint(ClassMetadata $classMetadata, $n
*
* @return boolean
*/
private function isTranslatable(\ReflectionClass $reflClass, $isRecursive = false)
private function isTranslatable(ClassMetadata $classMetadata, $isRecursive = false)
{
$isSupported = $reflClass->hasProperty('translations');

while ($isRecursive and !$isSupported and $reflClass->getParentClass()) {
$reflClass = $reflClass->getParentClass();
$isSupported = $this->isTranslatable($reflClass, true);
}

return $isSupported;
return $this->isEntityHasProperty($classMetadata->reflClass, 'translations', $isRecursive);
}

private function isTranslation(ClassMetadata $classMetadata)
Expand All @@ -136,7 +131,7 @@ public function postLoad(LifecycleEventArgs $eventArgs)
$entity = $eventArgs->getEntity();
$classMetadata = $em->getClassMetadata(get_class($entity));

if (!$classMetadata->reflClass->hasMethod('setCurrentLocale')) {
if (!$this->isEntityHasMethod($classMetadata->reflClass, 'setCurrentLocale', false)) {
return;
}

Expand Down