Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 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;
}
}