Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 45c4292

Browse files
committed
SonataAdminEnhancer
1 parent 53bf3da commit 45c4292

File tree

6 files changed

+195
-4
lines changed

6 files changed

+195
-4
lines changed

DependencyInjection/CmfResourceRestExtension.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1818
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1919
use Symfony\Component\Config\Definition\Processor;
20+
use Symfony\Component\Config\Loader\LoaderInterface;
2021

2122
class CmfResourceRestExtension extends Extension implements PrependExtensionInterface
2223
{
24+
private $nativeEnhancers = array(
25+
'payload',
26+
'sonata_admin',
27+
);
28+
2329
public function prepend(ContainerBuilder $container)
2430
{
2531
$container->prependExtensionConfig('jms_serializer', array(
@@ -54,24 +60,56 @@ public function load(array $configs, ContainerBuilder $container)
5460

5561
$loader->load('serializer.xml');
5662
$loader->load('resource-rest.xml');
57-
$loader->load('enhancer.xml');
5863

64+
$this->loadEnhancers($container, $loader, $config['enhancer_map']);
5965
$this->configurePayloadAliasRegistry($container, $config['payload_alias_map']);
60-
$this->configureEnhancerMap($container, $config['enhancer_map']);
66+
$this->configureEnhancers($container, $config['enhancer_map']);
6167
}
6268

6369
public function getNamespace()
6470
{
6571
return 'http://cmf.symfony.com/schema/dic/' . $this->getAlias();
6672
}
6773

74+
/**
75+
* Automatically include native enhancers
76+
*/
77+
private function loadEnhancers(ContainerBuilder $container, LoaderInterface $loader, $enhancerMap)
78+
{
79+
$loaded = array();
80+
foreach ($enhancerMap as $unit) {
81+
$enhancerName = $unit['enhancer'];
82+
83+
if (!in_array($enhancerName, $this->nativeEnhancers)) {
84+
continue;
85+
}
86+
87+
if (isset($loaded[$enhancerName])) {
88+
continue;
89+
}
90+
91+
$loader->load('enhancer.' . $enhancerName . '.xml');
92+
$loaded[$enhancerName] = true;
93+
}
94+
95+
$bundles = $container->getParameter('kernel.bundles');
96+
97+
if (isset($loaded['sonata_admin'])) {
98+
if (!isset($bundles['SonataAdminBundle'])) {
99+
throw new \InvalidArgumentException(
100+
'You must enable the SonataAdminBundle in order to use the "sonata_admin" enhancer'
101+
);
102+
}
103+
}
104+
}
105+
68106
private function configurePayloadAliasRegistry(ContainerBuilder $container, $aliasMap)
69107
{
70108
$registry = $container->getDefinition('cmf_resource_rest.registry.payload_alias');
71109
$registry->replaceArgument(1, $aliasMap);
72110
}
73111

74-
private function configureEnhancerMap(ContainerBuilder $container, $enhancerMap)
112+
private function configureEnhancers(ContainerBuilder $container, $enhancerMap)
75113
{
76114
$enhancerMap = $this->normalizeEnhancerMap($enhancerMap);
77115
$registry = $container->getDefinition('cmf_resource_rest.registry.enhancer');

Enhancer/SonataAdminEnhancer.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
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 Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer;
13+
14+
use JMS\Serializer\Context;
15+
use Puli\Repository\Api\Resource\Resource;
16+
use Sonata\AdminBundle\Admin\Pool;
17+
use Doctrine\Common\Util\ClassUtils;
18+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
19+
20+
/**
21+
* Add links and metainfo from Sonata Admin
22+
*
23+
* @author Daniel Leech <[email protected]>
24+
*/
25+
class SonataAdminEnhancer implements EnhancerInterface
26+
{
27+
private $pool;
28+
private $urlGenerator;
29+
30+
public function __construct(Pool $pool, UrlGeneratorInterface $urlGenerator)
31+
{
32+
$this->pool = $pool;
33+
$this->urlGenerator = $urlGenerator;
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function enhance(Context $context, Resource $resource)
40+
{
41+
$object = $resource->getPayload();
42+
43+
// sonata has dependency on ClassUtils so this is fine.
44+
$class = ClassUtils::getClass($object);
45+
46+
if (false === $this->pool->hasAdminByClass($class)) {
47+
return;
48+
}
49+
50+
$admin = $this->pool->getAdminByClass($class);
51+
$visitor = $context->getVisitor();
52+
53+
$links = array();
54+
foreach (array_keys($admin->getRoutes()) as $routeName) {
55+
$url = $this->urlGenerator->generate($routeName, array(
56+
$admin->getIdParameter(),
57+
$admin->getUrlsafeIdentifier($object)
58+
));
59+
60+
$links[$routeName] = $url;
61+
}
62+
63+
$visitor->addData('_admin_label', $admin->getLabel());
64+
$visitor->addData('_admin_links', $links);
65+
}
66+
}
67+

Resources/config/enhancer.admin.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="cmf_resource_rest.enhancer.sonata_admin.class">Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer\SonataAdminEnhancer</parameter>
9+
</parameters>
10+
11+
<services>
12+
13+
<service id="cmf_resource_rest.enhancer.sonata_admin" class="%cmf_resource_rest.enhancer.sonata_admin.class%">
14+
<argument type="service" id="sonata.admin.pool" />
15+
<tag name="cmf_resource_rest.enhancer" alias="sonata_admin" />
16+
</service>
17+
18+
</services>
19+
</container>
20+
21+
File renamed without changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2014 Symfony CMF
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 Unit\Enhancer;
13+
14+
use Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer\SonataAdminEnhancer;
15+
use Prophecy\PhpUnit\ProphecyTestCase;
16+
17+
class SonataAdminEnhancerTest extends ProphecyTestCase
18+
{
19+
private $enhancer;
20+
private $pool;
21+
private $admin;
22+
23+
public function setUp()
24+
{
25+
$this->admin = $this->prophesize('Sonata\AdminBundle\Admin\Admin');
26+
$this->pool = $this->prophesize('Sonata\AdminBundle\Admin\Pool');
27+
$this->context = $this->prophesize('JMS\Serializer\Context');
28+
$this->visitor = $this->prophesize('JMS\Serializer\GenericSerializationVisitor');
29+
$this->generator = $this->prophesize('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
30+
$this->payload = new \stdClass;
31+
$this->resource = $this->prophesize('Puli\Repository\Api\Resource\Resource');
32+
33+
$this->enhancer = new SonataAdminEnhancer($this->pool->reveal(), $this->generator->reveal());
34+
35+
}
36+
37+
public function testEnhancerNoHasClass()
38+
{
39+
$this->pool->hasAdminByClass('stdClass')->willReturn(false);
40+
$this->resource->getPayload()->willReturn($this->payload);
41+
42+
$result = $this->enhancer->enhance(
43+
$this->context->reveal(),
44+
$this->resource->reveal()
45+
);
46+
47+
$this->visitor->addData()->shouldNotBeCalled();
48+
}
49+
50+
public function testEnhancer()
51+
{
52+
$this->pool->hasAdminByClass('stdClass')->willReturn(true);
53+
$this->pool->getAdminByClass('stdClass')->willReturn($this->admin);
54+
55+
$this->resource->getPayload()->willReturn($this->payload);
56+
57+
$result = $this->enhancer->enhance(
58+
$this->context->reveal(),
59+
$this->resource->reveal()
60+
);
61+
62+
$this->assertNull($result);
63+
}
64+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"behat/web-api-extension" : "~1.0",
2222
"behat/symfony2-extension": "~2.0@dev",
2323
"matthiasnoback/symfony-dependency-injection-test": "0.*",
24-
"matthiasnoback/symfony-config-test": "0.*"
24+
"matthiasnoback/symfony-config-test": "0.*",
25+
"sonata-project/doctrine-phpcr-admin-bundle": "~1.2"
2526
},
2627
"suggest": {
2728
"doctrine/phpcr-odm": "To enable support for the PHPCR ODM documents",

0 commit comments

Comments
 (0)