This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,97 @@
<?php
namespace Gedmo\SoftDeleteable\Filter\ODM;
use Doctrine\ODM\MongoDB\Query\Filter\BsonFilter;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
class SoftDeleteableFilter extends BsonFilter
{
protected $listener;
protected $documentManager;
protected $disabled = array();
/**
* Gets the criteria part to add to a query.
*
* @param ClassMetadata $targetEntity
*
* @return array The criteria array, if there is available, empty array otherwise
*/
public function addFilterCriteria(ClassMetadata $targetEntity)
{
$class = $targetEntity->getName();
if (array_key_exists($class, $this->disabled) && $this->disabled[$class] === true) {
return array();
} elseif (array_key_exists($targetEntity->rootDocumentName, $this->disabled) && $this->disabled[$targetEntity->rootDocumentName] === true) {
return array();
}
$config = $this->getListener()->getConfiguration($this->getDocumentManager(), $targetEntity->name);
if (!isset($config['softDeleteable']) || !$config['softDeleteable']) {
return array();
}
$column = $targetEntity->fieldMappings[$config['fieldName']];
if (isset($config['timeAware']) && $config['timeAware']) {
return array(
'$or' => array(
array($column['fieldName'] => null),
array($column['fieldName'] => array('$gt' => new \DateTime('now'))),
),
);
}
return array(
$column['fieldName'] => null,
);
}
protected function getListener()
{
if ($this->listener === null) {
$em = $this->getDocumentManager();
$evm = $em->getEventManager();
foreach ($evm->getListeners() as $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof SoftDeleteableListener) {
$this->listener = $listener;
break 2;
}
}
}
if ($this->listener === null) {
throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
}
}
return $this->listener;
}
protected function getDocumentManager()
{
if ($this->documentManager === null) {
$refl = new \ReflectionProperty('Doctrine\ODM\MongoDB\Query\Filter\BsonFilter', 'dm');
$refl->setAccessible(true);
$this->documentManager = $refl->getValue($this);
}
return $this->documentManager;
}
public function disableForDocument($class)
{
$this->disabled[$class] = true;
}
public function enableForDocument($class)
{
$this->disabled[$class] = false;
}
}

View File

@@ -0,0 +1,130 @@
<?php
namespace Gedmo\SoftDeleteable\Filter;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
/**
* The SoftDeleteableFilter adds the condition necessary to
* filter entities which were deleted "softly"
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Patrik Votoček <patrik@votocek.cz>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class SoftDeleteableFilter extends SQLFilter
{
/**
* @var SoftDeleteableListener
*/
protected $listener;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var string[bool]
*/
protected $disabled = array();
/**
* @param ClassMetadata $targetEntity
* @param string $targetTableAlias
* @return string
*/
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
{
$class = $targetEntity->getName();
if (array_key_exists($class, $this->disabled) && $this->disabled[$class] === true) {
return '';
} elseif (array_key_exists($targetEntity->rootEntityName, $this->disabled) && $this->disabled[$targetEntity->rootEntityName] === true) {
return '';
}
$config = $this->getListener()->getConfiguration($this->getEntityManager(), $targetEntity->name);
if (!isset($config['softDeleteable']) || !$config['softDeleteable']) {
return '';
}
$conn = $this->getEntityManager()->getConnection();
$platform = $conn->getDatabasePlatform();
$column = $targetEntity->getQuotedColumnName($config['fieldName'], $platform);
$addCondSql = $platform->getIsNullExpression($targetTableAlias.'.'.$column);
if (isset($config['timeAware']) && $config['timeAware']) {
$addCondSql = "({$addCondSql} OR {$targetTableAlias}.{$column} > {$platform->getCurrentTimestampSQL()})";
}
return $addCondSql;
}
/**
* @param string $class
*/
public function disableForEntity($class)
{
$this->disabled[$class] = true;
// Make sure the hash (@see SQLFilter::__toString()) for this filter will be changed to invalidate the query cache.
$this->setParameter(sprintf('disabled_%s', $class), true);
}
/**
* @param string $class
*/
public function enableForEntity($class)
{
$this->disabled[$class] = false;
// Make sure the hash (@see SQLFilter::__toString()) for this filter will be changed to invalidate the query cache.
$this->setParameter(sprintf('disabled_%s', $class), false);
}
/**
* @return SoftDeleteableListener
* @throws \RuntimeException
*/
protected function getListener()
{
if ($this->listener === null) {
$em = $this->getEntityManager();
$evm = $em->getEventManager();
foreach ($evm->getListeners() as $listeners) {
foreach ($listeners as $listener) {
if ($listener instanceof SoftDeleteableListener) {
$this->listener = $listener;
break 2;
}
}
}
if ($this->listener === null) {
throw new \RuntimeException('Listener "SoftDeleteableListener" was not added to the EventManager!');
}
}
return $this->listener;
}
/**
* @return EntityManagerInterface
*/
protected function getEntityManager()
{
if ($this->entityManager === null) {
$refl = new \ReflectionProperty('Doctrine\ORM\Query\Filter\SQLFilter', 'em');
$refl->setAccessible(true);
$this->entityManager = $refl->getValue($this);
}
return $this->entityManager;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Gedmo\SoftDeleteable\Mapping\Driver;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\SoftDeleteable\Mapping\Validator;
/**
* This is an annotation mapping driver for SoftDeleteable
* behavioral extension. Used for extraction of extended
* metadata from Annotations specifically for SoftDeleteable
* extension.
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Annotation extends AbstractAnnotationDriver
{
/**
* Annotation to define that this object is loggable
*/
const SOFT_DELETEABLE = 'Gedmo\\Mapping\\Annotation\\SoftDeleteable';
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$class = $this->getMetaReflectionClass($meta);
// class annotations
if ($class !== null && $annot = $this->reader->getClassAnnotation($class, self::SOFT_DELETEABLE)) {
$config['softDeleteable'] = true;
Validator::validateField($meta, $annot->fieldName);
$config['fieldName'] = $annot->fieldName;
$config['timeAware'] = false;
if (isset($annot->timeAware)) {
if (!is_bool($annot->timeAware)) {
throw new InvalidMappingException("timeAware must be boolean. ".gettype($annot->timeAware)." provided.");
}
$config['timeAware'] = $annot->timeAware;
}
$config['hardDelete'] = true;
if (isset($annot->hardDelete)) {
if (!is_bool($annot->hardDelete)) {
throw new InvalidMappingException("hardDelete must be boolean. ".gettype($annot->hardDelete)." provided.");
}
$config['hardDelete'] = $annot->hardDelete;
}
}
$this->validateFullMetadata($meta, $config);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Gedmo\SoftDeleteable\Mapping\Driver;
use Gedmo\Mapping\Driver\Xml as BaseXml;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\SoftDeleteable\Mapping\Validator;
/**
* This is a xml mapping driver for SoftDeleteable
* behavioral extension. Used for extraction of extended
* metadata from xml specifically for SoftDeleteable
* extension.
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Xml extends BaseXml
{
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
/**
* @var \SimpleXmlElement $xml
*/
$xml = $this->_getMapping($meta->name);
$xmlDoctrine = $xml;
$xml = $xml->children(self::GEDMO_NAMESPACE_URI);
if (in_array($xmlDoctrine->getName(), array('mapped-superclass', 'entity', 'document', 'embedded-document'))) {
if (isset($xml->{'soft-deleteable'})) {
$field = $this->_getAttribute($xml->{'soft-deleteable'}, 'field-name');
if (!$field) {
throw new InvalidMappingException('Field name for SoftDeleteable class is mandatory.');
}
Validator::validateField($meta, $field);
$config['softDeleteable'] = true;
$config['fieldName'] = $field;
$config['timeAware'] = false;
if ($this->_isAttributeSet($xml->{'soft-deleteable'}, 'time-aware')) {
$config['timeAware'] = $this->_getBooleanAttribute($xml->{'soft-deleteable'}, 'time-aware');
}
$config['hardDelete'] = true;
if ($this->_isAttributeSet($xml->{'soft-deleteable'}, 'hard-delete')) {
$config['hardDelete'] = $this->_getBooleanAttribute($xml->{'soft-deleteable'}, 'hard-delete');
}
}
}
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Gedmo\SoftDeleteable\Mapping\Driver;
use Gedmo\Mapping\Driver\File;
use Gedmo\Mapping\Driver;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\SoftDeleteable\Mapping\Validator;
/**
* This is a yaml mapping driver for Timestampable
* behavioral extension. Used for extraction of extended
* metadata from yaml specifically for Timestampable
* extension.
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Yaml extends File implements Driver
{
/**
* File extension
* @var string
*/
protected $_extension = '.dcm.yml';
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$mapping = $this->_getMapping($meta->name);
if (isset($mapping['gedmo'])) {
$classMapping = $mapping['gedmo'];
if (isset($classMapping['soft_deleteable'])) {
$config['softDeleteable'] = true;
if (!isset($classMapping['soft_deleteable']['field_name'])) {
throw new InvalidMappingException('Field name for SoftDeleteable class is mandatory.');
}
$fieldName = $classMapping['soft_deleteable']['field_name'];
Validator::validateField($meta, $fieldName);
$config['fieldName'] = $fieldName;
$config['timeAware'] = false;
if (isset($classMapping['soft_deleteable']['time_aware'])) {
if (!is_bool($classMapping['soft_deleteable']['time_aware'])) {
throw new InvalidMappingException("timeAware must be boolean. ".gettype($classMapping['soft_deleteable']['time_aware'])." provided.");
}
$config['timeAware'] = $classMapping['soft_deleteable']['time_aware'];
}
$config['hardDelete'] = true;
if (isset($classMapping['soft_deleteable']['hard_delete'])) {
if (!is_bool($classMapping['soft_deleteable']['hard_delete'])) {
throw new InvalidMappingException("hardDelete must be boolean. ".gettype($classMapping['soft_deleteable']['hard_delete'])." provided.");
}
$config['hardDelete'] = $classMapping['soft_deleteable']['hard_delete'];
}
}
}
}
/**
* {@inheritDoc}
*/
protected function _loadMappingFile($file)
{
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\SoftDeleteable\Mapping\Event\Adapter;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter;
/**
* Doctrine event adapter for ORM adapted
* for SoftDeleteable behavior.
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ORM extends BaseAdapterORM implements SoftDeleteableAdapter
{
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Gedmo\SoftDeleteable\Mapping\Event;
use Gedmo\Mapping\Event\AdapterInterface;
/**
* Doctrine event adapter interface
* for SoftDeleteable behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface SoftDeleteableAdapter extends AdapterInterface
{
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Gedmo\SoftDeleteable\Mapping;
use Gedmo\Exception\InvalidMappingException;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
/**
* This class is used to validate mapping information
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Validator
{
/**
* List of types which are valid for timestamp
*
* @var array
*/
public static $validTypes = array(
'date',
'date_immutable',
'time',
'time_immutable',
'datetime',
'datetime_immutable',
'datetimetz',
'datetimetz_immutable',
'timestamp',
'zenddate',
);
public static function validateField(ClassMetadata $meta, $field)
{
if ($meta->isMappedSuperclass) {
return;
}
$fieldMapping = $meta->getFieldMapping($field);
if (!in_array($fieldMapping['type'], self::$validTypes)) {
throw new InvalidMappingException(sprintf('Field "%s" (type "%s") must be of one of the following types: "%s" in entity %s',
$field,
$fieldMapping['type'],
implode(', ', self::$validTypes),
$meta->name));
}
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace Gedmo\SoftDeleteable\Query\TreeWalker\Exec;
use Doctrine\ORM\Query\Exec\MultiTableDeleteExecutor as BaseMultiTableDeleteExecutor;
use Doctrine\ORM\Query\AST\Node;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* This class is used when a DELETE DQL query is called for entities
* that are part of an inheritance tree
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class MultiTableDeleteExecutor extends BaseMultiTableDeleteExecutor
{
/**
* {@inheritDoc}
*/
public function __construct(Node $AST, $sqlWalker, ClassMetadataInfo $meta, AbstractPlatform $platform, array $config)
{
parent::__construct($AST, $sqlWalker);
$reflProp = new \ReflectionProperty(get_class($this), '_sqlStatements');
$reflProp->setAccessible(true);
$sqlStatements = $reflProp->getValue($this);
foreach ($sqlStatements as $index => $stmt) {
$matches = array();
preg_match('/DELETE FROM (\w+) .+/', $stmt, $matches);
if (isset($matches[1]) && $meta->getQuotedTableName($platform) === $matches[1]) {
$sqlStatements[$index] = str_replace('DELETE FROM', 'UPDATE', $stmt);
$sqlStatements[$index] = str_replace(
'WHERE',
'SET '.$config['fieldName'].' = '.$platform->getCurrentTimestampSQL().' WHERE',
$sqlStatements[$index]
);
} else {
// We have to avoid the removal of registers of child entities of a SoftDeleteable entity
unset($sqlStatements[$index]);
}
}
$reflProp->setValue($this, $sqlStatements);
}
}

View File

@@ -0,0 +1,139 @@
<?php
namespace Gedmo\SoftDeleteable\Query\TreeWalker;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\AST\DeleteStatement;
use Doctrine\ORM\Query\AST\DeleteClause;
use Doctrine\ORM\Query\Exec\SingleTableDeleteUpdateExecutor;
use Gedmo\SoftDeleteable\SoftDeleteableListener;
use Gedmo\SoftDeleteable\Query\TreeWalker\Exec\MultiTableDeleteExecutor;
/**
* This SqlWalker is needed when you need to use a DELETE DQL query.
* It will update the "deletedAt" field with the actual date, instead
* of actually deleting it.
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class SoftDeleteableWalker extends SqlWalker
{
protected $conn;
protected $platform;
protected $listener;
protected $configuration;
protected $alias;
protected $deletedAtField;
protected $meta;
/**
* {@inheritDoc}
*/
public function __construct($query, $parserResult, array $queryComponents)
{
parent::__construct($query, $parserResult, $queryComponents);
$this->conn = $this->getConnection();
$this->platform = $this->conn->getDatabasePlatform();
$this->listener = $this->getSoftDeleteableListener();
$this->extractComponents($queryComponents);
}
/**
* {@inheritDoc}
*/
public function getExecutor($AST)
{
switch (true) {
case ($AST instanceof DeleteStatement):
$primaryClass = $this->getEntityManager()->getClassMetadata($AST->deleteClause->abstractSchemaName);
return ($primaryClass->isInheritanceTypeJoined())
? new MultiTableDeleteExecutor($AST, $this, $this->meta, $this->platform, $this->configuration)
: new SingleTableDeleteUpdateExecutor($AST, $this);
default:
throw new \Gedmo\Exception\UnexpectedValueException('SoftDeleteable walker should be used only on delete statement');
}
}
/**
* Change a DELETE clause for an UPDATE clause
*
* @param DeleteClause $deleteClause
*
* @return string The SQL.
*/
public function walkDeleteClause(DeleteClause $deleteClause)
{
$em = $this->getEntityManager();
$class = $em->getClassMetadata($deleteClause->abstractSchemaName);
$tableName = $class->getTableName();
$this->setSQLTableAlias($tableName, $tableName, $deleteClause->aliasIdentificationVariable);
$quotedTableName = $class->getQuotedTableName($this->platform);
$quotedColumnName = $class->getQuotedColumnName($this->deletedAtField, $this->platform);
$sql = 'UPDATE '.$quotedTableName.' SET '.$quotedColumnName.' = '.$this->platform->getCurrentTimestampSQL();
return $sql;
}
/**
* Get the currently used SoftDeleteableListener
*
* @throws \Gedmo\Exception\RuntimeException - if listener is not found
*
* @return SoftDeleteableListener
*/
private function getSoftDeleteableListener()
{
if (is_null($this->listener)) {
$em = $this->getEntityManager();
foreach ($em->getEventManager()->getListeners() as $event => $listeners) {
foreach ($listeners as $hash => $listener) {
if ($listener instanceof SoftDeleteableListener) {
$this->listener = $listener;
break;
}
}
if ($this->listener) {
break;
}
}
if (is_null($this->listener)) {
throw new \Gedmo\Exception\RuntimeException('The SoftDeleteable listener could not be found.');
}
}
return $this->listener;
}
/**
* Search for components in the delete clause
*
* @param array $queryComponents
*
* @return void
*/
private function extractComponents(array $queryComponents)
{
$em = $this->getEntityManager();
foreach ($queryComponents as $alias => $comp) {
if (!isset($comp['metadata'])) {
continue;
}
$meta = $comp['metadata'];
$config = $this->listener->getConfiguration($em, $meta->name);
if ($config && isset($config['softDeleteable']) && $config['softDeleteable']) {
$this->configuration = $config;
$this->deletedAtField = $config['fieldName'];
$this->meta = $meta;
}
}
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Gedmo\SoftDeleteable;
/**
* This interface is not necessary but can be implemented for
* Domain Objects which in some cases needs to be identified as
* SoftDeleteable
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface SoftDeleteable
{
// this interface is not necessary to implement
/**
* @gedmo:SoftDeleteable
* to mark the class as SoftDeleteable use class annotation @gedmo:SoftDeleteable
* this object will be able to be soft deleted
* example:
*
* @gedmo:SoftDeleteable
* class MyEntity
*/
}

View File

@@ -0,0 +1,119 @@
<?php
namespace Gedmo\SoftDeleteable;
use Gedmo\Mapping\MappedEventSubscriber;
use Doctrine\Common\EventArgs;
use Doctrine\ODM\MongoDB\UnitOfWork as MongoDBUnitOfWork;
/**
* SoftDeleteable listener
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class SoftDeleteableListener extends MappedEventSubscriber
{
/**
* Pre soft-delete event
*
* @var string
*/
const PRE_SOFT_DELETE = "preSoftDelete";
/**
* Post soft-delete event
*
* @var string
*/
const POST_SOFT_DELETE = "postSoftDelete";
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array(
'loadClassMetadata',
'onFlush',
);
}
/**
* If it's a SoftDeleteable object, update the "deletedAt" field
* and skip the removal of the object
*
* @param EventArgs $args
*
* @return void
*/
public function onFlush(EventArgs $args)
{
$ea = $this->getEventAdapter($args);
$om = $ea->getObjectManager();
$uow = $om->getUnitOfWork();
$evm = $om->getEventManager();
//getScheduledDocumentDeletions
foreach ($ea->getScheduledObjectDeletions($uow) as $object) {
$meta = $om->getClassMetadata(get_class($object));
$config = $this->getConfiguration($om, $meta->name);
if (isset($config['softDeleteable']) && $config['softDeleteable']) {
$reflProp = $meta->getReflectionProperty($config['fieldName']);
$oldValue = $reflProp->getValue($object);
$date = new \DateTime();
// Remove `$oldValue instanceof \DateTime` check when PHP version is bumped to >=5.5
if (isset($config['hardDelete']) && $config['hardDelete'] && ($oldValue instanceof \DateTime || $oldValue instanceof \DateTimeInterface) && $oldValue <= $date) {
continue; // want to hard delete
}
$evm->dispatchEvent(
self::PRE_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
);
$reflProp->setValue($object, $date);
$om->persist($object);
$uow->propertyChanged($object, $config['fieldName'], $oldValue, $date);
if ($uow instanceof MongoDBUnitOfWork && !method_exists($uow, 'scheduleExtraUpdate')) {
$ea->recomputeSingleObjectChangeSet($uow, $meta, $object);
} else {
$uow->scheduleExtraUpdate($object, array(
$config['fieldName'] => array($oldValue, $date),
));
}
$evm->dispatchEvent(
self::POST_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
);
}
}
}
/**
* Maps additional metadata
*
* @param EventArgs $eventArgs
*
* @return void
*/
public function loadClassMetadata(EventArgs $eventArgs)
{
$ea = $this->getEventAdapter($eventArgs);
$this->loadMetadataForObjectClass($ea->getObjectManager(), $eventArgs->getClassMetadata());
}
/**
* {@inheritDoc}
*/
protected function getNamespace()
{
return __NAMESPACE__;
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Gedmo\SoftDeleteable\Traits;
use DateTime;
/**
* A generic trait to use on your self-deletable entities.
* There is no mapping information defined in this trait.
*
* @author Wesley van Opdorp <wesley.van.opdorp@freshheads.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait SoftDeleteable
{
/**
* @var DateTime|null
*/
protected $deletedAt;
/**
* Set or clear the deleted at timestamp.
*
* @return self
*/
public function setDeletedAt(DateTime $deletedAt = null)
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get the deleted at timestamp value. Will return null if
* the entity has not been soft deleted.
*
* @return DateTime|null
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Check if the entity has been soft deleted.
*
* @return bool
*/
public function isDeleted()
{
return null !== $this->deletedAt;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Gedmo\SoftDeleteable\Traits;
use DateTime;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
/**
* A soft deletable trait you can apply to your MongoDB entities.
* Includes default annotation mapping.
*
* @author Wesley van Opdorp <wesley.van.opdorp@freshheads.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait SoftDeleteableDocument
{
/**
* @ODM\Field(type="date")
*
* @var DateTime|null
*/
protected $deletedAt;
/**
* Set or clear the deleted at timestamp.
*
* @return self
*/
public function setDeletedAt(DateTime $deletedAt = null)
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get the deleted at timestamp value. Will return null if
* the entity has not been soft deleted.
*
* @return DateTime|null
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Check if the entity has been soft deleted.
*
* @return bool
*/
public function isDeleted()
{
return null !== $this->deletedAt;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace Gedmo\SoftDeleteable\Traits;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
/**
* A soft deletable trait you can apply to your Doctrine ORM entities.
* Includes default annotation mapping.
*
* @author Wesley van Opdorp <wesley.van.opdorp@freshheads.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait SoftDeleteableEntity
{
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var DateTime|null
*/
protected $deletedAt;
/**
* Set or clear the deleted at timestamp.
*
* @return self
*/
public function setDeletedAt(DateTime $deletedAt = null)
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* Get the deleted at timestamp value. Will return null if
* the entity has not been soft deleted.
*
* @return DateTime|null
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* Check if the entity has been soft deleted.
*
* @return bool
*/
public function isDeleted()
{
return null !== $this->deletedAt;
}
}