Skip to content

Commit

Permalink
Flip the inheritance chain between annotation and attribute drivers f…
Browse files Browse the repository at this point in the history
…or the extensions
  • Loading branch information
mbabker committed Feb 19, 2024
1 parent 4f44f4e commit ea1aea7
Show file tree
Hide file tree
Showing 26 changed files with 1,382 additions and 1,287 deletions.
79 changes: 3 additions & 76 deletions src/Blameable/Mapping/Driver/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,88 +9,15 @@

namespace Gedmo\Blameable\Mapping\Driver;

use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Annotation\Blameable;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
use Gedmo\Mapping\Driver\AnnotationDriverInterface;

/**
* 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>
* Mapping driver for the blamable extension which reads extended metadata from annotations on a blamable class.
*
* @deprecated since gedmo/doctrine-extensions 3.16, will be removed in version 4.0.
*
* @internal
*/
class Annotation extends AbstractAnnotationDriver
class Annotation extends Attribute implements AnnotationDriverInterface
{
/**
* Annotation field is blameable
*/
public const BLAMEABLE = Blameable::class;

/**
* List of types which are valid for blame
*
* @var string[]
*/
protected $validTypes = [
'one',
'string',
'int',
];

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->getName()}");
}
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->getName()}");
}
} 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->getName()}");
}
}
if (!in_array($blameable->on, ['update', 'create', 'change'], true)) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->getName()}");
}
if ('change' === $blameable->on) {
if (!isset($blameable->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->getName()}");
}
if (is_array($blameable->field) && isset($blameable->value)) {
throw new InvalidMappingException('Blameable extension does not support multiple value changeset detection yet.');
}
$field = [
'field' => $field,
'trackedField' => $blameable->field,
'value' => $blameable->value,
];
}
// properties are unique and mapper checks that, no risk here
$config[$blameable->on][] = $field;
}
}

return $config;
}
}
88 changes: 82 additions & 6 deletions src/Blameable/Mapping/Driver/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,92 @@

namespace Gedmo\Blameable\Mapping\Driver;

use Gedmo\Mapping\Driver\AttributeDriverInterface;
use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Annotation\Blameable;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;

/**
* This is an attribute mapping driver for Blameable
* behavioral extension. Used for extraction of extended
* metadata from attribute specifically for Blameable
* extension.
* Mapping driver for the blameable extension which reads extended metadata from attributes on a blameable class.
*
* @author David Buchmann <mail@davidbu.ch>
*
* @internal
*/
final class Attribute extends Annotation implements AttributeDriverInterface
class Attribute extends AbstractAnnotationDriver
{
/**
* Mapping object for the blameable extension.
*/
public const BLAMEABLE = Blameable::class;

/**
* List of types which are valid for blame
*
* @var string[]
*/
protected $validTypes = [
'one',
'string',
'int',
];

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)) {
\assert($blameable instanceof 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->getName()}");
}

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->getName()}");
}
} 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->getName()}");
}
}

if (!in_array($blameable->on, ['update', 'create', 'change'], true)) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->getName()}");
}

if ('change' === $blameable->on) {
if (!isset($blameable->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->getName()}");
}

if (is_array($blameable->field) && isset($blameable->value)) {
throw new InvalidMappingException('Blameable extension does not support multiple value changeset detection yet.');
}

$field = [
'field' => $field,
'trackedField' => $blameable->field,
'value' => $blameable->value,
];
}
// properties are unique and mapper checks that, no risk here
$config[$blameable->on][] = $field;
}
}

return $config;
}
}
70 changes: 3 additions & 67 deletions src/IpTraceable/Mapping/Driver/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,15 @@

namespace Gedmo\IpTraceable\Mapping\Driver;

use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Annotation\IpTraceable;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;
use Gedmo\Mapping\Driver\AnnotationDriverInterface;

/**
* 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>
* Mapping driver for the IP traceable extension which reads extended metadata from annotations on an IP traceable class.
*
* @deprecated since gedmo/doctrine-extensions 3.16, will be removed in version 4.0.
*
* @internal
*/
class Annotation extends AbstractAnnotationDriver
class Annotation extends Attribute implements AnnotationDriverInterface
{
/**
* Annotation field is ipTraceable
*/
public const IP_TRACEABLE = IpTraceable::class;

/**
* List of types which are valid for IP
*
* @var string[]
*/
protected $validTypes = [
'string',
];

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->getName()}");
}
if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' - {$meta->getName()}");
}
if (!in_array($ipTraceable->on, ['update', 'create', 'change'], true)) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->getName()}");
}
if ('change' === $ipTraceable->on) {
if (!isset($ipTraceable->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->getName()}");
}
if (is_array($ipTraceable->field) && isset($ipTraceable->value)) {
throw new InvalidMappingException('IpTraceable extension does not support multiple value changeset detection yet.');
}
$field = [
'field' => $field,
'trackedField' => $ipTraceable->field,
'value' => $ipTraceable->value,
];
}
// properties are unique and mapper checks that, no risk here
$config[$ipTraceable->on][] = $field;
}
}

return $config;
}
}
79 changes: 73 additions & 6 deletions src/IpTraceable/Mapping/Driver/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,84 @@

namespace Gedmo\IpTraceable\Mapping\Driver;

use Gedmo\Exception\InvalidMappingException;
use Gedmo\Mapping\Annotation\IpTraceable;
use Gedmo\Mapping\Driver\AttributeDriverInterface;
use Gedmo\Mapping\Driver\AbstractAnnotationDriver;

/**
* This is an attribute mapping driver for IpTraceable
* behavioral extension. Used for extraction of extended
* metadata from attribute specifically for IpTraceable
* extension.
* Mapping driver for the IP traceable extension which reads extended metadata from attributes on an IP traceable class.
*
* @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
*
* @internal
*/
final class Attribute extends Annotation implements AttributeDriverInterface
class Attribute extends AbstractAnnotationDriver
{
/**
* Mapping object for the IP traceable extension.
*/
public const IP_TRACEABLE = IpTraceable::class;

/**
* List of types which are valid for IP
*
* @var string[]
*/
protected $validTypes = [
'string',
];

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)) {
\assert($ipTraceable instanceof IpTraceable);

$field = $property->getName();

if (!$meta->hasField($field)) {
throw new InvalidMappingException("Unable to find ipTraceable [{$field}] as mapped property in entity - {$meta->getName()}");
}

if (!$this->isValidField($meta, $field)) {
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' - {$meta->getName()}");
}

if (!in_array($ipTraceable->on, ['update', 'create', 'change'], true)) {
throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->getName()}");
}

if ('change' === $ipTraceable->on) {
if (!isset($ipTraceable->field)) {
throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->getName()}");
}

if (is_array($ipTraceable->field) && isset($ipTraceable->value)) {
throw new InvalidMappingException('IpTraceable extension does not support multiple value changeset detection yet.');
}

$field = [
'field' => $field,
'trackedField' => $ipTraceable->field,
'value' => $ipTraceable->value,
];
}

// properties are unique and mapper checks that, no risk here
$config[$ipTraceable->on][] = $field;
}
}

return $config;
}
}
Loading

0 comments on commit ea1aea7

Please sign in to comment.