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,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);
}