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

Commit 577f373

Browse files
committed
Merge pull request #9 from symfony-cmf/sonata_admin
SonataAdminEnhancer
2 parents 53bf3da + 1c6475a commit 577f373

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+583
-500
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ before_script:
4141

4242
script:
4343
- phpunit --coverage-text
44-
- ./vendor/symfony-cmf/testing/bin/server &
45-
- ./vendor/bin/behat
44+
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then ./vendor/symfony-cmf/testing/bin/server & fi
45+
- if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then ./vendor/bin/behat; fi
4646

4747
notifications:
4848
irc: "irc.freenode.org#symfony-cmf"

CmfResourceRestBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the Symfony CMF package.
55
*
6-
* (c) 2011-2014 Symfony CMF
6+
* (c) 2011-2015 Symfony CMF
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.

Controller/ResourceController.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Symfony\Cmf\Component\Resource\RepositoryRegistryInterface;
66
use Symfony\Component\HttpFoundation\Response;
7-
use Hateoas\UrlGenerator\UrlGeneratorInterface;
87
use JMS\Serializer\SerializerInterface;
98
use JMS\Serializer\SerializationContext;
109

@@ -15,11 +14,6 @@ class ResourceController
1514
*/
1615
private $registry;
1716

18-
/**
19-
* @var UrlGeneratorInterface
20-
*/
21-
private $urlGenerator;
22-
2317
/**
2418
* @var SerializerInterface
2519
*/
@@ -30,23 +24,24 @@ class ResourceController
3024
*/
3125
public function __construct(
3226
SerializerInterface $serializer,
33-
RepositoryRegistryInterface $registry,
34-
UrlGeneratorInterface $urlGenerator
27+
RepositoryRegistryInterface $registry
3528
) {
3629
$this->serializer = $serializer;
3730
$this->registry = $registry;
38-
$this->urlGenerator = $urlGenerator;
3931
}
4032

4133
public function resourceAction($repositoryName, $path)
4234
{
4335
$repository = $this->registry->get($repositoryName);
4436
$resource = $repository->get('/' . $path);
4537

38+
$context = SerializationContext::create();
39+
$context->enableMaxDepthChecks();
40+
$context->setSerializeNull(true);
4641
$json = $this->serializer->serialize(
4742
$resource,
4843
'json',
49-
SerializationContext::create()->enableMaxDepthChecks()
44+
$context
5045
);
5146

5247
return new Response($json);

DependencyInjection/CmfResourceRestExtension.php

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the Symfony CMF package.
55
*
6-
* (c) 2011-2014 Symfony CMF
6+
* (c) 2011-2015 Symfony CMF
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -15,32 +15,15 @@
1515
use Symfony\Component\Config\FileLocator;
1616
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1717
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18-
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
1918
use Symfony\Component\Config\Definition\Processor;
19+
use Symfony\Component\Config\Loader\LoaderInterface;
2020

21-
class CmfResourceRestExtension extends Extension implements PrependExtensionInterface
21+
class CmfResourceRestExtension extends Extension
2222
{
23-
public function prepend(ContainerBuilder $container)
24-
{
25-
$container->prependExtensionConfig('jms_serializer', array(
26-
'metadata' => array(
27-
'directories' => array(
28-
array(
29-
'path' => __DIR__ . '/../Resources/config/serializer',
30-
'namespace_prefix' => 'Symfony\Cmf\Component\Resource\Repository\Resource',
31-
),
32-
array(
33-
'path' => __DIR__ . '/../Resources/config/serializer',
34-
'namespace_prefix' => 'Puli\Repository\Resource',
35-
),
36-
array(
37-
'path' => __DIR__ . '/../Resources/config/serializer',
38-
'namespace_prefix' => 'PHPCR',
39-
),
40-
),
41-
),
42-
));
43-
}
23+
private $nativeEnhancers = array(
24+
'payload',
25+
'sonata_admin',
26+
);
4427

4528
/**
4629
* {@inheritDoc}
@@ -54,24 +37,56 @@ public function load(array $configs, ContainerBuilder $container)
5437

5538
$loader->load('serializer.xml');
5639
$loader->load('resource-rest.xml');
57-
$loader->load('enhancer.xml');
5840

41+
$this->loadEnhancers($container, $loader, $config['enhancer_map']);
5942
$this->configurePayloadAliasRegistry($container, $config['payload_alias_map']);
60-
$this->configureEnhancerMap($container, $config['enhancer_map']);
43+
$this->configureEnhancers($container, $config['enhancer_map']);
6144
}
6245

6346
public function getNamespace()
6447
{
6548
return 'http://cmf.symfony.com/schema/dic/' . $this->getAlias();
6649
}
6750

51+
/**
52+
* Automatically include native enhancers
53+
*/
54+
private function loadEnhancers(ContainerBuilder $container, LoaderInterface $loader, $enhancerMap)
55+
{
56+
$loaded = array();
57+
foreach ($enhancerMap as $unit) {
58+
$enhancerName = $unit['enhancer'];
59+
60+
if (!in_array($enhancerName, $this->nativeEnhancers)) {
61+
continue;
62+
}
63+
64+
if (isset($loaded[$enhancerName])) {
65+
continue;
66+
}
67+
68+
$loader->load('enhancer.' . $enhancerName . '.xml');
69+
$loaded[$enhancerName] = true;
70+
}
71+
72+
$bundles = $container->getParameter('kernel.bundles');
73+
74+
if (isset($loaded['sonata_admin'])) {
75+
if (!isset($bundles['SonataAdminBundle'])) {
76+
throw new \InvalidArgumentException(
77+
'You must enable the SonataAdminBundle in order to use the "sonata_admin" enhancer'
78+
);
79+
}
80+
}
81+
}
82+
6883
private function configurePayloadAliasRegistry(ContainerBuilder $container, $aliasMap)
6984
{
7085
$registry = $container->getDefinition('cmf_resource_rest.registry.payload_alias');
7186
$registry->replaceArgument(1, $aliasMap);
7287
}
7388

74-
private function configureEnhancerMap(ContainerBuilder $container, $enhancerMap)
89+
private function configureEnhancers(ContainerBuilder $container, $enhancerMap)
7590
{
7691
$enhancerMap = $this->normalizeEnhancerMap($enhancerMap);
7792
$registry = $container->getDefinition('cmf_resource_rest.registry.enhancer');

DependencyInjection/Compiler/EnhancerPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the Symfony CMF package.
55
*
6-
* (c) 2011-2014 Symfony CMF
6+
* (c) 2011-2015 Symfony CMF
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
1515
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16-
use Symfony\Component\DependencyInjection\Reference;
1716
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1817

1918
/**

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the Symfony CMF package.
55
*
6-
* (c) 2011-2014 Symfony CMF
6+
* (c) 2011-2015 Symfony CMF
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.

Enhancer/EnhancerInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of the Symfony CMF package.
55
*
6-
* (c) 2011-2014 Symfony CMF
6+
* (c) 2011-2015 Symfony CMF
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
@@ -31,5 +31,5 @@ interface EnhancerInterface
3131
* @param Context Serialization context
3232
* @param Resource The resource being serialized
3333
*/
34-
public function enhance(Context $context, Resource $resource);
34+
public function enhance(array $data, Resource $resource);
3535
}

Enhancer/PayloadEnhancer.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
/*
44
* This file is part of the Symfony CMF package.
55
*
6-
* (c) 2011-2014 Symfony CMF
6+
* (c) 2011-2015 Symfony CMF
77
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
1111

1212
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Enhancer;
1313

14-
use JMS\Serializer\Context;
1514
use Puli\Repository\Api\Resource\Resource;
1615

1716
/**
@@ -24,9 +23,11 @@ class PayloadEnhancer implements EnhancerInterface
2423
/**
2524
* {@inheritDoc}
2625
*/
27-
public function enhance(Context $context, Resource $resource)
26+
public function enhance(array $data, Resource $resource)
2827
{
29-
$visitor = $context->getVisitor();
30-
$visitor->addData('payload', $context->accept($resource->getPayload()));
28+
$payload = $resource->getPayload();
29+
$data['payload'] = $payload;
30+
31+
return $data;
3132
}
3233
}

Enhancer/SonataAdminEnhancer.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2015 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 Puli\Repository\Api\Resource\Resource;
15+
use Sonata\AdminBundle\Admin\Pool;
16+
use Doctrine\Common\Util\ClassUtils;
17+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18+
19+
/**
20+
* Add links and meta-info from Sonata Admin
21+
*
22+
* @author Daniel Leech <[email protected]>
23+
*/
24+
class SonataAdminEnhancer implements EnhancerInterface
25+
{
26+
/**
27+
* @var Pool
28+
*/
29+
private $pool;
30+
31+
/**
32+
* @var UrlGeneratorInterface
33+
*/
34+
private $urlGenerator;
35+
36+
/**
37+
* __construct
38+
*
39+
* @param Pool $pool
40+
* @param UrlGeneratorInterface $urlGenerator
41+
*/
42+
public function __construct(Pool $pool, UrlGeneratorInterface $urlGenerator)
43+
{
44+
$this->pool = $pool;
45+
$this->urlGenerator = $urlGenerator;
46+
}
47+
48+
/**
49+
* {@inheritDoc}
50+
*/
51+
public function enhance(array $data, Resource $resource)
52+
{
53+
$object = $resource->getPayload();
54+
55+
// sonata has dependency on ClassUtils so this is fine.
56+
$class = ClassUtils::getClass($object);
57+
58+
if (false === $this->pool->hasAdminByClass($class)) {
59+
return $data;
60+
}
61+
62+
$admin = $this->pool->getAdminByClass($class);
63+
64+
$links = array();
65+
66+
$routeCollection = $admin->getRoutes();
67+
68+
foreach ($routeCollection->getElements() as $code => $route) {
69+
$routeName = $route->getDefault('_sonata_name');
70+
$url = $this->urlGenerator->generate($routeName, array(
71+
$admin->getIdParameter() => $admin->getUrlsafeIdentifier($object),
72+
), true);
73+
74+
$routeRole = substr($code, strlen($admin->getCode()) + 1);
75+
76+
$links[$routeRole] = $url;
77+
}
78+
79+
$data['sonata_label'] = $admin->getLabel();
80+
$data['sonata_links'] = $links;
81+
82+
return $data;
83+
}
84+
}

Helper/PathHelper.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

0 commit comments

Comments
 (0)