Skip to content

Commit 535bdcb

Browse files
franmomuphansys
authored andcommitted
Allow to use Loggable extension with attributes
1 parent 3cf1b67 commit 535bdcb

File tree

21 files changed

+302
-156
lines changed

21 files changed

+302
-156
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ a release.
2323
- PHP 8 Attributes support for Doctrine MongoDB to document & traits.
2424
- Support for doctrine/dbal >=3.2.
2525
- Timestampable: Support to use annotations as attributes on PHP >= 8.0.
26+
- Loggable: Support to use annotations as attributes on PHP >= 8.0.
2627

2728
### Changes
2829
- Translatable: Dropped support for other values than "true", "false", "1" and "0" in the `fallback` attribute of the XML mapping.

doc/loggable.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Features:
99
- ORM and ODM support using same listener
1010
- Can be nested with other behaviors
1111
- Objects can be reverted to previous versions
12-
- Annotation, Yaml and Xml mapping support for extensions
12+
- Attributes, Annotation, Yaml and Xml mapping support for extensions
1313

1414
Update **2011-04-04**
1515

@@ -47,6 +47,12 @@ on how to setup and use the extensions in most optimized way.
4747
will store logs to optionally specified **logEntryClass**. You will still need to specify versioned fields with the following annotation.
4848
- **@Gedmo\Mapping\Annotation\Versioned** tracks annotated property for changes
4949

50+
### Loggable annotations:
51+
52+
- **\#[Gedmo\Mapping\Annotation\Loggable(logEntryClass: MyClass::class]** this class attribute
53+
will store logs to optionally specified **logEntryClass**. You will still need to specify versioned fields with the following attribute.
54+
- **\#[Gedmo\Mapping\Annotation\Versioned]** tracks attributed property for changes
55+
5056
### Loggable username:
5157

5258
In order to set the username, when adding the loggable listener you need to set it this way:
@@ -65,30 +71,41 @@ $evm->addEventSubscriber($loggableListener);
6571
you need to identify entity as being Loggable. The metadata is loaded only once when
6672
cache is active
6773

74+
**Note:** this example is using annotations and attributes for mapping, you should use
75+
one of them, not both.
76+
6877
``` php
6978
<?php
7079
namespace Entity;
7180

7281
use Gedmo\Mapping\Annotation as Gedmo;
82+
use Doctrine\DBAL\Types\Types;
7383
use Doctrine\ORM\Mapping as ORM;
7484

7585
/**
7686
* @ORM\Entity
7787
* @Gedmo\Loggable
7888
*/
89+
#[ORM\Entity]
90+
#[Gedmo\Loggable]
7991
class Article
8092
{
8193
/**
8294
* @ORM\Column(name="id", type="integer")
8395
* @ORM\Id
8496
* @ORM\GeneratedValue(strategy="IDENTITY")
8597
*/
98+
#[ORM\Id]
99+
#[ORM\Column(name: 'id', type: Types::INTEGER)]
100+
#[ORM\GeneratedValue(strategy: 'IDENTITY')]
86101
private $id;
87102

88103
/**
89104
* @Gedmo\Versioned
90105
* @ORM\Column(name="title", type="string", length=8)
91106
*/
107+
#[Gedmo\Versioned]
108+
#[ORM\Column(name: 'title', type: Types::STRING, length: 8)]
92109
private $title;
93110

94111
public function getId()
@@ -118,20 +135,26 @@ namespace Document;
118135

119136
use Gedmo\Mapping\Annotation as Gedmo;
120137
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
138+
use Doctrine\ODM\MongoDB\Types\Type;
121139

122140
/**
123141
* @ODM\Document(collection="articles")
124142
* @Gedmo\Loggable
125143
*/
144+
#[Gedmo\Loggable]
145+
#[ODM\Document(collection: 'articles')]
126146
class Article
127147
{
128148
/** @ODM\Id */
149+
#[ODM\Id]
129150
private $id;
130151

131152
/**
132153
* @ODM\Field(type="string")
133154
* @Gedmo\Versioned
134155
*/
156+
#[Gedmo\Versioned]
157+
#[ODM\Field(type: Type::STRING)]
135158
private $title;
136159

137160
public function __toString()

doc/timestampable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Features:
1111
- Specific annotations for properties, and no interface required
1212
- Can react to specific property or relation changes to specific value
1313
- Can be nested with other behaviors
14-
- Annotation, Yaml and Xml mapping support for extensions
14+
- Attribute, Annotation, Yaml and Xml mapping support for extensions
1515

1616
Update **2012-06-26**
1717

doc/translatable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Features:
1212
- Automatic translation of Entity or Document fields when loaded
1313
- ORM query can use **hint** to translate all records without issuing additional queries
1414
- Can be nested with other behaviors
15-
- Annotation, Yaml and Xml mapping support for extensions
15+
- Attribute, Annotation, Yaml and Xml mapping support for extensions
1616

1717
**2012-01-28**
1818

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Doctrine Behavioral Extensions package.
5+
* (c) Gediminas Morkevicius <[email protected]> http://www.gediminasm.org
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Gedmo\Loggable\Mapping\Driver;
11+
12+
/**
13+
* This is an attribute mapping driver for Loggable
14+
* behavioral extension. Used for extraction of extended
15+
* metadata from attributes specifically for Loggable
16+
* extension.
17+
*/
18+
final class Attribute extends Annotation
19+
{
20+
}

src/Mapping/Annotation/Loggable.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,40 @@
99

1010
namespace Gedmo\Mapping\Annotation;
1111

12+
use Attribute;
1213
use Doctrine\Common\Annotations\Annotation;
14+
use Gedmo\Mapping\Annotation\Annotation as GedmoAnnotation;
1315

1416
/**
1517
* Loggable annotation for Loggable behavioral extension
1618
*
1719
* @Annotation
20+
* @NamedArgumentConstructor
1821
* @Target("CLASS")
1922
*
2023
* @author Gediminas Morkevicius <[email protected]>
2124
*/
22-
final class Loggable extends Annotation
25+
#[Attribute(Attribute::TARGET_CLASS)]
26+
final class Loggable implements GedmoAnnotation
2327
{
24-
/** @var string */
28+
/**
29+
* @var string|null
30+
* @phpstan-var class-string|null
31+
*/
2532
public $logEntryClass;
33+
34+
/**
35+
* @phpstan-param class-string|null $logEntryClass
36+
*/
37+
public function __construct(array $data = [], ?string $logEntryClass = null)
38+
{
39+
if ([] !== $data) {
40+
@trigger_error(sprintf(
41+
'Passing an array as first argument to "%s()" is deprecated. Use named arguments instead.',
42+
__METHOD__
43+
), E_USER_DEPRECATED);
44+
}
45+
46+
$this->logEntryClass = $data['logEntryClass'] ?? $logEntryClass;
47+
}
2648
}

src/Mapping/Annotation/Versioned.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@
99

1010
namespace Gedmo\Mapping\Annotation;
1111

12+
use Attribute;
1213
use Doctrine\Common\Annotations\Annotation;
14+
use Gedmo\Mapping\Annotation\Annotation as GedmoAnnotation;
1315

1416
/**
1517
* Versioned annotation for Loggable behavioral extension
1618
*
1719
* @Annotation
20+
* @NamedArgumentConstructor
1821
* @Target("PROPERTY")
1922
*
2023
* @author Gediminas Morkevicius <[email protected]>
2124
*/
22-
final class Versioned extends Annotation
25+
#[Attribute(Attribute::TARGET_PROPERTY)]
26+
final class Versioned implements GedmoAnnotation
2327
{
2428
}

tests/Gedmo/Loggable/Fixture/Document/Article.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,68 @@
1212
namespace Gedmo\Tests\Loggable\Fixture\Document;
1313

1414
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
15+
use Doctrine\ODM\MongoDB\Types\Type;
1516
use Gedmo\Mapping\Annotation as Gedmo;
1617

1718
/**
1819
* @ODM\Document(collection="articles")
1920
* @Gedmo\Loggable
2021
*/
22+
#[ODM\Document(collection: 'articles')]
23+
#[Gedmo\Loggable]
2124
class Article
2225
{
23-
/** @ODM\Id */
26+
/**
27+
* @var string|null
28+
* @ODM\Id
29+
*/
30+
#[ODM\Id]
2431
private $id;
2532

2633
/**
34+
* @var string|null
2735
* @Gedmo\Versioned
2836
* @ODM\Field(type="string")
2937
*/
38+
#[ODM\Field(type: Type::STRING)]
39+
#[Gedmo\Versioned]
3040
private $title;
3141

3242
/**
43+
* @var Author|null
3344
* @ODM\EmbedOne(targetDocument="Gedmo\Tests\Loggable\Fixture\Document\Author")
3445
* @Gedmo\Versioned
3546
*/
47+
#[ODM\EmbedOne(targetDocument: Author::class)]
48+
#[Gedmo\Versioned]
3649
private $author;
3750

3851
public function __toString()
3952
{
4053
return $this->title;
4154
}
4255

43-
public function getId()
56+
public function getId(): ?string
4457
{
4558
return $this->id;
4659
}
4760

48-
public function setTitle($title)
61+
public function setTitle(?string $title): void
4962
{
5063
$this->title = $title;
5164
}
5265

53-
public function getTitle()
66+
public function getTitle(): ?string
5467
{
5568
return $this->title;
5669
}
5770

58-
public function setAuthor($author)
71+
public function setAuthor(?Author $author): void
5972
{
6073
$this->author = $author;
6174
}
6275

63-
public function getAuthor()
76+
public function getAuthor(): ?Author
6477
{
6578
return $this->author;
6679
}

tests/Gedmo/Loggable/Fixture/Document/Author.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,56 @@
1212
namespace Gedmo\Tests\Loggable\Fixture\Document;
1313

1414
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
15+
use Doctrine\ODM\MongoDB\Types\Type;
1516
use Gedmo\Mapping\Annotation as Gedmo;
1617

1718
/**
1819
* @ODM\EmbeddedDocument
1920
* @Gedmo\Loggable
2021
*/
22+
#[ODM\EmbeddedDocument]
23+
#[Gedmo\Loggable]
2124
class Author
2225
{
2326
/**
27+
* @var string|null
2428
* @Gedmo\Versioned
2529
* @ODM\Field(type="string")
2630
*/
31+
#[ODM\Field(type: Type::STRING)]
32+
#[Gedmo\Versioned]
2733
private $name;
2834

2935
/**
36+
* @var string|null
3037
* @Gedmo\Versioned
3138
* @ODM\Field(type="string")
3239
*/
40+
#[ODM\Field(type: Type::STRING)]
41+
#[Gedmo\Versioned]
3342
private $email;
3443

3544
public function __toString()
3645
{
37-
return $this->getName();
46+
return (string) $this->getName();
3847
}
3948

40-
public function setName($name)
49+
public function setName(?string $name): void
4150
{
4251
$this->name = $name;
4352
}
4453

45-
public function getName()
54+
public function getName(): ?string
4655
{
4756
return $this->name;
4857
}
4958

50-
public function setEmail($email)
59+
public function setEmail(?string $email): void
5160
{
5261
$this->email = $email;
5362
}
5463

55-
public function getEmail()
64+
public function getEmail(): ?string
5665
{
5766
return $this->email;
5867
}

0 commit comments

Comments
 (0)