Skip to content

Commit

Permalink
Fix listeners dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
picoss committed Feb 5, 2018
1 parent 1b8300e commit 2b62f84
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 49 deletions.
5 changes: 4 additions & 1 deletion DependencyInjection/Compiler/AddAuditEntityCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class AddAuditEntityCompilerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('doctrine.orm.entity_manager')) {
return;
}

$auditedEntities = array();
foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {

Expand All @@ -39,7 +43,6 @@ public function process(ContainerBuilder $container)
}

$definition = $container->getDefinition($id);
$definition->addMethodCall('setRouteBuilder', array(new Reference('picoss.sonataextraadmin.route.entity')));
$auditedEntities[] = $this->getModelName($container, $definition->getArgument(1));
}

Expand Down
5 changes: 4 additions & 1 deletion DependencyInjection/Compiler/AddTrashEntityCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class AddTrashEntityCompilerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('doctrine.orm.entity_manager')) {
return;
}

$trashedEntities = array();
foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {

Expand All @@ -39,7 +43,6 @@ public function process(ContainerBuilder $container)
}

$definition = $container->getDefinition($id);
$definition->addMethodCall('setRouteBuilder', array(new Reference('picoss.sonataextraadmin.route.entity')));
$trashedEntities[] = $this->getModelName($container, $definition->getArgument(1));
}

Expand Down
35 changes: 35 additions & 0 deletions DependencyInjection/Compiler/EntityRouterCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the YesWeHack BugBounty backend
*
* (c) Romain Honel <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Picoss\SonataExtraAdminBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class EntityRouterCompilerPass
*
* @author Romain Honel <[email protected]>
*/
class EntityRouterCompilerPass implements CompilerPassInterface
{
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
$definition = $container->getDefinition($id);
$definition->addMethodCall('setRouteBuilder', array(new Reference('picoss.sonataextraadmin.route.entity')));
}
}
}
1 change: 0 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function getConfigTreeBuilder()
->scalarNode('untrash')->defaultValue('@PicossSonataExtraAdmin/CRUD/untrash.html.twig')->cannotBeEmpty()->end()
->scalarNode('inner_trash_list_row')->defaultValue('@PicossSonataExtraAdmin/CRUD/list_trash_inner_row.html.twig')->cannotBeEmpty()->end()
->scalarNode('button_trash')->defaultValue('@PicossSonataExtraAdmin/Button/trash_button.html.twig')->cannotBeEmpty()->end()
// ->scalarNode('list')->defaultValue('@PicossSonataExtraAdmin/CRUD/base_list.html.twig')->cannotBeEmpty()->end()
->arrayNode('types')
->children()
->arrayNode('list')
Expand Down
10 changes: 9 additions & 1 deletion DependencyInjection/PicossSonataExtraAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ public function load(array $configs, ContainerBuilder $container)

$bundles = $container->getParameter('kernel.bundles');
if (isset($bundles['SonataDoctrineORMAdminBundle'])) {
$loader->load('ORM/services.xml');
$loader->load('ORM/sortable.xml');

if ($container->hasDefinition('stof_doctrine_extensions.listener.loggable')) {
$loader->load('ORM/audit.xml');
}

if ($container->hasDefinition('stof_doctrine_extensions.listener.softdeleteable')) {
$loader->load('ORM/trash.xml');
}
}

$container->registerForAutoconfiguration(ExtraAdminController::class)
Expand Down
24 changes: 18 additions & 6 deletions PicossSonataExtraAdminBundle.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
<?php

/*
* This file is part of the YesWeHack BugBounty backend
*
* (c) Romain Honel <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Picoss\SonataExtraAdminBundle;

use Picoss\SonataExtraAdminBundle\DependencyInjection\Compiler\AddAuditEntityCompilerPass;
use Picoss\SonataExtraAdminBundle\DependencyInjection\Compiler\AddTrashEntityCompilerPass;
use Picoss\SonataExtraAdminBundle\DependencyInjection\Compiler\EntityRouterCompilerPass;
use Picoss\SonataExtraAdminBundle\DependencyInjection\Compiler\FormPass;
use Picoss\SonataExtraAdminBundle\DependencyInjection\Compiler\SonataTemplatesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
* Class PicossSonataExtraAdminBundle
*
* @author Romain Honel <[email protected]>
*/
class PicossSonataExtraAdminBundle extends Bundle
{
/**
Expand All @@ -19,11 +34,8 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new SonataTemplatesPass());

$bundles = $container->getParameter('kernel.bundles');
if (isset($bundles['SonataDoctrineORMAdminBundle'])) {
$container->addCompilerPass(new AddAuditEntityCompilerPass());
$container->addCompilerPass(new AddTrashEntityCompilerPass());
}
$container->addCompilerPass(new AddAuditEntityCompilerPass());
$container->addCompilerPass(new AddTrashEntityCompilerPass());
$container->addCompilerPass(new EntityRouterCompilerPass());
}
}
12 changes: 12 additions & 0 deletions Resources/config/ORM/audit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="picoss.sonata.extra.admin.audit.orm.reader" class="Picoss\SonataExtraAdminBundle\Model\AuditReader" public="true">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="stof_doctrine_extensions.listener.loggable" />
</service>
</services>
</container>
33 changes: 0 additions & 33 deletions Resources/config/ORM/services.xml

This file was deleted.

15 changes: 15 additions & 0 deletions Resources/config/ORM/sortable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="picoss.sonataextraadmin.handler.sortable.class">Picoss\SonataExtraAdminBundle\Handler\SortableHandler</parameter>
</parameters>
<services>
<service id="picoss.sonataextraadmin.handler.sortable" class="%picoss.sonataextraadmin.handler.sortable.class%">
<argument type="service" id="doctrine.orm.entity_manager"/>
</service>
<service id="%picoss.sonataextraadmin.handler.sortable.class%" alias="picoss.sonataextraadmin.handler.sortable" />
</services>
</container>
13 changes: 13 additions & 0 deletions Resources/config/ORM/trash.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="Picoss\SonataExtraAdminBundle\Model\TrashManager" alias="picoss.sonataextraadmin.trash.manager" />
<service id="picoss.sonata.extra.admin.trash.orm.reader" class="Picoss\SonataExtraAdminBundle\Model\TrashReader" public="true">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="stof_doctrine_extensions.listener.softdeleteable" />
</service>
</services>
</container>
7 changes: 7 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
<service id="picoss.sonataextraadmin.twig.extension.stringloader" class="Twig_Extension_StringLoader">
<tag name="twig.extension"/>
</service>
<service id="picoss.sonataextraadmin.trash.manager" class="Picoss\SonataExtraAdminBundle\Model\TrashManager">
<argument type="service" id="service_container" />
</service>
<service id="picoss.sonataextraadmin.route.entity" class="Picoss\SonataExtraAdminBundle\Route\EntityRouterBuilder">
<argument type="service" id="sonata.admin.audit.manager"/>
<argument type="service" id="picoss.sonataextraadmin.trash.manager"/>
</service>
</services>
</container>
30 changes: 24 additions & 6 deletions Route/EntityRouterBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,33 @@ class EntityRouterBuilder extends PathInfoBuilder implements RouteBuilderInterfa
*/
protected $trashManager;

/**
* @param AuditManagerInterface $manager
*/
public function __construct(AuditManagerInterface $manager, TrashManagerInterface $trashManager)
{
$this->manager = $manager;
parent::__construct($manager);

$this->trashManager = $trashManager;
}

// /**
// * Set audit manager
// *
// * @param AuditManagerInterface $manager
// */
// public function setAuditManager(AuditManagerInterface $manager)
// {
// $this->manager = $manager;
// }
//
// /**
// * Set trash manager
// *
// * @param TrashManagerInterface $trashManager
// */
// public function setTrashManager(TrashManagerInterface $trashManager)
// {
// $this->trashManager = $trashManager;
// }

/**
* @param AdminInterface $admin
* @param RouteCollection $collection
Expand All @@ -52,11 +70,11 @@ public function build(AdminInterface $admin, RouteCollection $collection)
{
parent::build($admin, $collection);

if ($this->manager->hasReader($admin->getClass())) {
if ($this->manager && $this->manager->hasReader($admin->getClass())) {
$collection->add('history_revert', $admin->getRouterIdParameter() . '/history/{revision}/revert');
}

if ($this->trashManager->hasReader($admin->getClass())) {
if ($this->trashManager && $this->trashManager->hasReader($admin->getClass())) {
$collection->add('trash', 'trash');
$collection->add('untrash', $admin->getRouterIdParameter() . '/untrash');
}
Expand Down

0 comments on commit 2b62f84

Please sign in to comment.