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 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
25 changes: 21 additions & 4 deletions config/orm-services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
parameters:
knp.doctrine_behaviors.reflection.class_analyzer.class: Knp\DoctrineBehaviors\Reflection\ClassAnalyzer
knp.doctrine_behaviors.translatable_listener.class: Knp\DoctrineBehaviors\ORM\Translatable\TranslatableListener
knp.doctrine_behaviors.translatable_listener.current_locale_callable.class: Knp\DoctrineBehaviors\ORM\Translatable\CurrentLocaleCallable
knp.doctrine_behaviors.softdeletable_listener.class: Knp\DoctrineBehaviors\ORM\SoftDeletable\SoftDeletableListener
Expand All @@ -12,10 +13,15 @@ parameters:
knp.doctrine_behaviors.sluggable_listener.class: Knp\DoctrineBehaviors\ORM\Sluggable\SluggableListener

services:
knp.doctrine_behaviors.reflection.class_analyzer:
class: "%knp.doctrine_behaviors.reflection.class_analyzer.class%"
public: false

knp.doctrine_behaviors.translatable_listener:
class: "%knp.doctrine_behaviors.translatable_listener.class%"
public: false
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "@knp.doctrine_behaviors.translatable_listener.current_locale_callable"
tags:
- { name: doctrine.event_subscriber }
Expand All @@ -29,18 +35,23 @@ services:
knp.doctrine_behaviors.softdeletable_listener:
class: "%knp.doctrine_behaviors.softdeletable_listener.class%"
public: false
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.timestampable_listener:
class: "%knp.doctrine_behaviors.timestampable_listener.class%"
public: false
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.blameable_listener:
class: "%knp.doctrine_behaviors.blameable_listener.class%"
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "@knp.doctrine_behaviors.blameable_listener.user_callable"
- "%knp.doctrine_behaviors.blameable_listener.user_entity%"
public: false
Expand All @@ -50,12 +61,14 @@ services:
knp.doctrine_behaviors.blameable_listener.user_callable:
class: "%knp.doctrine_behaviors.blameable_listener.user_callable.class%"
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "@service_container" # because of circular dep
public: false

knp.doctrine_behaviors.loggable_listener:
class: "%knp.doctrine_behaviors.loggable_listener.class%"
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "@knp.doctrine_behaviors.loggable_listener.logger_callable"
public: false
tags:
Expand All @@ -64,20 +77,24 @@ services:
knp.doctrine_behaviors.loggable_listener.logger_callable:
class: "%knp.doctrine_behaviors.loggable_listener.logger_callable.class%"
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "@logger"
public: false

knp.doctrine_behaviors.geocodable_listener:
class: "%knp.doctrine_behaviors.geocodable_listener.class%"
public: false
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
- "@?knp.doctrine_behaviors.geocodable_callable"
tags:
- { name: doctrine.event_subscriber }

knp.doctrine_behaviors.sluggable_listener:
class: "%knp.doctrine_behaviors.sluggable_listener.class%"
public: false
tags:
- { name: doctrine.event_subscriber }
class: "%knp.doctrine_behaviors.sluggable_listener.class%"
public: false
arguments:
- "@knp.doctrine_behaviors.reflection.class_analyzer"
tags:
- { name: doctrine.event_subscriber }

19 changes: 19 additions & 0 deletions src/Knp/DoctrineBehaviors/Model/SoftDeletable/SoftDeletable.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ public function isDeleted()
return true;
}

/**
* Checks whether the entity will be deleted.
*
* @return Boolean
*/
public function willBeDeleted(\DateTime $at = null)
{
if ($this->deletedAt === null) {

return false;
}
if ($at === null) {

return true;
}

return $this->deletedAt <= $at;
}

/**
* Returns date on which entity was been deleted.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Knp/DoctrineBehaviors/ORM/AbstractListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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;
use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

abstract class AbstractListener implements EventSubscriber
{

private $classAnalyser;

public function __construct(ClassAnalyzer $classAnalyser)
{
$this->classAnalyser = $classAnalyser;
}

protected function getClassAnalyzer()
{
return $this->classAnalyser;
}

abstract public function getSubscribedEvents();

}
21 changes: 10 additions & 11 deletions src/Knp/DoctrineBehaviors/ORM/Blameable/BlameableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Knp\DoctrineBehaviors\ORM\Blameable;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

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 +28,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 All @@ -47,8 +51,10 @@ class BlameableListener implements EventSubscriber
* @param callable
* @param string $userEntity
*/
public function __construct(callable $userCallable = null, $userEntity = null)
public function __construct(ClassAnalyzer $classAnalyzer, callable $userCallable = null, $userEntity = null)
{
parent::__construct($classAnalyzer);

$this->userCallable = $userCallable;
$this->userEntity = $userEntity;
}
Expand Down Expand Up @@ -237,16 +243,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->getClassAnalyzer()->isObjectUseTrait($reflClass, 'Knp\DoctrineBehaviors\Model\Blameable\Blameable', $isRecursive)
|| $this->getClassAnalyzer()->isObjectUseTrait($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
12 changes: 9 additions & 3 deletions src/Knp/DoctrineBehaviors/ORM/Geocodable/GeocodableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Knp\DoctrineBehaviors\ORM\Geocodable;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

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

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
Expand All @@ -27,7 +31,7 @@
* GeocodableListener handle Geocodable entites
* Adds doctrine point type
*/
class GeocodableListener implements EventSubscriber
class GeocodableListener extends AbstractListener
{
/**
* @var callable
Expand All @@ -39,8 +43,10 @@ class GeocodableListener implements EventSubscriber
*
* @param callable
*/
public function __construct(callable $geolocationCallable = null)
public function __construct(ClassAnalyzer $classAnalyzer, callable $geolocationCallable = null)
{
parent::__construct($classAnalyzer);

$this->geolocationCallable = $geolocationCallable;
}

Expand Down Expand Up @@ -140,7 +146,7 @@ public function getLocation($entity)
*/
private function isEntitySupported(\ReflectionClass $reflClass)
{
return $reflClass->hasMethod('getLocation');
return $this->getClassAnalyzer()->isObjectHasMethod($reflClass, 'getLocation');
}

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

namespace Knp\DoctrineBehaviors\ORM\Loggable;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\ORM\Event\LifecycleEventArgs;

use Doctrine\Common\EventSubscriber,
Expand All @@ -21,7 +25,7 @@
* LoggableListener handle Loggable entites
* Listens to lifecycle events
*/
class LoggableListener implements EventSubscriber
class LoggableListener extends AbstractListener
{
/**
* @var callable
Expand All @@ -33,8 +37,10 @@ class LoggableListener implements EventSubscriber
*
* @param callable
*/
public function __construct(callable $loggerCallable)
public function __construct(ClassAnalyzer $classAnalyzer, callable $loggerCallable)
{
parent::__construct($classAnalyzer);

$this->loggerCallable = $loggerCallable;
}

Expand Down Expand Up @@ -96,7 +102,7 @@ public function setLoggerCallable(callable $callable)
*/
private function isEntitySupported(\ReflectionClass $reflClass)
{
return in_array('Knp\DoctrineBehaviors\Model\Loggable\Loggable', $reflClass->getTraitNames());
return $this->getClassAnalyzer()->isObjectUseTrait($reflClass, 'Knp\DoctrineBehaviors\Model\Loggable\Loggable');
}

public function getSubscribedEvents()
Expand Down
12 changes: 7 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,10 @@

namespace Knp\DoctrineBehaviors\ORM\Sluggable;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs,
Doctrine\Common\EventSubscriber,
Doctrine\ORM\Events,
Expand All @@ -16,7 +20,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 +52,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->getClassAnalyzer()->isObjectUseTrait($classMetadata->reflClass, 'Knp\DoctrineBehaviors\Model\Sluggable\Sluggable');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

namespace Knp\DoctrineBehaviors\ORM\SoftDeletable;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\Common\Persistence\Mapping\ClassMetadata,
Doctrine\Common\EventSubscriber,
Doctrine\ORM\Event\OnFlushEventArgs,
Expand All @@ -22,7 +26,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 +63,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->getClassAnalyzer()->isObjectUseTrait($classMetadata->reflClass, 'Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletable');
}

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

namespace Knp\DoctrineBehaviors\ORM\Timestampable;

use Knp\DoctrineBehaviors\Reflection\ClassAnalyzer;

use Knp\DoctrineBehaviors\ORM\AbstractListener;

use Doctrine\ORM\Event\LoadClassMetadataEventArgs,
Doctrine\Common\EventSubscriber,
Doctrine\ORM\Events,
Expand All @@ -21,7 +25,7 @@
*
* Adds mapping to the timestampable entites.
*/
class TimestampableListener implements EventSubscriber
class TimestampableListener extends AbstractListener
{
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
{
Expand All @@ -32,7 +36,7 @@ public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs)
}

if ($this->isEntitySupported($classMetadata)) {
if ($classMetadata->reflClass->hasMethod('updateTimestamps')) {
if ($this->getClassAnalyzer()->isObjectHasMethod($classMetadata->reflClass, 'updateTimestamps')) {
$classMetadata->addLifecycleCallback('updateTimestamps', Events::prePersist);
$classMetadata->addLifecycleCallback('updateTimestamps', Events::preUpdate);
}
Expand All @@ -53,8 +57,6 @@ public function getSubscribedEvents()
*/
private function isEntitySupported(ClassMetadata $classMetadata)
{
$traitNames = $classMetadata->reflClass->getTraitNames();

return in_array('Knp\DoctrineBehaviors\Model\Timestampable\Timestampable', $traitNames);
return $this->getClassAnalyzer()->isObjectUseTrait($classMetadata->reflClass, 'Knp\DoctrineBehaviors\Model\Timestampable\Timestampable');
}
}
Loading