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,227 @@
<?php
namespace Gedmo;
use Doctrine\Common\EventArgs;
use Doctrine\Common\NotifyPropertyChanged;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\UnitOfWork;
use Gedmo\Exception\UnexpectedValueException;
use Gedmo\Mapping\Event\AdapterInterface;
use Gedmo\Mapping\MappedEventSubscriber;
/**
* The Timestampable listener handles the update of
* dates on creation and update.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
abstract class AbstractTrackingListener extends MappedEventSubscriber
{
/**
* Specifies the list of events to listen
*
* @return array
*/
public function getSubscribedEvents()
{
return array(
'prePersist',
'onFlush',
'loadClassMetadata',
);
}
/**
* Maps additional metadata for the Entity
*
* @param EventArgs $eventArgs
*
* @return void
*/
public function loadClassMetadata(EventArgs $eventArgs)
{
$ea = $this->getEventAdapter($eventArgs);
$this->loadMetadataForObjectClass($ea->getObjectManager(), $eventArgs->getClassMetadata());
}
/**
* Looks for Timestampable objects being updated
* to update modification date
*
* @param EventArgs $args
*
* @return void
*/
public function onFlush(EventArgs $args)
{
$ea = $this->getEventAdapter($args);
$om = $ea->getObjectManager();
$uow = $om->getUnitOfWork();
// check all scheduled updates
$all = array_merge($ea->getScheduledObjectInsertions($uow), $ea->getScheduledObjectUpdates($uow));
foreach ($all as $object) {
$meta = $om->getClassMetadata(get_class($object));
if (!$config = $this->getConfiguration($om, $meta->name)) {
continue;
}
$changeSet = $ea->getObjectChangeSet($uow, $object);
$needChanges = false;
if ($uow->isScheduledForInsert($object) && isset($config['create'])) {
foreach ($config['create'] as $field) {
// Field can not exist in change set, when persisting embedded document without parent for example
$new = array_key_exists($field, $changeSet) ? $changeSet[$field][1] : false;
if ($new === null) { // let manual values
$needChanges = true;
$this->updateField($object, $ea, $meta, $field);
}
}
}
if (isset($config['update'])) {
foreach ($config['update'] as $field) {
$isInsertAndNull = $uow->isScheduledForInsert($object)
&& array_key_exists($field, $changeSet)
&& $changeSet[$field][1] === null;
if (!isset($changeSet[$field]) || $isInsertAndNull) { // let manual values
$needChanges = true;
$this->updateField($object, $ea, $meta, $field);
}
}
}
if (!$uow->isScheduledForInsert($object) && isset($config['change'])) {
foreach ($config['change'] as $options) {
if (isset($changeSet[$options['field']])) {
continue; // value was set manually
}
if (!is_array($options['trackedField'])) {
$singleField = true;
$trackedFields = array($options['trackedField']);
} else {
$singleField = false;
$trackedFields = $options['trackedField'];
}
foreach ($trackedFields as $trackedField) {
$trackedChild = null;
$tracked = null;
$parts = explode('.', $trackedField);
if (isset($parts[1])) {
$tracked = $parts[0];
$trackedChild = $parts[1];
}
if (!isset($tracked) || array_key_exists($trackedField, $changeSet)) {
$tracked = $trackedField;
$trackedChild = null;
}
if (isset($changeSet[$tracked])) {
$changes = $changeSet[$tracked];
if (isset($trackedChild)) {
$changingObject = $changes[1];
if (!is_object($changingObject)) {
throw new UnexpectedValueException(
"Field - [{$tracked}] is expected to be object in class - {$meta->name}"
);
}
$objectMeta = $om->getClassMetadata(get_class($changingObject));
$om->initializeObject($changingObject);
$value = $objectMeta->getReflectionProperty($trackedChild)->getValue($changingObject);
} else {
$value = $changes[1];
}
if (($singleField && in_array($value, (array) $options['value'])) || $options['value'] === null) {
$needChanges = true;
$this->updateField($object, $ea, $meta, $options['field']);
}
}
}
}
}
if ($needChanges) {
$ea->recomputeSingleObjectChangeSet($uow, $meta, $object);
}
}
}
/**
* Checks for persisted Timestampable objects
* to update creation and modification dates
*
* @param EventArgs $args
*
* @return void
*/
public function prePersist(EventArgs $args)
{
$ea = $this->getEventAdapter($args);
$om = $ea->getObjectManager();
$object = $ea->getObject();
$meta = $om->getClassMetadata(get_class($object));
if ($config = $this->getConfiguration($om, $meta->getName())) {
if (isset($config['update'])) {
foreach ($config['update'] as $field) {
if ($meta->getReflectionProperty($field)->getValue($object) === null) { // let manual values
$this->updateField($object, $ea, $meta, $field);
}
}
}
if (isset($config['create'])) {
foreach ($config['create'] as $field) {
if ($meta->getReflectionProperty($field)->getValue($object) === null) { // let manual values
$this->updateField($object, $ea, $meta, $field);
}
}
}
}
}
/**
* Get value for update field
*
* @param ClassMetadata $meta
* @param string $field
* @param AdapterInterface $eventAdapter
*/
abstract protected function getFieldValue($meta, $field, $eventAdapter);
/**
* Updates a field
*
* @param object $object
* @param AdapterInterface $eventAdapter
* @param ClassMetadata $meta
* @param string $field
*/
protected function updateField($object, $eventAdapter, $meta, $field)
{
/** @var \Doctrine\Orm\Mapping\ClassMetadata|\Doctrine\ODM\MongoDB\Mapping\ClassMetadata $meta */
$property = $meta->getReflectionProperty($field);
$oldValue = $property->getValue($object);
$newValue = $this->getFieldValue($meta, $field, $eventAdapter);
// if field value is reference, persist object
if ($meta->hasAssociation($field) && is_object($newValue) && !$eventAdapter->getObjectManager()->contains($newValue)) {
$uow = $eventAdapter->getObjectManager()->getUnitOfWork();
// Check to persist only when the entity isn't already managed, persists always for MongoDB
if(!($uow instanceof UnitOfWork) || $uow->getEntityState($newValue) !== UnitOfWork::STATE_MANAGED) {
$eventAdapter->getObjectManager()->persist($newValue);
}
}
$property->setValue($object, $newValue);
if ($object instanceof NotifyPropertyChanged) {
$uow = $eventAdapter->getObjectManager()->getUnitOfWork();
$uow->propertyChanged($object, $field, $oldValue, $newValue);
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Gedmo\Blameable;
/**
* This interface is not necessary but can be implemented for
* Entities which in some cases needs to be identified as
* Blameable
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface Blameable
{
// blameable expects annotations on properties
/**
* @gedmo:Blameable(on="create")
* fields which should be updated on insert only
*/
/**
* @gedmo:Blameable(on="update")
* fields which should be updated on update and insert
*/
/**
* @gedmo:Blameable(on="change", field="field", value="value")
* fields which should be updated on changed "property"
* value and become equal to given "value"
*/
/**
* @gedmo:Blameable(on="change", field="field")
* fields which should be updated on changed "property"
*/
/**
* @gedmo:Blameable(on="change", fields={"field1", "field2"})
* fields which should be updated if at least one of the given fields changed
*/
/**
* example
*
* @gedmo:Blameable(on="create")
* @Column(type="string")
* $created
*/
}

View File

@@ -0,0 +1,72 @@
<?php
namespace Gedmo\Blameable;
use Doctrine\Common\NotifyPropertyChanged;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Gedmo\AbstractTrackingListener;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Timestampable\TimestampableListener;
use Gedmo\Blameable\Mapping\Event\BlameableAdapter;
/**
* The Blameable listener handles the update of
* dates on creation and update.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class BlameableListener extends AbstractTrackingListener
{
protected $user;
/**
* Get the user value to set on a blameable field
*
* @param object $meta
* @param string $field
*
* @return mixed
*/
public function getFieldValue($meta, $field, $eventAdapter)
{
if ($meta->hasAssociation($field)) {
if (null !== $this->user && ! is_object($this->user)) {
throw new InvalidArgumentException("Blame is reference, user must be an object");
}
return $this->user;
}
// ok so its not an association, then it is a string
if (is_object($this->user)) {
if (method_exists($this->user, 'getUsername')) {
return (string) $this->user->getUsername();
}
if (method_exists($this->user, '__toString')) {
return $this->user->__toString();
}
throw new InvalidArgumentException("Field expects string, user must be a string, or object should have method getUsername or __toString");
}
return $this->user;
}
/**
* Set a user value to return
*
* @param mixed $user
*/
public function setUserValue($user)
{
$this->user = $user;
}
/**
* {@inheritDoc}
*/
protected function getNamespace()
{
return __NAMESPACE__;
}
}

View File

@@ -0,0 +1,86 @@
<?php
namespace Gedmo\Blameable\Mapping\Driver;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
use Gedmo\Exception\InvalidMappingException;
/**
* This is an annotation mapping driver for Blameable
* behavioral extension. Used for extraction of extended
* metadata from Annotations specifically for Blameable
* extension.
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Annotation extends AbstractAnnotationDriver
{
/**
* Annotation field is blameable
*/
const BLAMEABLE = 'Gedmo\\Mapping\\Annotation\\Blameable';
/**
* List of types which are valid for blame
*
* @var array
*/
protected $validTypes = array(
'one',
'string',
'int',
);
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$class = $this->getMetaReflectionClass($meta);
// property annotations
foreach ($class->getProperties() as $property) {
if ($meta->isMappedSuperclass && !$property->isPrivate() ||
$meta->isInheritedField($property->name) ||
isset($meta->associationMappings[$property->name]['inherited'])
) {
continue;
}
if ($blameable = $this->reader->getPropertyAnnotation($property, self::BLAMEABLE)) {
$field = $property->getName();
if (!$meta->hasField($field) && !$meta->hasAssociation($field)) {
throw new InvalidMappingException("Unable to find blameable [{$field}] as mapped property in entity - {$meta->name}");
}
if ($meta->hasField($field)) {
if ( !$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a one-to-many relation in class - {$meta->name}");
}
} else {
// association
if (! $meta->isSingleValuedAssociation($field)) {
throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
}
}
if (!in_array($blameable->on, array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($blameable->on == 'change') {
if (!isset($blameable->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
if (is_array($blameable->field) && isset($blameable->value)) {
throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $blameable->field,
'value' => $blameable->value,
);
}
// properties are unique and mapper checks that, no risk here
$config[$blameable->on][] = $field;
}
}
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace Gedmo\Blameable\Mapping\Driver;
use Gedmo\Mapping\Driver\Xml as BaseXml;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a xml mapping driver for Blameable
* behavioral extension. Used for extraction of extended
* metadata from xml specifically for Blameable
* extension.
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Xml extends BaseXml
{
/**
* List of types which are valid for blame
*
* @var array
*/
private $validTypes = array(
'one',
'string',
'int',
);
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
/**
* @var \SimpleXmlElement $mapping
*/
$mapping = $this->_getMapping($meta->name);
if (isset($mapping->field)) {
/**
* @var \SimpleXmlElement $fieldMapping
*/
foreach ($mapping->field as $fieldMapping) {
$fieldMappingDoctrine = $fieldMapping;
$fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
if (isset($fieldMapping->blameable)) {
/**
* @var \SimpleXmlElement $data
*/
$data = $fieldMapping->blameable;
$field = $this->_getAttribute($fieldMappingDoctrine, 'name');
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a reference in class - {$meta->name}");
}
if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($this->_getAttribute($data, 'on') == 'change') {
if (!$this->_isAttributeSet($data, 'field')) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $this->_getAttribute($data, 'field');
$valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$this->_getAttribute($data, 'on')][] = $field;
}
}
}
if (isset($mapping->{'many-to-one'})) {
foreach ($mapping->{'many-to-one'} as $fieldMapping) {
$field = $this->_getAttribute($fieldMapping, 'field');
$fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
if (isset($fieldMapping->blameable)) {
$data = $fieldMapping->blameable;
if (! $meta->isSingleValuedAssociation($field)) {
throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
}
if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($this->_getAttribute($data, 'on') == 'change') {
if (!$this->_isAttributeSet($data, 'field')) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $this->_getAttribute($data, 'field');
$valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$this->_getAttribute($data, 'on')][] = $field;
}
}
}
}
/**
* Checks if $field type is valid
*
* @param object $meta
* @param string $field
*
* @return boolean
*/
protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $mapping && in_array($mapping['type'], $this->validTypes);
}
}

View File

@@ -0,0 +1,129 @@
<?php
namespace Gedmo\Blameable\Mapping\Driver;
use Gedmo\Mapping\Driver\File;
use Gedmo\Mapping\Driver;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a yaml mapping driver for Blameable
* behavioral extension. Used for extraction of extended
* metadata from yaml specifically for Blameable
* extension.
*
* @author David Buchmann <mail@davidbu.ch>
* @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';
/**
* List of types which are valid for blameable
*
* @var array
*/
private $validTypes = array(
'one',
'string',
'int',
);
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$mapping = $this->_getMapping($meta->name);
if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo']['blameable'])) {
$mappingProperty = $fieldMapping['gedmo']['blameable'];
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a reference in class - {$meta->name}");
}
if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($mappingProperty['on'] == 'change') {
if (!isset($mappingProperty['field'])) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $mappingProperty['field'];
$valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$mappingProperty['on']][] = $field;
}
}
}
if (isset($mapping['manyToOne'])) {
foreach ($mapping['manyToOne'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo']['blameable'])) {
$mappingProperty = $fieldMapping['gedmo']['blameable'];
if (! $meta->isSingleValuedAssociation($field)) {
throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
}
if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($mappingProperty['on'] == 'change') {
if (!isset($mappingProperty['field'])) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $mappingProperty['field'];
$valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$mappingProperty['on']][] = $field;
}
}
}
}
/**
* {@inheritDoc}
*/
protected function _loadMappingFile($file)
{
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
}
/**
* Checks if $field type is valid
*
* @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $meta
* @param string $field
*
* @return boolean
*/
protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $mapping && in_array($mapping['type'], $this->validTypes);
}
}

View File

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

View File

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

View File

@@ -0,0 +1,16 @@
<?php
namespace Gedmo\Blameable\Mapping\Event;
use Gedmo\Mapping\Event\AdapterInterface;
/**
* Doctrine event adapter interface
* for Blameable behavior.
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface BlameableAdapter extends AdapterInterface
{
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Gedmo\Blameable\Traits;
/**
* Blameable Trait, usable with PHP >= 5.4
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait Blameable
{
/**
* @var string
*/
private $createdBy;
/**
* @var string
*/
private $updatedBy;
/**
* Sets createdBy.
*
* @param string $createdBy
* @return $this
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* Returns createdBy.
*
* @return string
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* Sets updatedBy.
*
* @param string $updatedBy
* @return $this
*/
public function setUpdatedBy($updatedBy)
{
$this->updatedBy = $updatedBy;
return $this;
}
/**
* Returns updatedBy.
*
* @return string
*/
public function getUpdatedBy()
{
return $this->updatedBy;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Gedmo\Blameable\Traits;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Blameable Trait, usable with PHP >= 5.4
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait BlameableDocument
{
/**
* @var string
* @Gedmo\Blameable(on="create")
* @ODM\Field(type="string")
*/
protected $createdBy;
/**
* @var string
* @Gedmo\Blameable(on="update")
* @ODM\Field(type="string")
*/
protected $updatedBy;
/**
* Sets createdBy.
*
* @param string $createdBy
* @return $this
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* Returns createdBy.
*
* @return string
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* Sets updatedBy.
*
* @param string $updatedBy
* @return $this
*/
public function setUpdatedBy($updatedBy)
{
$this->updatedBy = $updatedBy;
return $this;
}
/**
* Returns updatedBy.
*
* @return string
*/
public function getUpdatedBy()
{
return $this->updatedBy;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Gedmo\Blameable\Traits;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Blameable Trait, usable with PHP >= 5.4
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait BlameableEntity
{
/**
* @var string
* @Gedmo\Blameable(on="create")
* @ORM\Column(nullable=true)
*/
protected $createdBy;
/**
* @var string
* @Gedmo\Blameable(on="update")
* @ORM\Column(nullable=true)
*/
protected $updatedBy;
/**
* Sets createdBy.
*
* @param string $createdBy
* @return $this
*/
public function setCreatedBy($createdBy)
{
$this->createdBy = $createdBy;
return $this;
}
/**
* Returns createdBy.
*
* @return string
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* Sets updatedBy.
*
* @param string $updatedBy
* @return $this
*/
public function setUpdatedBy($updatedBy)
{
$this->updatedBy = $updatedBy;
return $this;
}
/**
* Returns updatedBy.
*
* @return string
*/
public function getUpdatedBy()
{
return $this->updatedBy;
}
}

View File

@@ -0,0 +1,117 @@
<?php
namespace Gedmo;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ORM\Mapping\Driver as DriverORM;
use Doctrine\ODM\MongoDB\Mapping\Driver as DriverMongodbODM;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Cache\ArrayCache;
/**
* Version class allows to checking the dependencies required
* and the current version of doctrine extensions
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class DoctrineExtensions
{
/**
* Current version of extensions
*/
const VERSION = 'v2.4.42';
/**
* Hooks all extensions metadata mapping drivers
* into given $driverChain of drivers for ORM
*
* @param MappingDriverChain $driverChain
* @param Reader|null $reader
*/
public static function registerMappingIntoDriverChainORM(MappingDriverChain $driverChain, Reader $reader = null)
{
self::registerAnnotations();
if (!$reader) {
$reader = new CachedReader(new AnnotationReader(), new ArrayCache());
}
$annotationDriver = new DriverORM\AnnotationDriver($reader, array(
__DIR__.'/Translatable/Entity',
__DIR__.'/Loggable/Entity',
__DIR__.'/Tree/Entity',
));
$driverChain->addDriver($annotationDriver, 'Gedmo');
}
/**
* Hooks only superclass metadata mapping drivers
* into given $driverChain of drivers for ORM
*
* @param MappingDriverChain $driverChain
* @param Reader|null $reader
*/
public static function registerAbstractMappingIntoDriverChainORM(MappingDriverChain $driverChain, Reader $reader = null)
{
self::registerAnnotations();
if (!$reader) {
$reader = new CachedReader(new AnnotationReader(), new ArrayCache());
}
$annotationDriver = new DriverORM\AnnotationDriver($reader, array(
__DIR__.'/Translatable/Entity/MappedSuperclass',
__DIR__.'/Loggable/Entity/MappedSuperclass',
__DIR__.'/Tree/Entity/MappedSuperclass',
));
$driverChain->addDriver($annotationDriver, 'Gedmo');
}
/**
* Hooks all extensions metadata mapping drivers
* into given $driverChain of drivers for ODM MongoDB
*
* @param MappingDriverChain $driverChain
* @param Reader|null $reader
*/
public static function registerMappingIntoDriverChainMongodbODM(MappingDriverChain $driverChain, Reader $reader = null)
{
self::registerAnnotations();
if (!$reader) {
$reader = new CachedReader(new AnnotationReader(), new ArrayCache());
}
$annotationDriver = new DriverMongodbODM\AnnotationDriver($reader, array(
__DIR__.'/Translatable/Document',
__DIR__.'/Loggable/Document',
));
$driverChain->addDriver($annotationDriver, 'Gedmo');
}
/**
* Hooks only superclass metadata mapping drivers
* into given $driverChain of drivers for ODM MongoDB
*
* @param MappingDriverChain $driverChain
* @param Reader|null $reader
*/
public static function registerAbstractMappingIntoDriverChainMongodbODM(MappingDriverChain $driverChain, Reader $reader = null)
{
self::registerAnnotations();
if (!$reader) {
$reader = new CachedReader(new AnnotationReader(), new ArrayCache());
}
$annotationDriver = new DriverMongodbODM\AnnotationDriver($reader, array(
__DIR__.'/Translatable/Document/MappedSuperclass',
__DIR__.'/Loggable/Document/MappedSuperclass',
));
$driverChain->addDriver($annotationDriver, 'Gedmo');
}
/**
* Includes all extension annotations once
*/
public static function registerAnnotations()
{
AnnotationRegistry::registerFile(__DIR__.'/Mapping/Annotation/All.php');
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Gedmo;
/**
* Common package exception interface to allow
* users of caching only this package specific
* exceptions thrown
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface Exception
{
/**
* Following best practices for PHP5.3 package exceptions.
* All exceptions thrown in this package will have to implement this interface
*/
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* BadMethodCallException
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class BadMethodCallException
extends \BadMethodCallException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* FeatureNotImplementedException
*
* @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 FeatureNotImplementedException
extends \RuntimeException
implements Exception
{
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* InvalidArgumentException
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class InvalidArgumentException
extends \InvalidArgumentException
implements Exception
{
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* InvalidMappingException
*
* Triggered when mapping user argument is not
* valid or incomplete.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class InvalidMappingException
extends InvalidArgumentException
implements Exception
{
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* ReferenceIntegrityStrictException
*
* @author Evert Harmeling <evert.harmeling@freshheads.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class ReferenceIntegrityStrictException extends RuntimeException
{
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* RuntimeException
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class RuntimeException
extends \RuntimeException
implements Exception
{
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* TreeLockingException
*
* @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 TreeLockingException extends RuntimeException
{
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UnexpectedValueException
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class UnexpectedValueException
extends \UnexpectedValueException
implements Exception
{
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UnsupportedObjectManager
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class UnsupportedObjectManagerException
extends InvalidArgumentException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableCantWriteException
*
* @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 UploadableCantWriteException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableCouldntGuessMimeTypeException
*
* @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 UploadableCouldntGuessMimeTypeException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableDirectoryNotFoundException
*
* @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 UploadableDirectoryNotFoundException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableException
*
* @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 UploadableException
extends RuntimeException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableExtensionException
*
* @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 UploadableExtensionException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableFileAlreadyExistsException
*
* @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 UploadableFileAlreadyExistsException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableFileNotReadableException
*
* @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 UploadableFileNotReadableException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableFormSizeException
*
* @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 UploadableFormSizeException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableIniSizeException
*
* @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 UploadableIniSizeException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableInvalidFileException
*
* @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 UploadableInvalidFileException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableInvalidMimeTypeException
*
* @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 UploadableInvalidMimeTypeException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableInvalidPathException
*
* @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 UploadableInvalidPathException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableMaxSizeException
*
* @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 UploadableMaxSizeException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableNoFileException
*
* @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 UploadableNoFileException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableNoPathDefinedException
*
* @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 UploadableNoPathDefinedException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableNoTmpDirException
*
* @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 UploadableNoTmpDirException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadablePartialException
*
* @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 UploadablePartialException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Exception;
use Gedmo\Exception;
/**
* UploadableUploadException
*
* @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 UploadableUploadException
extends UploadableException
implements Exception
{
}

View File

@@ -0,0 +1,50 @@
<?php
namespace Gedmo\IpTraceable;
/**
* This interface is not necessary but can be implemented for
* Entities which in some cases needs to be identified as
* IpTraceable
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface IpTraceable
{
// ipTraceable expects annotations on properties
/**
* @gedmo:IpTraceable(on="create")
* strings which should be updated on insert only
*/
/**
* @gedmo:IpTraceable(on="update")
* strings which should be updated on update and insert
*/
/**
* @gedmo:IpTraceable(on="change", field="field", value="value")
* strings which should be updated on changed "property"
* value and become equal to given "value"
*/
/**
* @gedmo:IpTraceable(on="change", field="field")
* strings which should be updated on changed "property"
*/
/**
* @gedmo:IpTraceable(on="change", fields={"field1", "field2"})
* strings which should be updated if at least one of the given fields changed
*/
/**
* example
*
* @gedmo:IpTraceable(on="create")
* @Column(type="string")
* $created
*/
}

View File

@@ -0,0 +1,59 @@
<?php
namespace Gedmo\IpTraceable;
use Gedmo\AbstractTrackingListener;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Mapping\Event\AdapterInterface;
/**
* The IpTraceable listener handles the update of
* IPs on creation and update.
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class IpTraceableListener extends AbstractTrackingListener
{
/**
* @var string|null
*/
protected $ip;
/**
* Get the ipValue value to set on a ip field
*
* @param object $meta
* @param string $field
* @param AdapterInterface $eventAdapter
*
* @return null|string
*/
public function getFieldValue($meta, $field, $eventAdapter)
{
return $this->ip;
}
/**
* Set a ip value to return
*
* @param string $ip
* @throws InvalidArgumentException
*/
public function setIpValue($ip = null)
{
if (isset($ip) && filter_var($ip, FILTER_VALIDATE_IP) === false) {
throw new InvalidArgumentException("ip address is not valid $ip");
}
$this->ip = $ip;
}
/**
* {@inheritDoc}
*/
protected function getNamespace()
{
return __NAMESPACE__;
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Gedmo\IpTraceable\Mapping\Driver;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
use Gedmo\Exception\InvalidMappingException;
/**
* This is an annotation mapping driver for IpTraceable
* behavioral extension. Used for extraction of extended
* metadata from Annotations specifically for IpTraceable
* extension.
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Annotation extends AbstractAnnotationDriver
{
/**
* Annotation field is ipTraceable
*/
const IP_TRACEABLE = 'Gedmo\\Mapping\\Annotation\\IpTraceable';
/**
* List of types which are valid for IP
*
* @var array
*/
protected $validTypes = array(
'string',
);
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$class = $this->getMetaReflectionClass($meta);
// property annotations
foreach ($class->getProperties() as $property) {
if ($meta->isMappedSuperclass && !$property->isPrivate() ||
$meta->isInheritedField($property->name) ||
isset($meta->associationMappings[$property->name]['inherited'])
) {
continue;
}
if ($ipTraceable = $this->reader->getPropertyAnnotation($property, self::IP_TRACEABLE)) {
$field = $property->getName();
if (!$meta->hasField($field)) {
throw new InvalidMappingException("Unable to find ipTraceable [{$field}] as mapped property in entity - {$meta->name}");
}
if ($meta->hasField($field) && !$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' - {$meta->name}");
}
if (!in_array($ipTraceable->on, array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($ipTraceable->on == 'change') {
if (!isset($ipTraceable->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
if (is_array($ipTraceable->field) && isset($ipTraceable->value)) {
throw new InvalidMappingException("IpTraceable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $ipTraceable->field,
'value' => $ipTraceable->value,
);
}
// properties are unique and mapper checks that, no risk here
$config[$ipTraceable->on][] = $field;
}
}
}
}

View File

@@ -0,0 +1,133 @@
<?php
namespace Gedmo\IpTraceable\Mapping\Driver;
use Gedmo\Mapping\Driver\Xml as BaseXml;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a xml mapping driver for IpTraceable
* behavioral extension. Used for extraction of extended
* metadata from xml specifically for IpTraceable
* extension.
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class Xml extends BaseXml
{
/**
* List of types which are valid for IP
*
* @var array
*/
private $validTypes = array(
'string',
);
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
/**
* @var \SimpleXmlElement $mapping
*/
$mapping = $this->_getMapping($meta->name);
if (isset($mapping->field)) {
/**
* @var \SimpleXmlElement $fieldMapping
*/
foreach ($mapping->field as $fieldMapping) {
$fieldMappingDoctrine = $fieldMapping;
$fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
if (isset($fieldMapping->{'ip-traceable'})) {
/**
* @var \SimpleXmlElement $data
*/
$data = $fieldMapping->{'ip-traceable'};
$field = $this->_getAttribute($fieldMappingDoctrine, 'name');
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
}
if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($this->_getAttribute($data, 'on') == 'change') {
if (!$this->_isAttributeSet($data, 'field')) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $this->_getAttribute($data, 'field');
$valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("IpTraceable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$this->_getAttribute($data, 'on')][] = $field;
}
}
}
if (isset($mapping->{'many-to-one'})) {
foreach ($mapping->{'many-to-one'} as $fieldMapping) {
$field = $this->_getAttribute($fieldMapping, 'field');
$fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
if (isset($fieldMapping->{'ip-traceable'})) {
/**
* @var \SimpleXmlElement $data
*/
$data = $fieldMapping->{'ip-traceable'};
if (! $meta->isSingleValuedAssociation($field)) {
throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
}
if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($this->_getAttribute($data, 'on') == 'change') {
if (!$this->_isAttributeSet($data, 'field')) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $this->_getAttribute($data, 'field');
$valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("IpTraceable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$this->_getAttribute($data, 'on')][] = $field;
}
}
}
}
/**
* Checks if $field type is valid
*
* @param object $meta
* @param string $field
*
* @return boolean
*/
protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $mapping && in_array($mapping['type'], $this->validTypes);
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace Gedmo\IpTraceable\Mapping\Driver;
use Gedmo\Mapping\Driver\File;
use Gedmo\Mapping\Driver;
use Gedmo\Exception\InvalidMappingException;
/**
* This is a yaml mapping driver for IpTraceable
* behavioral extension. Used for extraction of extended
* metadata from yaml specifically for IpTraceable
* extension.
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.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';
/**
* List of types which are valid for IP
*
* @var array
*/
private $validTypes = array(
'string',
);
/**
* {@inheritDoc}
*/
public function readExtendedMetadata($meta, array &$config)
{
$mapping = $this->_getMapping($meta->name);
if (isset($mapping['fields'])) {
foreach ($mapping['fields'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo']['ipTraceable'])) {
$mappingProperty = $fieldMapping['gedmo']['ipTraceable'];
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
}
if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($mappingProperty['on'] == 'change') {
if (!isset($mappingProperty['field'])) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $mappingProperty['field'];
$valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("IpTraceable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$mappingProperty['on']][] = $field;
}
}
}
if (isset($mapping['manyToOne'])) {
foreach ($mapping['manyToOne'] as $field => $fieldMapping) {
if (isset($fieldMapping['gedmo']['ipTraceable'])) {
$mappingProperty = $fieldMapping['gedmo']['ipTraceable'];
if (! $meta->isSingleValuedAssociation($field)) {
throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
}
if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
}
if ($mappingProperty['on'] == 'change') {
if (!isset($mappingProperty['field'])) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
}
$trackedFieldAttribute = $mappingProperty['field'];
$valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
throw new InvalidMappingException("IpTraceable extension does not support multiple value changeset detection yet.");
}
$field = array(
'field' => $field,
'trackedField' => $trackedFieldAttribute,
'value' => $valueAttribute,
);
}
$config[$mappingProperty['on']][] = $field;
}
}
}
}
/**
* {@inheritDoc}
*/
protected function _loadMappingFile($file)
{
return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
}
/**
* Checks if $field type is valid
*
* @param object $meta
* @param string $field
*
* @return boolean
*/
protected function isValidField($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
return $mapping && in_array($mapping['type'], $this->validTypes);
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\IpTraceable\Mapping\Event\Adapter;
use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
use Gedmo\IpTraceable\Mapping\Event\IpTraceableAdapter;
/**
* Doctrine event adapter for ODM adapted
* for IpTraceable behavior
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ODM extends BaseAdapterODM implements IpTraceableAdapter
{
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Gedmo\IpTraceable\Mapping\Event\Adapter;
use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM;
use Gedmo\IpTraceable\Mapping\Event\IpTraceableAdapter;
/**
* Doctrine event adapter for ORM adapted
* for IpTraceable behavior
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ORM extends BaseAdapterORM implements IpTraceableAdapter
{
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Gedmo\IpTraceable\Mapping\Event;
use Gedmo\Mapping\Event\AdapterInterface;
/**
* Doctrine event adapter interface
* for IpTraceable behavior
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
interface IpTraceableAdapter extends AdapterInterface
{
}

View File

@@ -0,0 +1,68 @@
<?php
namespace Gedmo\IpTraceable\Traits;
/**
* IpTraceable Trait, usable with PHP >= 5.4
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait IpTraceable
{
/**
* @var string
*/
protected $createdFromIp;
/**
* @var string
*/
protected $updatedFromIp;
/**
* Sets createdFromIp.
*
* @param string $createdFromIp
* @return $this
*/
public function setCreatedFromIp($createdFromIp)
{
$this->createdFromIp = $createdFromIp;
return $this;
}
/**
* Returns createdFromIp.
*
* @return string
*/
public function getCreatedFromIp()
{
return $this->createdFromIp;
}
/**
* Sets updatedFromIp.
*
* @param string $updatedFromIp
* @return $this
*/
public function setUpdatedFromIp($updatedFromIp)
{
$this->updatedFromIp = $updatedFromIp;
return $this;
}
/**
* Returns updatedFromIp.
*
* @return string
*/
public function getUpdatedFromIp()
{
return $this->updatedFromIp;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Gedmo\IpTraceable\Traits;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* IpTraceable Trait, usable with PHP >= 5.4
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait IpTraceableDocument
{
/**
* @var string
* @Gedmo\IpTraceable(on="create")
* @ODM\Field(type="string")
*/
protected $createdFromIp;
/**
* @var string
* @Gedmo\IpTraceable(on="update")
* @ODM\Field(type="string")
*/
protected $updatedFromIp;
/**
* Sets createdFromIp.
*
* @param string $createdFromIp
* @return $this
*/
public function setCreatedFromIp($createdFromIp)
{
$this->createdFromIp = $createdFromIp;
return $this;
}
/**
* Returns createdFromIp.
*
* @return string
*/
public function getCreatedFromIp()
{
return $this->createdFromIp;
}
/**
* Sets updatedFromIp.
*
* @param string $updatedFromIp
* @return $this
*/
public function setUpdatedFromIp($updatedFromIp)
{
$this->updatedFromIp = $updatedFromIp;
return $this;
}
/**
* Returns updatedFromIp.
*
* @return string
*/
public function getUpdatedFromIp()
{
return $this->updatedFromIp;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Gedmo\IpTraceable\Traits;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* IpTraceable Trait, usable with PHP >= 5.4
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
trait IpTraceableEntity
{
/**
* @var string
* @Gedmo\IpTraceable(on="create")
* @ORM\Column(length=45, nullable=true)
*/
protected $createdFromIp;
/**
* @var string
* @Gedmo\IpTraceable(on="update")
* @ORM\Column(length=45, nullable=true)
*/
protected $updatedFromIp;
/**
* Sets createdFromIp.
*
* @param string $createdFromIp
* @return $this
*/
public function setCreatedFromIp($createdFromIp)
{
$this->createdFromIp = $createdFromIp;
return $this;
}
/**
* Returns createdFromIp.
*
* @return string
*/
public function getCreatedFromIp()
{
return $this->createdFromIp;
}
/**
* Sets updatedFromIp.
*
* @param string $updatedFromIp
* @return $this
*/
public function setUpdatedFromIp($updatedFromIp)
{
$this->updatedFromIp = $updatedFromIp;
return $this;
}
/**
* Returns updatedFromIp.
*
* @return string
*/
public function getUpdatedFromIp()
{
return $this->updatedFromIp;
}
}

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

View File

@@ -0,0 +1,15 @@
<?php
/**
* Contains all annotations for extensions
* NOTE: should be included with require_once
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
foreach (glob(__DIR__ . "/*.php") as $filename) {
if (basename($filename, '.php') === 'All') {
continue;
}
include_once $filename;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Blameable annotation for Blameable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author David Buchmann <mail@davidbu.ch>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Blameable extends Annotation
{
/** @var string */
public $on = 'update';
/** @var string|array */
public $field;
/** @var mixed */
public $value;
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* IpTraceable annotation for IpTraceable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class IpTraceable extends Annotation
{
/** @var string */
public $on = 'update';
/** @var string|array */
public $field;
/** @var mixed */
public $value;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Language annotation for Translatable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Language extends Annotation
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Locale annotation for Translatable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Locale extends Annotation
{
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Loggable annotation for Loggable behavioral extension
*
* @Annotation
* @Target("CLASS")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Loggable extends Annotation
{
/** @var string */
public $logEntryClass;
}

View File

@@ -0,0 +1,22 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Reference annotation for ORM -> ODM references extension
* to be user like "@ReferenceMany(type="entity", class="MyEntity", identifier="entity_id")"
*
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @Annotation
*/
abstract class Reference extends Annotation
{
public $type;
public $class;
public $identifier;
public $mappedBy;
public $inversedBy;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* ReferenceIntegrity annotation for ReferenceIntegrity behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Evert Harmeling <evert.harmeling@freshheads.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class ReferenceIntegrity extends Annotation
{
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Gedmo\Mapping\Annotation;
/**
* Reference annotation for ORM -> ODM references extension
* to be user like "@ReferenceMany(type="entity", class="MyEntity", identifier="entity_id")"
*
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @Annotation
*/
class ReferenceMany extends Reference
{
}

View File

@@ -0,0 +1,10 @@
<?php
namespace Gedmo\Mapping\Annotation;
/**
* @Annotation
*/
class ReferenceManyEmbed extends Reference
{
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Gedmo\Mapping\Annotation;
/**
* Reference annotation for ORM -> ODM references extension
* to be user like "@ReferenceOne(type="entity", class="MyEntity", identifier="entity_id")"
*
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @Annotation
*/
class ReferenceOne extends Reference
{
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Slug annotation for Sluggable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Slug extends Annotation
{
/** @var array<string> @Required */
public $fields = array();
/** @var boolean */
public $updatable = true;
/** @var string */
public $style = 'default'; // or "camel"
/** @var boolean */
public $unique = true;
/** @var string */
public $unique_base = null;
/** @var string */
public $separator = '-';
/** @var string */
public $prefix = '';
/** @var string */
public $suffix = '';
/** @var array<Gedmo\Mapping\Annotation\SlugHandler> */
public $handlers = array();
/** @var string */
public $dateFormat = 'Y-m-d-H:i';
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* SlugHandler annotation for Sluggable behavioral extension
*
* @Annotation
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class SlugHandler extends Annotation
{
public $class = '';
public $options = array();
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* SlugHandlerOption annotation for Sluggable behavioral extension
*
* @Annotation
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class SlugHandlerOption extends Annotation
{
public $name;
public $value;
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Group annotation for SoftDeleteable extension
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @Annotation
* @Target("CLASS")
*/
final class SoftDeleteable extends Annotation
{
/** @var string */
public $fieldName = 'deletedAt';
/** @var bool */
public $timeAware = false;
/** @var bool */
public $hardDelete = true;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Group annotation for Sortable extension
*
* @author Lukas Botsch <lukas.botsch@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @Annotation
* @Target("PROPERTY")
*/
final class SortableGroup extends Annotation
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Position annotation for Sortable extension
*
* @author Lukas Botsch <lukas.botsch@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*
* @Annotation
* @Target("PROPERTY")
*/
final class SortablePosition extends Annotation
{
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Timestampable annotation for Timestampable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Timestampable extends Annotation
{
/** @var string */
public $on = 'update';
/** @var string|array */
public $field;
/** @var mixed */
public $value;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Translatable annotation for Translatable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Translatable extends Annotation
{
/** @var boolean */
public $fallback;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TranslationEntity annotation for Translatable behavioral extension
*
* @Annotation
* @Target("CLASS")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TranslationEntity extends Annotation
{
/** @var string @Required */
public $class;
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* Tree annotation for Tree behavioral extension
*
* @Annotation
* @Target("CLASS")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Tree extends Annotation
{
/** @var string */
public $type = 'nested';
/** @var string */
public $activateLocking = false;
/** @var integer */
public $lockingTimeout = 3;
/** @var string $identifierMethod */
public $identifierMethod;
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreeClosure annotation for Tree behavioral extension
*
* @Annotation
* @Target("CLASS")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreeClosure extends Annotation
{
/** @var string @Required */
public $class;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreeLeft annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreeLeft extends Annotation
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreeLevel annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreeLevel extends Annotation
{
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreeLockTime annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreeLockTime extends Annotation
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreeParent annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreeParent extends Annotation
{
}

View File

@@ -0,0 +1,27 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreePath annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @author <rocco@roccosportal.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreePath extends Annotation
{
public $separator = ',';
public $appendId = null;
public $startsWithSeparator = false;
public $endsWithSeparator = true;
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreePath annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author <rocco@roccosportal.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreePathHash extends Annotation
{
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreePath annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreePathSource extends Annotation
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreeRight annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreeRight extends Annotation
{
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* TreeRoot annotation for Tree behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class TreeRoot extends Annotation
{
/** @var string $identifierMethod */
public $identifierMethod;
}

View File

@@ -0,0 +1,46 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
use Gedmo\Uploadable\Mapping\Validator;
/**
* Uploadable annotation for Uploadable behavioral extension
*
* @Annotation
* @Target("CLASS")
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class Uploadable extends Annotation
{
/** @var boolean */
public $allowOverwrite = false;
/** @var boolean */
public $appendNumber = false;
/** @var string */
public $path = '';
/** @var string */
public $pathMethod = '';
/** @var string */
public $callback = '';
/** @var string */
public $filenameGenerator = Validator::FILENAME_GENERATOR_NONE;
/** @var double */
public $maxSize = 0;
/** @var array */
public $allowedTypes = '';
/** @var array */
public $disallowedTypes = '';
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* UploadableFileMimeType Annotation for Uploadable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class UploadableFileMimeType extends Annotation
{
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Gedmo\Mapping\Annotation;
use Doctrine\Common\Annotations\Annotation;
/**
* UploadableFileName Annotation for Uploadable behavioral extension
*
* @Annotation
* @Target("PROPERTY")
*
* @author tiger-seo <tiger.seo@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
final class UploadableFileName extends Annotation
{
}

Some files were not shown because too many files have changed in this diff Show More