Skip to content

Commit 3a79af0

Browse files
mbabkerfranmomu
authored andcommitted
Update docs examples to show configuring and injecting the attribute reader to listeners
1 parent d98fbd4 commit 3a79af0

File tree

2 files changed

+77
-47
lines changed

2 files changed

+77
-47
lines changed

doc/ip_traceable.md

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# IpTraceable behavior extension for Doctrine
22

33
**IpTraceable** behavior will automate the update of IP trace
4-
on your Entities or Documents. It works through annotations and can update
4+
on your Entities or Documents. It works through annotations or attributes and can update
55
fields on creation, update, property subset update, or even on specific property value change.
66

7-
This is very similar to Timestampable but sets a string.
7+
This is very similar to the Timestampable behavior.
88

99
Note that you need to set the IP on the IpTraceableListener (unless you use the
10-
Symfony extension which does automatically assign the current request IP).
11-
10+
Symfony bundle which automatically assigns the current request IP).
1211

1312
Features:
1413

@@ -17,16 +16,16 @@ Features:
1716
- Specific attributes and annotations for properties, and no interface required
1817
- Can react to specific property or relation changes to specific value
1918
- Can be nested with other behaviors
20-
- Attribute, Annotation and Xml mapping support for extensions
19+
- Attribute, Annotation and XML mapping support for extensions
2120

22-
This article will cover the basic installation and functionality of **IpTraceable** behavior
21+
This article will cover the basic installation and functionality of the **IpTraceable** behavior
2322

2423
Content:
2524

2625
- [Including](#including-extension) the extension
2726
- Entity [example](#entity-mapping)
2827
- Document [example](#document-mapping)
29-
- [Xml](#xml-mapping) mapping example
28+
- [XML](#xml-mapping) mapping example
3029
- Advanced usage [examples](#advanced-examples)
3130
- Using [Traits](#traits)
3231

@@ -36,7 +35,7 @@ Content:
3635

3736
Read the [documentation](./annotations.md#em-setup)
3837
or check the [example code](../example)
39-
on how to setup and use the extensions in most optimized way.
38+
on how to set up and use the extensions.
4039

4140
<a name="entity-mapping"></a>
4241

@@ -58,11 +57,11 @@ should be updated
5857
- **value** - only valid if **on="change"** is specified and the tracked field is a single field (not an array), if the tracked field has this **value**
5958
then it updates the trace
6059

61-
**Note:** that IpTraceable interface is not necessary, except in cases there
62-
you need to identify entity as being IpTraceable. The metadata is loaded only once then
63-
cache is activated
60+
**Note:** the IpTraceable interface is not necessary, except in cases where
61+
you need to identify the object as being IpTraceable in your application.
62+
The metadata is loaded only once when the cache is activated.
6463

65-
**Note:** these examples are using annotations and attributes for mapping, you should use
64+
**Note:** these examples are using annotations and attributes for mapping, you should only use
6665
one of them, not both.
6766

6867
Column is a string field:
@@ -91,7 +90,7 @@ class Article
9190
private $id;
9291

9392
/**
94-
* * @var string|null
93+
* @var string|null
9594
* @ORM\Column(type="string", length=128)
9695
*/
9796
#[ORM\Column(type: Types::STRING, length: 128)]
@@ -256,7 +255,7 @@ Now on update and creation these annotated fields will be automatically updated
256255

257256
<a name="xml-mapping"></a>
258257

259-
## Xml mapping example
258+
## XML mapping example
260259

261260
```xml
262261
<?xml version="1.0" encoding="UTF-8"?>
@@ -268,13 +267,13 @@ Now on update and creation these annotated fields will be automatically updated
268267
<generator strategy="AUTO"/>
269268
</id>
270269

271-
<field name="createdFromIp" type="string", length="45", nullable="true">
270+
<field name="createdFromIp" type="string" length="45" nullable="true">
272271
<gedmo:ip-traceable on="create"/>
273272
</field>
274-
<field name="updatedFromIp" type="string", length="45", nullable="true">
273+
<field name="updatedFromIp" type="string" length="45" nullable="true">
275274
<gedmo:ip-traceable on="update"/>
276275
</field>
277-
<field name="publishedFromIp" type="string" nullable="true", length="45">
276+
<field name="publishedFromIp" type="string" nullable="true" length="45">
278277
<gedmo:ip-traceable on="change" field="status.title" value="Published"/>
279278
</field>
280279

@@ -423,7 +422,7 @@ class Article
423422
}
424423
```
425424

426-
Now few operations to get it all done:
425+
Now a few operations to get it all done:
427426

428427
```php
429428
<?php
@@ -455,12 +454,12 @@ Easy like that, any suggestions on improvements are very welcome
455454

456455
## Traits
457456

458-
You can use IpTraceable traits for quick **createdFromIp** **updatedFromIp** string definitions
459-
when using annotation mapping.
460-
There is also a trait without annotations for easy integration purposes.
457+
You can use the IpTraceable traits to quickly add **createdFromIp** and **updatedFromIp** fields to your objects
458+
when using annotation or attribute mapping.
461459

462-
**Note:** this feature is only available since php **5.4.0**. And you are not required
463-
to use the Traits provided by extensions.
460+
There is also a trait without annotation or attribute mappings for easy integration.
461+
462+
**Note:** You are not required to use the traits provided by extensions.
464463

465464
```php
466465
<?php
@@ -500,17 +499,17 @@ own Traits specific to your project. The ones provided by this bundle can be use
500499

501500
## Example of implementation in Symfony
502501

503-
In your Sf2 application, declare an event subscriber that automatically set IP value on IpTraceableListener.
502+
In your Symfony application, declare an event subscriber that automatically sets the IP address value on IpTraceableListener.
504503

505-
### Code of subscriber class
504+
### Example event subscriber class
506505

507506
```php
508507
<?php
509508

510509
namespace Acme\DemoBundle\EventListener;
511510

512511
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
513-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
512+
use Symfony\Component\HttpKernel\Event\RequestEvent;
514513
use Symfony\Component\HttpKernel\KernelEvents;
515514
use Symfony\Component\HttpFoundation\Request;
516515

@@ -521,38 +520,30 @@ use Gedmo\IpTraceable\IpTraceableListener;
521520
*/
522521
class IpTraceSubscriber implements EventSubscriberInterface
523522
{
524-
/**
525-
* @var Request
526-
*/
527-
private $request;
528-
529523
/**
530524
* @var IpTraceableListener
531525
*/
532526
private $ipTraceableListener;
533527

534-
public function __construct(IpTraceableListener $ipTraceableListener, Request $request = null)
528+
public function __construct(IpTraceableListener $ipTraceableListener)
535529
{
536530
$this->ipTraceableListener = $ipTraceableListener;
537-
$this->request = $request;
538531
}
539532

540533
/**
541-
* Set the username from the security context by listening on core.request
542-
*
543-
* @param GetResponseEvent $event
534+
* Set the IP address during the `kernel.request` event
544535
*/
545-
public function onKernelRequest(GetResponseEvent $event)
536+
public function onKernelRequest(RequestEvent $event)
546537
{
547-
if (null === $this->request) {
538+
// Generally, the listener should only be updated during the main request
539+
if (!$event->isMainRequest()) {
548540
return;
549541
}
550542

551543
// If you use a cache like Varnish, you may want to set a proxy to Request::getClientIp() method
552-
// $this->request->setTrustedProxies(array('127.0.0.1'));
544+
// $event->getRequest()->setTrustedProxies(array('127.0.0.1'));
553545

554-
// $ip = $_SERVER['REMOTE_ADDR'];
555-
$ip = $this->request->getClientIp();
546+
$ip = $event->getRequest()->getClientIp();
556547

557548
if (null !== $ip) {
558549
$this->ipTraceableListener->setIpValue($ip);
@@ -579,23 +570,26 @@ class IpTraceSubscriber implements EventSubscriberInterface
579570
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
580571

581572
<parameters>
582-
<parameter key="alterphp_doctrine_extensions.event_listener.ip_trace.class">Acme\DemoBundle\EventListener\IpTraceListener</parameter>
573+
<parameter key="acme_doctrine_extensions.event_listener.ip_trace.class">Acme\DemoBundle\EventListener\IpTraceListener</parameter>
583574
</parameters>
584575

585576
<services>
586577

587-
...
578+
<!-- If your application is using PHP 8 and attributes, you can provide this attribute reader to the listener instead of an annotation reader -->
579+
<service id="gedmo_doctrine_extensions.mapping.driver.attribute" class="Gedmo\Mapping\Driver\AttributeReader" public="false" />
588580

589581
<service id="gedmo_doctrine_extensions.listener.ip_traceable" class="Gedmo\IpTraceable\IpTraceableListener" public="false">
590582
<tag name="doctrine.event_subscriber" connection="default" />
591583
<call method="setAnnotationReader">
584+
<!-- Uncomment the below argument if using attributes, and comment the argument for the annotation reader -->
585+
<!-- <argument type="service" id="gedmo_doctrine_extensions.mapping.driver.attribute" /> -->
586+
<!-- The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0 -->
592587
<argument type="service" id="annotation_reader" />
593588
</call>
594589
</service>
595590

596-
<service id="alterphp_doctrine_extensions.event_listener.ip_trace" class="%alterphp_doctrine_extensions.event_listener.ip_trace.class%" public="false" scope="request">
591+
<service id="acme_doctrine_extensions.event_listener.ip_trace" class="%acme_doctrine_extensions.event_listener.ip_trace.class%" public="false">
597592
<argument type="service" id="gedmo_doctrine_extensions.listener.ip_traceable" />
598-
<argument type="service" id="request" on-invalid="null" />
599593
<tag name="kernel.event_subscriber" />
600594
</service>
601595

doc/symfony.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ in the bundle, it depends on your preferences. Edit **config/packages/doctrine_e
151151
# services to handle doctrine extensions
152152
# import it in config/packages/doctrine_extensions.yaml
153153
services:
154+
# Attribute mapping driver for the Doctrine Extension listeners
155+
gedmo.mapping.driver.attribute:
156+
class: Gedmo\Mapping\Driver\AttributeReader
157+
154158
# Doctrine Extension listeners to handle behaviors
155159
gedmo.listener.tree:
156160
class: Gedmo\Tree\TreeListener
@@ -164,6 +168,9 @@ services:
164168
- { name: doctrine.event_listener, event: 'postUpdate'}
165169
- { name: doctrine.event_listener, event: 'postRemove'}
166170
calls:
171+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
172+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
173+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
167174
- [ setAnnotationReader, [ "@annotation_reader" ] ]
168175

169176
Gedmo\Translatable\TranslatableListener:
@@ -174,7 +181,11 @@ services:
174181
- { name: doctrine.event_listener, event: 'onFlush' }
175182
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
176183
calls:
184+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
185+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
186+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
177187
- [ setAnnotationReader, [ "@annotation_reader" ] ]
188+
178189
- [ setDefaultLocale, [ "%locale%" ] ]
179190
- [ setTranslationFallback, [ false ] ]
180191

@@ -185,6 +196,9 @@ services:
185196
- { name: doctrine.event_listener, event: 'onFlush' }
186197
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
187198
calls:
199+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
200+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
201+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
188202
- [ setAnnotationReader, [ "@annotation_reader" ] ]
189203

190204
gedmo.listener.sluggable:
@@ -194,6 +208,9 @@ services:
194208
- { name: doctrine.event_listener, event: 'onFlush' }
195209
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
196210
calls:
211+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
212+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
213+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
197214
- [ setAnnotationReader, [ "@annotation_reader" ] ]
198215

199216
gedmo.listener.sortable:
@@ -207,6 +224,9 @@ services:
207224
- { name: doctrine.event_listener, event: 'postRemove' }
208225
- { name: doctrine.event_listener, event: 'postFlush' }
209226
calls:
227+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
228+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
229+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
210230
- [ setAnnotationReader, [ "@annotation_reader" ] ]
211231

212232
gedmo.listener.softdeleteable:
@@ -215,6 +235,9 @@ services:
215235
- { name: doctrine.event_listener, event: 'onFlush' }
216236
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
217237
calls:
238+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
239+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
240+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
218241
- [ setAnnotationReader, [ "@annotation_reader" ] ]
219242

220243
Gedmo\Loggable\LoggableListener:
@@ -223,6 +246,9 @@ services:
223246
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
224247
- { name: doctrine.event_listener, event: 'postPersist' }
225248
calls:
249+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
250+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
251+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
226252
- [ setAnnotationReader, [ "@annotation_reader" ] ]
227253

228254
Gedmo\Blameable\BlameableListener:
@@ -231,6 +257,9 @@ services:
231257
- { name: doctrine.event_listener, event: 'onFlush' }
232258
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
233259
calls:
260+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
261+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
262+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
234263
- [ setAnnotationReader, [ "@annotation_reader" ] ]
235264

236265
Gedmo\IpTraceable\IpTraceableListener:
@@ -239,7 +268,10 @@ services:
239268
- { name: doctrine.event_listener, event: 'onFlush' }
240269
- { name: doctrine.event_listener, event: 'loadClassMetadata' }
241270
calls:
242-
- [ setAnnotationReader, [ "@annotation_reader" ] ]
271+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
272+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
273+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
274+
- [ setAnnotationReader, [ "@annotation_reader" ] ]
243275

244276
```
245277

@@ -258,12 +290,16 @@ You also need to manually tag the listeners. Otherwise, the listeners will not b
258290
of Doctrine.
259291

260292
```yaml
261-
Gedmo\Loggable\LoggableListener:
293+
services:
294+
Gedmo\Loggable\LoggableListener:
262295
tags:
263296
- { name: doctrine_mongodb.odm.event_listener, event: 'onFlush' }
264297
- { name: doctrine_mongodb.odm.event_listener, event: 'loadClassMetadata' }
265298
- { name: doctrine_mongodb.odm.event_listener, event: 'postPersist' }
266299
calls:
300+
# Uncomment the below call if using attributes, and comment the call for the annotation reader
301+
# - [ setAnnotationReader, [ "@gedmo.mapping.driver.attribute" ] ]
302+
# The `annotation_reader` service was deprecated in Symfony 6.4 and removed in Symfony 7.0
267303
- [ setAnnotationReader, [ "@annotation_reader" ] ]
268304
```
269305

0 commit comments

Comments
 (0)