Skip to content

Commit abe1688

Browse files
committed
Event support. FOSUser bridge. Updated readme.
1 parent 574a743 commit abe1688

File tree

13 files changed

+307
-23
lines changed

13 files changed

+307
-23
lines changed

ApiDocumentationBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ class ApiDocumentationBuilder
4848
private $description;
4949

5050
/**
51-
* @param Resources $resources
52-
* @param RouterInterface $router
51+
* @param Resources $resources
52+
* @param RouterInterface $router
5353
* @param ClassMetadataFactory $classMetadataFactory
54-
* @param string $title
55-
* @param string $description
54+
* @param string $title
55+
* @param string $description
5656
*/
5757
public function __construct(
5858
Resources $resources,
@@ -96,7 +96,7 @@ public function getApiDocumentation()
9696
foreach ($resource->getCollectionOperations() as $operation) {
9797
$supportedOperation = [];
9898

99-
if('POST' === $operation['hydra:method']) {
99+
if ('POST' === $operation['hydra:method']) {
100100
$supportedOperation['@type'] = 'hydra:CreateResourceOperation';
101101
$supportedOperation['hydra:title'] = sprintf('Creates a %s resource.', $shortName);
102102
$supportedOperation['hydra:expects'] = $shortName;

Controller/ResourceController.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Dunglas\JsonLdApiBundle\Controller;
1313

1414
use Doctrine\ORM\Tools\Pagination\Paginator;
15+
use Dunglas\JsonLdApiBundle\DunglasJsonLdApiEvents;
16+
use Dunglas\JsonLdApiBundle\Event\ObjectEvent;
1517
use Dunglas\JsonLdApiBundle\Resource;
1618
use Dunglas\JsonLdApiBundle\Response\JsonLdResponse;
1719
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@@ -167,10 +169,7 @@ public function postAction(Request $request)
167169
$violations = $this->get('validator')->validate($object, null, $resource->getValidationGroups());
168170
if (0 === count($violations)) {
169171
// Validation succeed
170-
$manager = $this->getManager($resource);
171-
172-
$manager->persist($object);
173-
$manager->flush();
172+
$this->get('event_dispatcher')->dispatch(DunglasJsonLdApiEvents::PRE_CREATE, new ObjectEvent($resource, $object));
174173

175174
return new JsonLdResponse($this->normalize($resource, $object), 201);
176175
}
@@ -226,7 +225,7 @@ public function putAction(Request $request, $id)
226225
$violations = $this->get('validator')->validate($object, null, $resource->getValidationGroups());
227226
if (0 === count($violations)) {
228227
// Validation succeed
229-
$this->getManager($resource)->flush();
228+
$this->get('event_dispatcher')->dispatch(DunglasJsonLdApiEvents::PRE_UPDATE, new ObjectEvent($resource, $object));
230229

231230
return new JsonLdResponse($this->normalize($resource, $object), 202);
232231
}
@@ -250,9 +249,7 @@ public function deleteAction(Request $request, $id)
250249
$resource = $this->getResource($request);
251250
$object = $this->findOrThrowNotFound($resource, $id);
252251

253-
$manager = $this->getManager($resource);
254-
$manager->remove($object);
255-
$manager->flush();
252+
$this->get('event_dispatcher')->dispatch(DunglasJsonLdApiEvents::PRE_DELETE, new ObjectEvent($resource, $object));
256253

257254
return new JsonLdResponse(null, 204);
258255
}

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function getConfigTreeBuilder()
3434
->scalarNode('title')->cannotBeEmpty()->isRequired()->info('API\'s title.')->end()
3535
->scalarNode('description')->cannotBeEmpty()->isRequired()->info('API\'s description.')->end()
3636
->integerNode('elements_by_page')->min(1)->defaultValue(100)->cannotBeEmpty()->info('The number of elements by page in collections.')->end()
37+
->booleanNode('enable_fos_user_event_subscriber')->defaultFalse()->end()
3738
->end()
3839
;
3940

DependencyInjection/DunglasJsonLdApiExtension.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,29 @@
1313

1414
use Symfony\Component\Config\FileLocator;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Definition;
17+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1618
use Symfony\Component\DependencyInjection\Loader;
19+
use Symfony\Component\DependencyInjection\Reference;
1720
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1821

19-
2022
/**
2123
* The extension of this bundle.
2224
*
2325
* @author Kévin Dunglas <[email protected]>
2426
*/
25-
class DunglasJsonLdApiExtension extends Extension
27+
class DunglasJsonLdApiExtension extends Extension implements PrependExtensionInterface
2628
{
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function prepend(ContainerBuilder $container)
33+
{
34+
if (isset($container->getParameter('kernel.bundles')['FOSUserBundle'])) {
35+
$container->prependExtensionConfig($this->getAlias(), ['enable_fos_user_event_subscriber' => true]);
36+
}
37+
}
38+
2739
/**
2840
* {@inheritDoc}
2941
*/
@@ -38,5 +50,15 @@ public function load(array $configs, ContainerBuilder $container)
3850

3951
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
4052
$loader->load('services.xml');
53+
54+
if ($config['enable_fos_user_event_subscriber']) {
55+
$definition = new Definition(
56+
'Dunglas\JsonLdApiBundle\EventListener\FOSUserEventSubscriber',
57+
[new Reference('fos_user.user_manager')]
58+
);
59+
$definition->setTags(['kernel.event_subscriber' => []]);
60+
61+
$container->setDefinition('dunglas_json_ld_api.event_subscriber.fos_user', $definition);
62+
}
4163
}
4264
}

DunglasJsonLdApiEvents.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DunglasJsonLdApiBundle package.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dunglas\JsonLdApiBundle;
13+
14+
/**
15+
* API Events.
16+
*
17+
* @author Kévin Dunglas <[email protected]>
18+
*/
19+
final class DunglasJsonLdApiEvents
20+
{
21+
/**
22+
* The PRE_CREATE event occurs after the object validation and before its persistence during POST request.
23+
*
24+
* @var string
25+
*/
26+
const PRE_CREATE = 'dunglas_json_ld_api.pre_create';
27+
/**
28+
* The PRE_UPDATE event occurs after the object validation and before its persistence during a PUT request.
29+
*
30+
* @var string
31+
*/
32+
const PRE_UPDATE = 'dunglas_json_ld_api.pre_update';
33+
/**
34+
* The PRE_DELETE event occurs before the object deletion.
35+
*/
36+
const PRE_DELETE = 'dunglas_json_ld_api.pre_delete';
37+
}

Event/ObjectEvent.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DunglasJsonLdApiBundle package.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dunglas\JsonLdApiBundle\Event;
13+
14+
use Dunglas\JsonLdApiBundle\Resource;
15+
use Symfony\Component\EventDispatcher\Event;
16+
17+
/**
18+
* ObjectEvent.
19+
*
20+
* @author Kévin Dunglas <[email protected]>
21+
*/
22+
class ObjectEvent extends Event
23+
{
24+
/**
25+
* @var Resource
26+
*/
27+
private $resource;
28+
/**
29+
* @var object
30+
*/
31+
private $object;
32+
33+
/**
34+
* @param object $object
35+
*/
36+
public function __construct(Resource $resource, $object)
37+
{
38+
$this->resource = $resource;
39+
$this->object = $object;
40+
}
41+
42+
/**
43+
* Gets related resource.
44+
*
45+
* @return Resource
46+
*/
47+
public function getResource()
48+
{
49+
return $this->resource;
50+
}
51+
52+
/**
53+
* Gets related object.
54+
*
55+
* @return object
56+
*/
57+
public function getObject()
58+
{
59+
return $this->object;
60+
}
61+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DunglasJsonLdApiBundle package.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dunglas\JsonLdApiBundle\EventListener;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Dunglas\JsonLdApiBundle\DunglasJsonLdApiEvents;
16+
use Dunglas\JsonLdApiBundle\Event\ObjectEvent;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18+
19+
/**
20+
* Doctrine Event Listener.
21+
*
22+
* @author Kévin Dunglas <[email protected]>
23+
*/
24+
class DoctrineEventSubscriber implements EventSubscriberInterface
25+
{
26+
/**
27+
* @var ManagerRegistry
28+
*/
29+
private $managerRegistry;
30+
31+
public function __construct(ManagerRegistry $managerRegistry)
32+
{
33+
$this->managerRegistry = $managerRegistry;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public static function getSubscribedEvents()
40+
{
41+
return [
42+
DunglasJsonLdApiEvents::PRE_CREATE => ['persistObject', 0],
43+
DunglasJsonLdApiEvents::PRE_UPDATE => ['persistObject', 0],
44+
DunglasJsonLdApiEvents::PRE_DELETE => ['deleteObject', 0],
45+
];
46+
}
47+
48+
/**
49+
* Persists the given object and flushes.
50+
*
51+
* @param ObjectEvent $event
52+
*/
53+
public function persistObject(ObjectEvent $event)
54+
{
55+
$objectManager = $this->managerRegistry->getManagerForClass($event->getResource()->getEntityClass());
56+
57+
$objectManager->persist($event->getObject());
58+
$objectManager->flush();
59+
}
60+
61+
/**
62+
* Removes the given object and flushes.
63+
*
64+
* @param ObjectEvent $event
65+
*/
66+
public function deleteObject(ObjectEvent $event)
67+
{
68+
$objectManager = $this->managerRegistry->getManagerForClass($event->getResource()->getEntityClass());
69+
70+
$objectManager->remove($event->getObject());
71+
$objectManager->flush();
72+
}
73+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the DunglasJsonLdApiBundle package.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dunglas\JsonLdApiBundle\EventListener;
13+
14+
use Dunglas\JsonLdApiBundle\DunglasJsonLdApiEvents;
15+
use Dunglas\JsonLdApiBundle\Event\ObjectEvent;
16+
use FOS\UserBundle\Model\UserManagerInterface;
17+
use FOS\UserBundle\Model\UserInterface;
18+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19+
20+
/**
21+
* Bridges between FOSUserBundle and DunglasJsonLdApiBundle.
22+
*
23+
* @author Kévin Dunglas <[email protected]>
24+
*/
25+
class FOSUserEventSubscriber implements EventSubscriberInterface
26+
{
27+
/**
28+
* @var UserManagerInterface
29+
*/
30+
private $userManager;
31+
32+
public function __construct(UserManagerInterface $userManager)
33+
{
34+
$this->userManager = $userManager;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public static function getSubscribedEvents()
41+
{
42+
return [
43+
DunglasJsonLdApiEvents::PRE_CREATE => ['persistObject', 1],
44+
DunglasJsonLdApiEvents::PRE_UPDATE => ['persistObject', 1],
45+
DunglasJsonLdApiEvents::PRE_DELETE => ['deleteObject', 1],
46+
];
47+
}
48+
49+
/**
50+
* Persists the given user object.
51+
*
52+
* @param ObjectEvent $event
53+
*/
54+
public function persistObject(ObjectEvent $event)
55+
{
56+
$object = $event->getObject();
57+
if ($object instanceof UserInterface) {
58+
$this->userManager->updateUser($object);
59+
$event->stopPropagation();
60+
}
61+
}
62+
63+
/**
64+
* Removes the given user object.
65+
*
66+
* @param ObjectEvent $event
67+
*/
68+
public function deleteObject(ObjectEvent $event)
69+
{
70+
$object = $event->getObject();
71+
if ($object instanceof UserInterface) {
72+
$this->userManager->deleteUser($event->getObject());
73+
$event->stopPropagation();
74+
}
75+
}
76+
}

Mapping/ClassMetadata.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ private function populateAttribute($name, array &$attribute, array $validationGr
309309

310310
if ($reflClass->hasProperty($name)) {
311311
$attribute['description'] = (new DocBlock($reflClass->getProperty($name)))->getShortDescription();
312+
312313
return;
313314
}
314315

@@ -324,6 +325,7 @@ private function populateAttribute($name, array &$attribute, array $validationGr
324325

325326
if ($method) {
326327
$attribute['description'] = (new DocBlock($reflClass->getMethod($method)))->getShortDescription();
328+
327329
return;
328330
}
329331

0 commit comments

Comments
 (0)