Skip to content

Commit

Permalink
fix symfony deprecations for event dispatching
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaicher authored and greg0ire committed Jan 26, 2020
1 parent 567b58a commit 73e0e8b
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 41 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -35,6 +35,7 @@
"symfony/dependency-injection": "^4.3",
"symfony/doctrine-bridge": "^4.3",
"symfony/event-dispatcher": "^4.3",
"symfony/event-dispatcher-contracts": "^1.1",
"symfony/expression-language": "^4.3",
"symfony/form": "^4.0",
"symfony/framework-bundle": "^4.3",
Expand Down
47 changes: 24 additions & 23 deletions src/Event/AdminEventExtension.php
Expand Up @@ -21,6 +21,7 @@
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;

/**
* @final since sonata-project/admin-bundle 3.52
Expand All @@ -33,94 +34,94 @@ class AdminEventExtension extends AbstractAdminExtension

public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
$this->eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
}

public function configureFormFields(FormMapper $form)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.configure.form',
new ConfigureEvent($form->getAdmin(), $form, ConfigureEvent::TYPE_FORM)
new ConfigureEvent($form->getAdmin(), $form, ConfigureEvent::TYPE_FORM),
'sonata.admin.event.configure.form'
);
}

public function configureListFields(ListMapper $list)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.configure.list',
new ConfigureEvent($list->getAdmin(), $list, ConfigureEvent::TYPE_LIST)
new ConfigureEvent($list->getAdmin(), $list, ConfigureEvent::TYPE_LIST),
'sonata.admin.event.configure.list'
);
}

public function configureDatagridFilters(DatagridMapper $filter)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.configure.datagrid',
new ConfigureEvent($filter->getAdmin(), $filter, ConfigureEvent::TYPE_DATAGRID)
new ConfigureEvent($filter->getAdmin(), $filter, ConfigureEvent::TYPE_DATAGRID),
'sonata.admin.event.configure.datagrid'
);
}

public function configureShowFields(ShowMapper $show)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.configure.show',
new ConfigureEvent($show->getAdmin(), $show, ConfigureEvent::TYPE_SHOW)
new ConfigureEvent($show->getAdmin(), $show, ConfigureEvent::TYPE_SHOW),
'sonata.admin.event.configure.show'
);
}

public function configureQuery(AdminInterface $admin, ProxyQueryInterface $query, $context = 'list')
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.configure.query',
new ConfigureQueryEvent($admin, $query, $context)
new ConfigureQueryEvent($admin, $query, $context),
'sonata.admin.event.configure.query'
);
}

public function preUpdate(AdminInterface $admin, $object)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.persistence.pre_update',
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_PRE_UPDATE)
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_PRE_UPDATE),
'sonata.admin.event.persistence.pre_update'
);
}

public function postUpdate(AdminInterface $admin, $object)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.persistence.post_update',
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_POST_UPDATE)
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_POST_UPDATE),
'sonata.admin.event.persistence.post_update'
);
}

public function prePersist(AdminInterface $admin, $object)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.persistence.pre_persist',
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_PRE_PERSIST)
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_PRE_PERSIST),
'sonata.admin.event.persistence.pre_persist'
);
}

public function postPersist(AdminInterface $admin, $object)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.persistence.post_persist',
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_POST_PERSIST)
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_POST_PERSIST),
'sonata.admin.event.persistence.post_persist'
);
}

public function preRemove(AdminInterface $admin, $object)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.persistence.pre_remove',
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_PRE_REMOVE)
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_PRE_REMOVE),
'sonata.admin.event.persistence.pre_remove'
);
}

public function postRemove(AdminInterface $admin, $object)
{
$this->eventDispatcher->dispatch(
'sonata.admin.event.persistence.post_remove',
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_POST_REMOVE)
new PersistenceEvent($admin, $object, PersistenceEvent::TYPE_POST_REMOVE),
'sonata.admin.event.persistence.post_remove'
);
}
}
2 changes: 1 addition & 1 deletion src/Event/ConfigureEvent.php
Expand Up @@ -15,7 +15,7 @@

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Mapper\BaseMapper;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

/**
* This event is sent by hook:
Expand Down
2 changes: 1 addition & 1 deletion src/Event/ConfigureMenuEvent.php
Expand Up @@ -15,7 +15,7 @@

use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Menu builder event. Used for extending the menus.
Expand Down
2 changes: 1 addition & 1 deletion src/Event/ConfigureQueryEvent.php
Expand Up @@ -15,7 +15,7 @@

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

/**
* This event is sent by hook:
Expand Down
2 changes: 1 addition & 1 deletion src/Event/PersistenceEvent.php
Expand Up @@ -14,7 +14,7 @@
namespace Sonata\AdminBundle\Event;

use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\Event;

/**
* This event is sent by hook:
Expand Down
5 changes: 3 additions & 2 deletions src/Menu/MenuBuilder.php
Expand Up @@ -19,6 +19,7 @@
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\Event\ConfigureMenuEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;

/**
* Sonata menu builder.
Expand Down Expand Up @@ -59,7 +60,7 @@ public function __construct(
$this->pool = $pool;
$this->factory = $factory;
$this->provider = $provider;
$this->eventDispatcher = $eventDispatcher;
$this->eventDispatcher = LegacyEventDispatcherProxy::decorate($eventDispatcher);
}

/**
Expand Down Expand Up @@ -93,7 +94,7 @@ public function createSidebarMenu()
}

$event = new ConfigureMenuEvent($this->factory, $menu);
$this->eventDispatcher->dispatch(ConfigureMenuEvent::SIDEBAR, $event);
$this->eventDispatcher->dispatch($event, ConfigureMenuEvent::SIDEBAR);

return $event->getMenu();
}
Expand Down
22 changes: 12 additions & 10 deletions tests/Event/AdminEventExtensionTest.php
Expand Up @@ -21,6 +21,7 @@
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Event\AdminEventExtension;
use Sonata\AdminBundle\Event\ConfigureEvent;
use Sonata\AdminBundle\Event\ConfigureQueryEvent;
use Sonata\AdminBundle\Event\PersistenceEvent;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Show\ShowMapper;
Expand Down Expand Up @@ -79,8 +80,8 @@ public function testConfigureFormFields(): void
{
$this
->getExtension([
$this->equalTo('sonata.admin.event.configure.form'),
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_FORM)),
$this->equalTo('sonata.admin.event.configure.form'),
])
->configureFormFields($this->getMapper(FormMapper::class));
}
Expand All @@ -89,8 +90,8 @@ public function testConfigureListFields(): void
{
$this
->getExtension([
$this->equalTo('sonata.admin.event.configure.list'),
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_LIST)),
$this->equalTo('sonata.admin.event.configure.list'),
])
->configureListFields($this->getMapper(ListMapper::class));
}
Expand All @@ -99,8 +100,8 @@ public function testConfigureDatagridFields(): void
{
$this
->getExtension([
$this->equalTo('sonata.admin.event.configure.datagrid'),
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_DATAGRID)),
$this->equalTo('sonata.admin.event.configure.datagrid'),
])
->configureDatagridFilters($this->getMapper(DatagridMapper::class));
}
Expand All @@ -109,64 +110,65 @@ public function testConfigureShowFields(): void
{
$this
->getExtension([
$this->equalTo('sonata.admin.event.configure.show'),
$this->callback($this->getConfigureEventClosure(ConfigureEvent::TYPE_SHOW)),
$this->equalTo('sonata.admin.event.configure.show'),
])
->configureShowFields($this->getMapper(ShowMapper::class));
}

public function testPreUpdate(): void
{
$this->getExtension([
$this->equalTo('sonata.admin.event.persistence.pre_update'),
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_UPDATE)),
$this->equalTo('sonata.admin.event.persistence.pre_update'),
])->preUpdate($this->createMock(AdminInterface::class), new \stdClass());
}

public function testConfigureQuery(): void
{
$this->getExtension([
$this->isInstanceOf(ConfigureQueryEvent::class),
$this->equalTo('sonata.admin.event.configure.query'),
])->configureQuery($this->createMock(AdminInterface::class), $this->createMock(ProxyQueryInterface::class));
}

public function testPostUpdate(): void
{
$this->getExtension([
$this->equalTo('sonata.admin.event.persistence.post_update'),
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_UPDATE)),
$this->equalTo('sonata.admin.event.persistence.post_update'),
])->postUpdate($this->createMock(AdminInterface::class), new \stdClass());
}

public function testPrePersist(): void
{
$this->getExtension([
$this->equalTo('sonata.admin.event.persistence.pre_persist'),
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_PERSIST)),
$this->equalTo('sonata.admin.event.persistence.pre_persist'),
])->prePersist($this->createMock(AdminInterface::class), new \stdClass());
}

public function testPostPersist(): void
{
$this->getExtension([
$this->equalTo('sonata.admin.event.persistence.post_persist'),
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_PERSIST)),
$this->equalTo('sonata.admin.event.persistence.post_persist'),
])->postPersist($this->createMock(AdminInterface::class), new \stdClass());
}

public function testPreRemove(): void
{
$this->getExtension([
$this->equalTo('sonata.admin.event.persistence.pre_remove'),
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_PRE_REMOVE)),
$this->equalTo('sonata.admin.event.persistence.pre_remove'),
])->preRemove($this->createMock(AdminInterface::class), new \stdClass());
}

public function testPostRemove(): void
{
$this->getExtension([
$this->equalTo('sonata.admin.event.persistence.post_remove'),
$this->callback($this->getConfigurePersistenceClosure(PersistenceEvent::TYPE_POST_REMOVE)),
$this->equalTo('sonata.admin.event.persistence.post_remove'),
])->postRemove($this->createMock(AdminInterface::class), new \stdClass());
}
}
4 changes: 2 additions & 2 deletions tests/Menu/MenuBuilderTest.php
Expand Up @@ -136,8 +136,8 @@ public function testGetKnpMenuAndDispatchEvent(): void
->expects($this->once())
->method('dispatch')
->with(
$this->equalTo('sonata.admin.event.configure.menu.sidebar'),
$this->isInstanceOf(ConfigureMenuEvent::class)
$this->isInstanceOf(ConfigureMenuEvent::class),
$this->equalTo('sonata.admin.event.configure.menu.sidebar')
);

$this->builder->createSidebarMenu();
Expand Down

0 comments on commit 73e0e8b

Please sign in to comment.