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,25 @@
<?php
namespace Gedmo\Loggable\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoODM;
/**
* Gedmo\Loggable\Document\LogEntry
*
* @MongoODM\Document(
* repositoryClass="Gedmo\Loggable\Document\Repository\LogEntryRepository",
* indexes={
* @MongoODM\Index(keys={"objectId"="asc", "objectClass"="asc", "version"="asc"}),
* @MongoODM\Index(keys={"loggedAt"="asc"}),
* @MongoODM\Index(keys={"objectClass"="asc"}),
* @MongoODM\Index(keys={"username"="asc"})
* }
* )
*/
class LogEntry extends MappedSuperclass\AbstractLogEntry
{
/**
* All required columns are mapped through inherited superclass
*/
}

View File

@@ -0,0 +1,217 @@
<?php
namespace Gedmo\Loggable\Document\MappedSuperclass;
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoODM;
/**
* Gedmo\Loggable\Document\MappedSuperclass\AbstractLogEntry
*
* @MongoODM\MappedSuperclass
*/
abstract class AbstractLogEntry
{
/**
* @var integer $id
*
* @MongoODM\Id
*/
protected $id;
/**
* @var string $action
*
* @MongoODM\Field(type="string")
*/
protected $action;
/**
* @var \DateTime $loggedAt
*
* @MongoODM\Field(type="date")
*/
protected $loggedAt;
/**
* @var string $objectId
*
* @MongoODM\Field(type="string", nullable=true)
*/
protected $objectId;
/**
* @var string $objectClass
*
* @MongoODM\Field(type="string")
*/
protected $objectClass;
/**
* @var integer $version
*
* @MongoODM\Field(type="int")
*/
protected $version;
/**
* @var string $data
*
* @MongoODM\Field(type="hash", nullable=true)
*/
protected $data;
/**
* @var string $data
*
* @MongoODM\Field(type="string", nullable=true)
*/
protected $username;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get action
*
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Set action
*
* @param string $action
*/
public function setAction($action)
{
$this->action = $action;
}
/**
* Get object class
*
* @return string
*/
public function getObjectClass()
{
return $this->objectClass;
}
/**
* Set object class
*
* @param string $objectClass
*/
public function setObjectClass($objectClass)
{
$this->objectClass = $objectClass;
}
/**
* Get object id
*
* @return string
*/
public function getObjectId()
{
return $this->objectId;
}
/**
* Set object id
*
* @param string $objectId
*/
public function setObjectId($objectId)
{
$this->objectId = $objectId;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set username
*
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Get loggedAt
*
* @return \DateTime
*/
public function getLoggedAt()
{
return $this->loggedAt;
}
/**
* Set loggedAt to "now"
*/
public function setLoggedAt()
{
$this->loggedAt = new \DateTime();
}
/**
* Get data
*
* @return array or null
*/
public function getData()
{
return $this->data;
}
/**
* Set data
*
* @param array $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
* Set current version
*
* @param integer $version
*/
public function setVersion($version)
{
$this->version = $version;
}
/**
* Get current version
*
* @return integer
*/
public function getVersion()
{
return $this->version;
}
}

View File

@@ -0,0 +1,161 @@
<?php
namespace Gedmo\Loggable\Document\Repository;
use Gedmo\Loggable\Document\LogEntry;
use Gedmo\Tool\Wrapper\MongoDocumentWrapper;
use Gedmo\Loggable\LoggableListener;
use Doctrine\ODM\MongoDB\DocumentRepository;
use Doctrine\ODM\MongoDB\Cursor;
/**
* The LogEntryRepository has some useful functions
* to interact with log entries.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class LogEntryRepository extends DocumentRepository
{
/**
* Currently used loggable listener
*
* @var LoggableListener
*/
private $listener;
/**
* Loads all log entries for the
* given $document
*
* @param object $document
*
* @return LogEntry[]
*/
public function getLogEntries($document)
{
$wrapped = new MongoDocumentWrapper($document, $this->dm);
$objectId = $wrapped->getIdentifier();
$qb = $this->createQueryBuilder();
$qb->field('objectId')->equals($objectId);
$qb->field('objectClass')->equals($wrapped->getMetadata()->name);
$qb->sort('version', 'DESC');
$q = $qb->getQuery();
$result = $q->execute();
if ($result instanceof Cursor) {
$result = $result->toArray();
}
return $result;
}
/**
* Reverts given $document to $revision by
* restoring all fields from that $revision.
* After this operation you will need to
* persist and flush the $document.
*
* @param object $document
* @param integer $version
*
* @throws \Gedmo\Exception\UnexpectedValueException
*
* @return void
*/
public function revert($document, $version = 1)
{
$wrapped = new MongoDocumentWrapper($document, $this->dm);
$objectMeta = $wrapped->getMetadata();
$objectId = $wrapped->getIdentifier();
$qb = $this->createQueryBuilder();
$qb->field('objectId')->equals($objectId);
$qb->field('objectClass')->equals($objectMeta->name);
$qb->field('version')->lte(intval($version));
$qb->sort('version', 'ASC');
$q = $qb->getQuery();
$logs = $q->execute();
if ($logs instanceof Cursor) {
$logs = $logs->toArray();
}
if ($logs) {
$data = array();
while (($log = array_shift($logs))) {
$data = array_merge($data, $log->getData());
}
$this->fillDocument($document, $data, $objectMeta);
} else {
throw new \Gedmo\Exception\UnexpectedValueException('Count not find any log entries under version: '.$version);
}
}
/**
* Fills a documents versioned fields with data
*
* @param object $document
* @param array $data
*/
protected function fillDocument($document, array $data)
{
$wrapped = new MongoDocumentWrapper($document, $this->dm);
$objectMeta = $wrapped->getMetadata();
$config = $this->getLoggableListener()->getConfiguration($this->dm, $objectMeta->name);
$fields = $config['versioned'];
foreach ($data as $field => $value) {
if (!in_array($field, $fields)) {
continue;
}
$mapping = $objectMeta->getFieldMapping($field);
// Fill the embedded document
if ($wrapped->isEmbeddedAssociation($field)) {
if (!empty($value)) {
$embeddedMetadata = $this->dm->getClassMetadata($mapping['targetDocument']);
$document = $embeddedMetadata->newInstance();
$this->fillDocument($document, $value);
$value = $document;
}
} elseif ($objectMeta->isSingleValuedAssociation($field)) {
$value = $value ? $this->dm->getReference($mapping['targetDocument'], $value) : null;
}
$wrapped->setPropertyValue($field, $value);
unset($fields[$field]);
}
/*
if (count($fields)) {
throw new \Gedmo\Exception\UnexpectedValueException('Cound not fully revert the document to version: '.$version);
}
*/
}
/**
* Get the currently used LoggableListener
*
* @throws \Gedmo\Exception\RuntimeException - if listener is not found
*
* @return LoggableListener
*/
private function getLoggableListener()
{
if (is_null($this->listener)) {
foreach ($this->dm->getEventManager()->getListeners() as $event => $listeners) {
foreach ($listeners as $hash => $listener) {
if ($listener instanceof LoggableListener) {
$this->listener = $listener;
break;
}
}
if ($this->listener) {
break;
}
}
if (is_null($this->listener)) {
throw new \Gedmo\Exception\RuntimeException('The loggable listener could not be found');
}
}
return $this->listener;
}
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Gedmo\Loggable\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Gedmo\Loggable\Entity\LogEntry
*
* @ORM\Table(
* name="ext_log_entries",
* options={"row_format":"DYNAMIC"},
* indexes={
* @ORM\Index(name="log_class_lookup_idx", columns={"object_class"}),
* @ORM\Index(name="log_date_lookup_idx", columns={"logged_at"}),
* @ORM\Index(name="log_user_lookup_idx", columns={"username"}),
* @ORM\Index(name="log_version_lookup_idx", columns={"object_id", "object_class", "version"})
* }
* )
* @ORM\Entity(repositoryClass="Gedmo\Loggable\Entity\Repository\LogEntryRepository")
*/
class LogEntry extends MappedSuperclass\AbstractLogEntry
{
/**
* All required columns are mapped through inherited superclass
*/
}

View File

@@ -0,0 +1,219 @@
<?php
namespace Gedmo\Loggable\Entity\MappedSuperclass;
use Doctrine\ORM\Mapping as ORM;
/**
* Gedmo\Loggable\Entity\AbstractLog
*
* @ORM\MappedSuperclass
*/
abstract class AbstractLogEntry
{
/**
* @var integer $id
*
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue
*/
protected $id;
/**
* @var string $action
*
* @ORM\Column(type="string", length=8)
*/
protected $action;
/**
* @var \DateTime $loggedAt
*
* @ORM\Column(name="logged_at", type="datetime")
*/
protected $loggedAt;
/**
* @var string $objectId
*
* @ORM\Column(name="object_id", length=64, nullable=true)
*/
protected $objectId;
/**
* @var string $objectClass
*
* @ORM\Column(name="object_class", type="string", length=255)
*/
protected $objectClass;
/**
* @var integer $version
*
* @ORM\Column(type="integer")
*/
protected $version;
/**
* @var array $data
*
* @ORM\Column(type="array", nullable=true)
*/
protected $data;
/**
* @var string $data
*
* @ORM\Column(length=255, nullable=true)
*/
protected $username;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get action
*
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Set action
*
* @param string $action
*/
public function setAction($action)
{
$this->action = $action;
}
/**
* Get object class
*
* @return string
*/
public function getObjectClass()
{
return $this->objectClass;
}
/**
* Set object class
*
* @param string $objectClass
*/
public function setObjectClass($objectClass)
{
$this->objectClass = $objectClass;
}
/**
* Get object id
*
* @return string
*/
public function getObjectId()
{
return $this->objectId;
}
/**
* Set object id
*
* @param string $objectId
*/
public function setObjectId($objectId)
{
$this->objectId = $objectId;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set username
*
* @param string $username
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Get loggedAt
*
* @return \DateTime
*/
public function getLoggedAt()
{
return $this->loggedAt;
}
/**
* Set loggedAt to "now"
*/
public function setLoggedAt()
{
$this->loggedAt = new \DateTime();
}
/**
* Get data
*
* @return array
*/
public function getData()
{
return $this->data;
}
/**
* Set data
*
* @param array $data
*/
public function setData($data)
{
$this->data = $data;
}
/**
* Set current version
*
* @param integer $version
*/
public function setVersion($version)
{
$this->version = $version;
}
/**
* Get current version
*
* @return integer
*/
public function getVersion()
{
return $this->version;
}
}

View File

@@ -0,0 +1,164 @@
<?php
namespace Gedmo\Loggable\Entity\Repository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
use Gedmo\Loggable\Entity\LogEntry;
use Gedmo\Tool\Wrapper\EntityWrapper;
use Doctrine\ORM\EntityRepository;
use Gedmo\Loggable\LoggableListener;
/**
* The LogEntryRepository has some useful functions
* to interact with log entries.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class LogEntryRepository extends EntityRepository
{
/**
* Currently used loggable listener
*
* @var LoggableListener
*/
private $listener;
/**
* Loads all log entries for the given entity
*
* @param object $entity
*
* @return LogEntry[]
*/
public function getLogEntries($entity)
{
$q = $this->getLogEntriesQuery($entity);
return $q->getResult();
}
/**
* Get the query for loading of log entries
*
* @param object $entity
*
* @return Query
*/
public function getLogEntriesQuery($entity)
{
$wrapped = new EntityWrapper($entity, $this->_em);
$objectClass = $wrapped->getMetadata()->name;
$meta = $this->getClassMetadata();
$dql = "SELECT log FROM {$meta->name} log";
$dql .= " WHERE log.objectId = :objectId";
$dql .= " AND log.objectClass = :objectClass";
$dql .= " ORDER BY log.version DESC";
$objectId = (string) $wrapped->getIdentifier();
$q = $this->_em->createQuery($dql);
$q->setParameters(compact('objectId', 'objectClass'));
return $q;
}
/**
* Reverts given $entity to $revision by
* restoring all fields from that $revision.
* After this operation you will need to
* persist and flush the $entity.
*
* @param object $entity
* @param integer $version
*
* @throws \Gedmo\Exception\UnexpectedValueException
*
* @return void
*/
public function revert($entity, $version = 1)
{
$wrapped = new EntityWrapper($entity, $this->_em);
$objectMeta = $wrapped->getMetadata();
$objectClass = $objectMeta->name;
$meta = $this->getClassMetadata();
$dql = "SELECT log FROM {$meta->name} log";
$dql .= " WHERE log.objectId = :objectId";
$dql .= " AND log.objectClass = :objectClass";
$dql .= " AND log.version <= :version";
$dql .= " ORDER BY log.version ASC";
$objectId = (string) $wrapped->getIdentifier();
$q = $this->_em->createQuery($dql);
$q->setParameters(compact('objectId', 'objectClass', 'version'));
$logs = $q->getResult();
if ($logs) {
$config = $this->getLoggableListener()->getConfiguration($this->_em, $objectMeta->name);
$fields = $config['versioned'];
$filled = false;
while (($log = array_pop($logs)) && !$filled) {
if ($data = $log->getData()) {
foreach ($data as $field => $value) {
if (in_array($field, $fields)) {
$this->mapValue($objectMeta, $field, $value);
$wrapped->setPropertyValue($field, $value);
unset($fields[array_search($field, $fields)]);
}
}
}
$filled = count($fields) === 0;
}
/*if (count($fields)) {
throw new \Gedmo\Exception\UnexpectedValueException('Could not fully revert the entity to version: '.$version);
}*/
} else {
throw new \Gedmo\Exception\UnexpectedValueException('Could not find any log entries under version: '.$version);
}
}
/**
* @param ClassMetadata $objectMeta
* @param string $field
* @param mixed $value
*/
protected function mapValue(ClassMetadata $objectMeta, $field, &$value)
{
if (!$objectMeta->isSingleValuedAssociation($field)) {
return;
}
$mapping = $objectMeta->getAssociationMapping($field);
$value = $value ? $this->_em->getReference($mapping['targetEntity'], $value) : null;
}
/**
* Get the currently used LoggableListener
*
* @throws \Gedmo\Exception\RuntimeException - if listener is not found
*
* @return LoggableListener
*/
private function getLoggableListener()
{
if (is_null($this->listener)) {
foreach ($this->_em->getEventManager()->getListeners() as $event => $listeners) {
foreach ($listeners as $hash => $listener) {
if ($listener instanceof LoggableListener) {
$this->listener = $listener;
break;
}
}
if ($this->listener) {
break;
}
}
if (is_null($this->listener)) {
throw new \Gedmo\Exception\RuntimeException('The loggable listener could not be found');
}
}
return $this->listener;
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Gedmo\Loggable;
/**
* This interface is not necessary but can be implemented for
* Domain Objects which in some cases needs to be identified as
* Loggable
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface Loggable
{
// this interface is not necessary to implement
/**
* @gedmo:Loggable
* to mark the class as loggable use class annotation @gedmo:Loggable
* this object will contain now a history
* available options:
* logEntryClass="My\LogEntryObject" (optional) defaultly will use internal object class
* example:
*
* @gedmo:Loggable(logEntryClass="My\LogEntryObject")
* class MyEntity
*/
}

View File

@@ -0,0 +1,324 @@
<?php
namespace Gedmo\Loggable;
use Doctrine\Common\EventArgs;
use Gedmo\Mapping\MappedEventSubscriber;
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
use Gedmo\Tool\Wrapper\AbstractWrapper;
/**
* Loggable listener
*
* @author Boussekeyt Jules <jules.boussekeyt@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class LoggableListener extends MappedEventSubscriber
{
/**
* Create action
*/
const ACTION_CREATE = 'create';
/**
* Update action
*/
const ACTION_UPDATE = 'update';
/**
* Remove action
*/
const ACTION_REMOVE = 'remove';
/**
* Username for identification
*
* @var string
*/
protected $username;
/**
* List of log entries which do not have the foreign
* key generated yet - MySQL case. These entries
* will be updated with new keys on postPersist event
*
* @var array
*/
protected $pendingLogEntryInserts = array();
/**
* For log of changed relations we use
* its identifiers to avoid storing serialized Proxies.
* These are pending relations in case it does not
* have an identifier yet
*
* @var array
*/
protected $pendingRelatedObjects = array();
/**
* Set username for identification
*
* @param mixed $username
*
* @throws \Gedmo\Exception\InvalidArgumentException Invalid username
*/
public function setUsername($username)
{
if (is_string($username)) {
$this->username = $username;
} elseif (is_object($username) && method_exists($username, 'getUsername')) {
$this->username = (string) $username->getUsername();
} else {
throw new \Gedmo\Exception\InvalidArgumentException("Username must be a string, or object should have method: getUsername");
}
}
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return array(
'onFlush',
'loadClassMetadata',
'postPersist',
);
}
/**
* Get the LogEntry class
*
* @param LoggableAdapter $ea
* @param string $class
*
* @return string
*/
protected function getLogEntryClass(LoggableAdapter $ea, $class)
{
return isset(self::$configurations[$this->name][$class]['logEntryClass']) ?
self::$configurations[$this->name][$class]['logEntryClass'] :
$ea->getDefaultLogEntryClass();
}
/**
* Maps additional metadata
*
* @param EventArgs $eventArgs
*
* @return void
*/
public function loadClassMetadata(EventArgs $eventArgs)
{
$ea = $this->getEventAdapter($eventArgs);
$this->loadMetadataForObjectClass($ea->getObjectManager(), $eventArgs->getClassMetadata());
}
/**
* Checks for inserted object to update its logEntry
* foreign key
*
* @param EventArgs $args
*
* @return void
*/
public function postPersist(EventArgs $args)
{
$ea = $this->getEventAdapter($args);
$object = $ea->getObject();
$om = $ea->getObjectManager();
$oid = spl_object_hash($object);
$uow = $om->getUnitOfWork();
if ($this->pendingLogEntryInserts && array_key_exists($oid, $this->pendingLogEntryInserts)) {
$wrapped = AbstractWrapper::wrap($object, $om);
$logEntry = $this->pendingLogEntryInserts[$oid];
$logEntryMeta = $om->getClassMetadata(get_class($logEntry));
$id = $wrapped->getIdentifier();
$logEntryMeta->getReflectionProperty('objectId')->setValue($logEntry, $id);
$uow->scheduleExtraUpdate($logEntry, array(
'objectId' => array(null, $id),
));
$ea->setOriginalObjectProperty($uow, spl_object_hash($logEntry), 'objectId', $id);
unset($this->pendingLogEntryInserts[$oid]);
}
if ($this->pendingRelatedObjects && array_key_exists($oid, $this->pendingRelatedObjects)) {
$wrapped = AbstractWrapper::wrap($object, $om);
$identifiers = $wrapped->getIdentifier(false);
foreach ($this->pendingRelatedObjects[$oid] as $props) {
$logEntry = $props['log'];
$logEntryMeta = $om->getClassMetadata(get_class($logEntry));
$oldData = $data = $logEntry->getData();
$data[$props['field']] = $identifiers;
$logEntry->setData($data);
$uow->scheduleExtraUpdate($logEntry, array(
'data' => array($oldData, $data),
));
$ea->setOriginalObjectProperty($uow, spl_object_hash($logEntry), 'data', $data);
}
unset($this->pendingRelatedObjects[$oid]);
}
}
/**
* Handle any custom LogEntry functionality that needs to be performed
* before persisting it
*
* @param object $logEntry The LogEntry being persisted
* @param object $object The object being Logged
*/
protected function prePersistLogEntry($logEntry, $object)
{
}
/**
* Looks for loggable objects being inserted or updated
* for further processing
*
* @param EventArgs $eventArgs
*
* @return void
*/
public function onFlush(EventArgs $eventArgs)
{
$ea = $this->getEventAdapter($eventArgs);
$om = $ea->getObjectManager();
$uow = $om->getUnitOfWork();
foreach ($ea->getScheduledObjectInsertions($uow) as $object) {
$this->createLogEntry(self::ACTION_CREATE, $object, $ea);
}
foreach ($ea->getScheduledObjectUpdates($uow) as $object) {
$this->createLogEntry(self::ACTION_UPDATE, $object, $ea);
}
foreach ($ea->getScheduledObjectDeletions($uow) as $object) {
$this->createLogEntry(self::ACTION_REMOVE, $object, $ea);
}
}
/**
* {@inheritDoc}
*/
protected function getNamespace()
{
return __NAMESPACE__;
}
/**
* Returns an objects changeset data
*
* @param LoggableAdapter $ea
* @param object $object
* @param object $logEntry
*
* @return array
*/
protected function getObjectChangeSetData($ea, $object, $logEntry)
{
$om = $ea->getObjectManager();
$wrapped = AbstractWrapper::wrap($object, $om);
$meta = $wrapped->getMetadata();
$config = $this->getConfiguration($om, $meta->name);
$uow = $om->getUnitOfWork();
$newValues = array();
foreach ($ea->getObjectChangeSet($uow, $object) as $field => $changes) {
if (empty($config['versioned']) || !in_array($field, $config['versioned'])) {
continue;
}
$value = $changes[1];
if ($meta->isSingleValuedAssociation($field) && $value) {
if ($wrapped->isEmbeddedAssociation($field)) {
$value = $this->getObjectChangeSetData($ea, $value, $logEntry);
} else {
$oid = spl_object_hash($value);
$wrappedAssoc = AbstractWrapper::wrap($value, $om);
$value = $wrappedAssoc->getIdentifier(false);
if (!is_array($value) && !$value) {
$this->pendingRelatedObjects[$oid][] = array(
'log' => $logEntry,
'field' => $field,
);
}
}
}
$newValues[$field] = $value;
}
return $newValues;
}
/**
* Create a new Log instance
*
* @param string $action
* @param object $object
* @param LoggableAdapter $ea
*
* @return \Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry|null
*/
protected function createLogEntry($action, $object, LoggableAdapter $ea)
{
$om = $ea->getObjectManager();
$wrapped = AbstractWrapper::wrap($object, $om);
$meta = $wrapped->getMetadata();
// Filter embedded documents
if (isset($meta->isEmbeddedDocument) && $meta->isEmbeddedDocument) {
return;
}
if ($config = $this->getConfiguration($om, $meta->name)) {
$logEntryClass = $this->getLogEntryClass($ea, $meta->name);
$logEntryMeta = $om->getClassMetadata($logEntryClass);
/** @var \Gedmo\Loggable\Entity\LogEntry $logEntry */
$logEntry = $logEntryMeta->newInstance();
$logEntry->setAction($action);
$logEntry->setUsername($this->username);
$logEntry->setObjectClass($meta->name);
$logEntry->setLoggedAt();
// check for the availability of the primary key
$uow = $om->getUnitOfWork();
if ($action === self::ACTION_CREATE && $ea->isPostInsertGenerator($meta)) {
$this->pendingLogEntryInserts[spl_object_hash($object)] = $logEntry;
} else {
$logEntry->setObjectId($wrapped->getIdentifier());
}
$newValues = array();
if ($action !== self::ACTION_REMOVE && isset($config['versioned'])) {
$newValues = $this->getObjectChangeSetData($ea, $object, $logEntry);
$logEntry->setData($newValues);
}
if($action === self::ACTION_UPDATE && 0 === count($newValues)) {
return null;
}
$version = 1;
if ($action !== self::ACTION_CREATE) {
$version = $ea->getNewVersion($logEntryMeta, $object);
if (empty($version)) {
// was versioned later
$version = 1;
}
}
$logEntry->setVersion($version);
$this->prePersistLogEntry($logEntry, $object);
$om->persist($logEntry);
$uow->computeChangeSet($logEntryMeta, $logEntry);
return $logEntry;
}
return null;
}
}

View File

@@ -0,0 +1,140 @@
<?php
namespace Gedmo\Loggable\Mapping\Driver;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
/**
* This is an annotation mapping driver for Loggable
* behavioral extension. Used for extraction of extended
* metadata from Annotations specifically for Loggable
* extension.
*
* @author Boussekeyt Jules <jules.boussekeyt@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 LOGGABLE = 'Gedmo\\Mapping\\Annotation\\Loggable';
/**
* Annotation to define that this property is versioned
*/
const VERSIONED = 'Gedmo\\Mapping\\Annotation\\Versioned';
/**
* {@inheritDoc}
*/
public function validateFullMetadata(ClassMetadata $meta, array $config)
{
if ($config && is_array($meta->identifier) && count($meta->identifier) > 1) {
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
}
if (isset($config['versioned']) && !isset($config['loggable'])) {
throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->name}");
}
}
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$class = $this->getMetaReflectionClass($meta);
// class annotations
if ($annot = $this->reader->getClassAnnotation($class, self::LOGGABLE)) {
$config['loggable'] = true;
if ($annot->logEntryClass) {
if (!$cl = $this->getRelatedClassName($meta, $annot->logEntryClass)) {
throw new InvalidMappingException("LogEntry class: {$annot->logEntryClass} does not exist.");
}
$config['logEntryClass'] = $cl;
}
}
// property annotations
foreach ($class->getProperties() as $property) {
$field = $property->getName();
if ($meta->isMappedSuperclass && !$property->isPrivate()) {
continue;
}
// versioned property
if ($this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
if (!$this->isMappingValid($meta, $field)) {
throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
}
if (isset($meta->embeddedClasses[$field])) {
$this->inspectEmbeddedForVersioned($field, $config, $meta);
continue;
}
// fields cannot be overrided and throws mapping exception
if (!(isset($config['versioned']) && in_array($field, $config['versioned']))) {
$config['versioned'][] = $field;
}
}
}
if (!$meta->isMappedSuperclass && $config) {
if (is_array($meta->identifier) && count($meta->identifier) > 1) {
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
}
if ($this->isClassAnnotationInValid($meta, $config)) {
throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->name}");
}
}
}
/**
* @param ClassMetadata $meta
* @param string $field
*
* @return bool
*/
protected function isMappingValid(ClassMetadata $meta, $field)
{
return $meta->isCollectionValuedAssociation($field) == false;
}
/**
* @param ClassMetadata $meta
* @param array $config
*
* @return bool
*/
protected function isClassAnnotationInValid(ClassMetadata $meta, array &$config)
{
return isset($config['versioned']) && !isset($config['loggable']) && (!isset($meta->isEmbeddedClass) || !$meta->isEmbeddedClass);
}
/**
* Searches properties of embedded object for versioned fields
*
* @param string $field
* @param array $config
* @param \Doctrine\ORM\Mapping\ClassMetadata $meta
*/
private function inspectEmbeddedForVersioned($field, array &$config, \Doctrine\ORM\Mapping\ClassMetadata $meta)
{
$сlass = new \ReflectionClass($meta->embeddedClasses[$field]['class']);
// property annotations
foreach ($сlass->getProperties() as $property) {
// versioned property
if ($this->reader->getPropertyAnnotation($property, self::VERSIONED)) {
$embeddedField = $field . '.' . $property->getName();
$config['versioned'][] = $embeddedField;
if (isset($meta->embeddedClasses[$embeddedField])) {
$this->inspectEmbeddedForVersioned($embeddedField, $config, $meta);
}
}
}
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace Gedmo\Loggable\Mapping\Driver;
use Gedmo\Mapping\Driver\Xml as BaseXml;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a xml mapping driver for Loggable
* behavioral extension. Used for extraction of extended
* metadata from xml specifically for Loggable
* extension.
*
* @author Boussekeyt Jules <jules.boussekeyt@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 ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'document' || $xmlDoctrine->getName() == 'mapped-superclass') {
if (isset($xml->loggable)) {
/**
* @var \SimpleXMLElement $data;
*/
$data = $xml->loggable;
$config['loggable'] = true;
if ($this->_isAttributeSet($data, 'log-entry-class')) {
$class = $this->_getAttribute($data, 'log-entry-class');
if (!$cl = $this->getRelatedClassName($meta, $class)) {
throw new InvalidMappingException("LogEntry class: {$class} does not exist.");
}
$config['logEntryClass'] = $cl;
}
}
}
if (isset($xmlDoctrine->field)) {
$this->inspectElementForVersioned($xmlDoctrine->field, $config, $meta);
}
if (isset($xmlDoctrine->{'many-to-one'})) {
$this->inspectElementForVersioned($xmlDoctrine->{'many-to-one'}, $config, $meta);
}
if (isset($xmlDoctrine->{'one-to-one'})) {
$this->inspectElementForVersioned($xmlDoctrine->{'one-to-one'}, $config, $meta);
}
if (isset($xmlDoctrine->{'reference-one'})) {
$this->inspectElementForVersioned($xmlDoctrine->{'reference-one'}, $config, $meta);
}
if (isset($xmlDoctrine->{'embedded'})) {
$this->inspectElementForVersioned($xmlDoctrine->{'embedded'}, $config, $meta);
}
if (!$meta->isMappedSuperclass && $config) {
if (is_array($meta->identifier) && count($meta->identifier) > 1) {
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
}
if (isset($config['versioned']) && !isset($config['loggable'])) {
throw new InvalidMappingException("Class must be annotated with Loggable annotation in order to track versioned fields in class - {$meta->name}");
}
}
}
/**
* Searches mappings on element for versioned fields
*
* @param \SimpleXMLElement $element
* @param array $config
* @param object $meta
*/
private function inspectElementForVersioned(\SimpleXMLElement $element, array &$config, $meta)
{
foreach ($element as $mapping) {
$mappingDoctrine = $mapping;
/**
* @var \SimpleXmlElement $mapping
*/
$mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
$isAssoc = $this->_isAttributeSet($mappingDoctrine, 'field');
$field = $this->_getAttribute($mappingDoctrine, $isAssoc ? 'field' : 'name');
if (isset($mapping->versioned)) {
if ($isAssoc && !$meta->associationMappings[$field]['isOwningSide']) {
throw new InvalidMappingException("Cannot version [{$field}] as it is not the owning side in object - {$meta->name}");
}
$config['versioned'][] = $field;
}
}
}
}

View File

@@ -0,0 +1,149 @@
<?php
namespace Gedmo\Loggable\Mapping\Driver;
use Gedmo\Mapping\Driver\File;
use Gedmo\Mapping\Driver;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a yaml mapping driver for Loggable
* behavioral extension. Used for extraction of extended
* metadata from yaml specifically for Loggable
* extension.
*
* @author Boussekeyt Jules <jules.boussekeyt@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['loggable'])) {
$config['loggable'] = true;
if (isset ($classMapping['loggable']['logEntryClass'])) {
if (!$cl = $this->getRelatedClassName($meta, $classMapping['loggable']['logEntryClass'])) {
throw new InvalidMappingException("LogEntry class: {$classMapping['loggable']['logEntryClass']} does not exist.");
}
$config['logEntryClass'] = $cl;
}
}
}
if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo'])) {
if (in_array('versioned', $fieldMapping['gedmo'])) {
if ($meta->isCollectionValuedAssociation($field)) {
throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
}
// fields cannot be overrided and throws mapping exception
$config['versioned'][] = $field;
}
}
}
}
if (isset($mapping['attributeOverride'])) {
foreach ($mapping['attributeOverride'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo'])) {
if (in_array('versioned', $fieldMapping['gedmo'])) {
if ($meta->isCollectionValuedAssociation($field)) {
throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
}
// fields cannot be overrided and throws mapping exception
$config['versioned'][] = $field;
}
}
}
}
if (isset($mapping['manyToOne'])) {
foreach ($mapping['manyToOne'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo'])) {
if (in_array('versioned', $fieldMapping['gedmo'])) {
if ($meta->isCollectionValuedAssociation($field)) {
throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
}
// fields cannot be overrided and throws mapping exception
$config['versioned'][] = $field;
}
}
}
}
if (isset($mapping['oneToOne'])) {
foreach ($mapping['oneToOne'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo'])) {
if (in_array('versioned', $fieldMapping['gedmo'])) {
if ($meta->isCollectionValuedAssociation($field)) {
throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
}
// fields cannot be overrided and throws mapping exception
$config['versioned'][] = $field;
}
}
}
}
if (isset($mapping['embedded'])) {
foreach ($mapping['embedded'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo'])) {
if (in_array('versioned', $fieldMapping['gedmo'])) {
if ($meta->isCollectionValuedAssociation($field)) {
throw new InvalidMappingException("Cannot apply versioning to field [{$field}] as it is collection in object - {$meta->name}");
}
// fields cannot be overrided and throws mapping exception
$mapping = $this->_getMapping($fieldMapping['class']);
$this->inspectEmbeddedForVersioned($field, $mapping, $config);
}
}
}
}
if (!$meta->isMappedSuperclass && $config) {
if (is_array($meta->identifier) && count($meta->identifier) > 1) {
throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
}
if (isset($config['versioned']) && !isset($config['loggable'])) {
throw new InvalidMappingException("Class must be annoted with Loggable annotation in order to track versioned fields in class - {$meta->name}");
}
}
}
/**
* {@inheritDoc}
*/
protected function _loadMappingFile($file)
{
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
}
/**
* @param string $field
* @param array $mapping
* @param array $config
*/
private function inspectEmbeddedForVersioned($field, array $mapping, array &$config)
{
if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $property => $fieldMapping) {
$config['versioned'][] = $field . '.' . $property;
}
}
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Gedmo\Loggable\Mapping\Event\Adapter;
use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
/**
* Doctrine event adapter for ODM adapted
* for Loggable behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ODM extends BaseAdapterODM implements LoggableAdapter
{
/**
* {@inheritDoc}
*/
public function getDefaultLogEntryClass()
{
return 'Gedmo\\Loggable\\Document\\LogEntry';
}
/**
* {@inheritDoc}
*/
public function isPostInsertGenerator($meta)
{
return false;
}
/**
* {@inheritDoc}
*/
public function getNewVersion($meta, $object)
{
$dm = $this->getObjectManager();
$objectMeta = $dm->getClassMetadata(get_class($object));
$identifierField = $this->getSingleIdentifierFieldName($objectMeta);
$objectId = $objectMeta->getReflectionProperty($identifierField)->getValue($object);
$qb = $dm->createQueryBuilder($meta->name);
$qb->select('version');
$qb->field('objectId')->equals($objectId);
$qb->field('objectClass')->equals($objectMeta->name);
$qb->sort('version', 'DESC');
$qb->limit(1);
$q = $qb->getQuery();
$q->setHydrate(false);
$result = $q->getSingleResult();
if ($result) {
$result = $result['version'] + 1;
}
return $result;
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Gedmo\Loggable\Mapping\Event\Adapter;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\Loggable\Mapping\Event\LoggableAdapter;
/**
* Doctrine event adapter for ORM adapted
* for Loggable behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ORM extends BaseAdapterORM implements LoggableAdapter
{
/**
* {@inheritDoc}
*/
public function getDefaultLogEntryClass()
{
return 'Gedmo\\Loggable\\Entity\\LogEntry';
}
/**
* {@inheritDoc}
*/
public function isPostInsertGenerator($meta)
{
return $meta->idGenerator->isPostInsertGenerator();
}
/**
* {@inheritDoc}
*/
public function getNewVersion($meta, $object)
{
$em = $this->getObjectManager();
$objectMeta = $em->getClassMetadata(get_class($object));
$identifierField = $this->getSingleIdentifierFieldName($objectMeta);
$objectId = (string) $objectMeta->getReflectionProperty($identifierField)->getValue($object);
$dql = "SELECT MAX(log.version) FROM {$meta->name} log";
$dql .= " WHERE log.objectId = :objectId";
$dql .= " AND log.objectClass = :objectClass";
$q = $em->createQuery($dql);
$q->setParameters(array(
'objectId' => $objectId,
'objectClass' => $objectMeta->name,
));
return $q->getSingleScalarResult() + 1;
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace Gedmo\Loggable\Mapping\Event;
use Gedmo\Mapping\Event\AdapterInterface;
/**
* Doctrine event adapter interface
* for Loggable behavior
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface LoggableAdapter extends AdapterInterface
{
/**
* Get default LogEntry class used to store the logs
*
* @return string
*/
public function getDefaultLogEntryClass();
/**
* Checks whether an id should be generated post insert
*
* @return boolean
*/
public function isPostInsertGenerator($meta);
/**
* Get new version number
*
* @param object $meta
* @param object $object
*
* @return integer
*/
public function getNewVersion($meta, $object);
}