Skip to content

Commit

Permalink
Make sonata-project/notification-bundle optional
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Dec 18, 2020
1 parent b634826 commit 55163fa
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"sonata-project/doctrine-extensions": "^1.8",
"sonata-project/doctrine-orm-admin-bundle": "^3.19",
"sonata-project/form-extensions": "^0.1.1 || ^1.4",
"sonata-project/notification-bundle": "^3.8",
"sonata-project/seo-bundle": "^2.11",
"sonata-project/twig-extensions": "^0.1.1 || ^1.3",
"symfony-cmf/routing-bundle": "^2.1",
Expand Down Expand Up @@ -65,6 +64,7 @@
"jms/serializer-bundle": "^2.0 || ^3.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.1.1",
"nelmio/api-doc-bundle": "^2.4",
"sonata-project/notification-bundle": "^3.8",
"symfony/browser-kit": "^4.4",
"symfony/css-selector": "^4.4",
"symfony/phpunit-bridge": "^5.1.4"
Expand Down
45 changes: 40 additions & 5 deletions src/Admin/Extension/CreateSnapshotAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\NotificationBundle\Backend\BackendInterface;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Publisher\Publisher;

class CreateSnapshotAdminExtension extends AbstractAdminExtension
{
Expand All @@ -26,9 +27,37 @@ class CreateSnapshotAdminExtension extends AbstractAdminExtension
*/
protected $backend;

public function __construct(BackendInterface $backend)
/**
* @var Publisher
*/
private $publisher;

/**
* @param Publisher|BackendInterface $publisherOrBackend
*/
public function __construct(object $publisherOrBackend)
{
$this->backend = $backend;
if ($publisherOrBackend instanceof Publisher) {
$this->publisher = $publisherOrBackend;
} elseif ($publisherOrBackend instanceof BackendInterface) {
@trigger_error(sprintf(
'Passing %s as argument 1 to %s() is deprecated since sonata-project/page-bundle 3.x'
.' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
BackendInterface::class,
__METHOD__,
Publisher::class
), E_USER_DEPRECATED);

$this->backend = $publisherOrBackend;
} else {
throw new TypeError(sprintf(
'Argument 1 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
__METHOD__,
BackendInterface::class,
Publisher::class,
\get_class($publisherOrBackend)
));
}
}

public function postUpdate(AdminInterface $admin, $object)
Expand All @@ -47,15 +76,21 @@ public function postPersist(AdminInterface $admin, $object)
protected function sendMessage($object)
{
if ($object instanceof BlockInterface && method_exists($object, 'getPage')) {
$pageId = $object->getPage()->getId();
$page = $object->getPage();
} elseif ($object instanceof PageInterface) {
$pageId = $object->getId();
$page = $object;
} else {
return;
}

if (null !== $this->publisher) {
$this->publisher->createSnapshot($page);

return;
}

$this->backend->createAndPublish('sonata.page.create_snapshot', [
'pageId' => $pageId,
'pageId' => $page->getId(),
]);
}
}
2 changes: 1 addition & 1 deletion src/CmsManager/CmsPageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getInternalRoute(SiteInterface $site, $pageName)
$page = $this->pageManager->create([
'url' => null,
'routeName' => $routeName,
'name' => sprintf(sprintf('Internal Page : %s', $pageName)),
'name' => sprintf('Internal Page : %s', $pageName),
'decorate' => false,
]);

Expand Down
6 changes: 6 additions & 0 deletions src/Command/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sonata\PageBundle\Model\PageManagerInterface;
use Sonata\PageBundle\Model\SiteManagerInterface;
use Sonata\PageBundle\Model\SnapshotManagerInterface;
use Sonata\PageBundle\Publisher\Publisher;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;

Expand Down Expand Up @@ -101,6 +102,11 @@ public function getNotificationBackend($mode)
return $this->getContainer()->get('sonata.notification.backend.runtime');
}

final protected function getPublisher(): Publisher
{
return $this->getContainer()->get(Publisher::class);
}

/**
* @return array
*/
Expand Down
17 changes: 11 additions & 6 deletions src/Command/CleanupSnapshotsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function configure()

$this->addOption('site', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Site id', null);
$this->addOption('base-console', null, InputOption::VALUE_OPTIONAL, 'Base Symfony console command', 'app/console');
$this->addOption('mode', null, InputOption::VALUE_OPTIONAL, 'Run the command asynchronously', 'sync');
$this->addOption('mode', null, InputOption::VALUE_OPTIONAL, '[DEPRECATED] Run the command asynchronously', 'sync');
$this->addOption('keep-snapshots', null, InputOption::VALUE_OPTIONAL, 'Keep a given count of snapshots per page', 5);
}

Expand Down Expand Up @@ -68,11 +68,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->write(sprintf('<info>%s</info> - Cleaning up snapshots ...', $site->getName()));
}

$this->getNotificationBackend($input->getOption('mode'))->createAndPublish('sonata.page.cleanup_snapshots', [
'siteId' => $site->getId(),
'mode' => $input->getOption('mode'),
'keepSnapshots' => $input->getOption('keep-snapshots'),
]);
if ('asnc' === $input->getOption('mode')) {
$this->getNotificationBackend('async')
->createAndPublish('sonata.page.cleanup_snapshots', [
'siteId' => $site->getId(),
'mode' => $input->getOption('mode'),
'keepSnapshots' => $input->getOption('keep-snapshots'),
]);
} else {
$this->getPublisher()->createSnapshots($site, (int) $input->getOption('keep-snapshots'));
}

$output->writeln(' done!');
} else {
Expand Down
15 changes: 10 additions & 5 deletions src/Command/CreateSnapshotsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function configure()
$this->addOption('site', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Site id', null);
$this->addOption('base-console', null, InputOption::VALUE_OPTIONAL, 'Base symfony console command', 'php app/console');

$this->addOption('mode', null, InputOption::VALUE_OPTIONAL, 'Run the command asynchronously', 'sync');
$this->addOption('mode', null, InputOption::VALUE_OPTIONAL, '[DEPRECATED] Run the command asynchronously', 'sync');
}

public function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -58,10 +58,15 @@ public function execute(InputInterface $input, OutputInterface $output)
$output->write(sprintf('<info>%s</info> - Generating snapshots ...', $site->getName()));
}

$this->getNotificationBackend($input->getOption('mode'))->createAndPublish('sonata.page.create_snapshots', [
'siteId' => $site->getId(),
'mode' => $input->getOption('mode'),
]);
if ('asnc' === $input->getOption('mode')) {
$this->getNotificationBackend('async')
->createAndPublish('sonata.page.create_snapshots', [
'siteId' => $site->getId(),
'mode' => $input->getOption('mode'),
]);
} else {
$this->getPublisher()->createSnapshots($site, (int) $input->getOption('keep-snapshots'));
}

$output->writeln(' done!');
} else {
Expand Down
51 changes: 45 additions & 6 deletions src/Controller/Api/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\PageManagerInterface;
use Sonata\PageBundle\Model\SiteManagerInterface;
use Sonata\PageBundle\Publisher\Publisher;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -59,13 +60,47 @@ class PageController extends FOSRestController
*/
protected $backend;

public function __construct(SiteManagerInterface $siteManager, PageManagerInterface $pageManager, BlockManagerInterface $blockManager, FormFactoryInterface $formFactory, BackendInterface $backend)
{
/**
* @var Publisher
*/
private $publisher;

/**
* @param Publisher|BackendInterface $publisherOrBackend
*/
public function __construct(
SiteManagerInterface $siteManager,
PageManagerInterface $pageManager,
BlockManagerInterface $blockManager,
FormFactoryInterface $formFactory,
object $publisherOrBackend
) {
$this->siteManager = $siteManager;
$this->pageManager = $pageManager;
$this->blockManager = $blockManager;
$this->formFactory = $formFactory;
$this->backend = $backend;

if ($publisherOrBackend instanceof Publisher) {
$this->publisher = $publisherOrBackend;
} elseif ($publisherOrBackend instanceof BackendInterface) {
@trigger_error(sprintf(
'Passing %s as argument 4 to %s() is deprecated since sonata-project/page-bundle 3.x'
.' and will throw a \TypeError in version 4.0. You must pass an instance of %s instead.',
BackendInterface::class,
__METHOD__,
Publisher::class
), E_USER_DEPRECATED);

$this->backend = $publisherOrBackend;
} else {
throw new \TypeError(sprintf(
'Argument 4 passed to %s() must be either null or an instance of %s or preferably %s, instance of %s given.',
__METHOD__,
BackendInterface::class,
Publisher::class,
\get_class($publisherOrBackend)
));
}
}

/**
Expand Down Expand Up @@ -352,9 +387,13 @@ public function postPageSnapshotAction($id)
{
$page = $this->getPage($id);

$this->backend->createAndPublish('sonata.page.create_snapshot', [
'pageId' => $page->getId(),
]);
if (null !== $this->publisher) {
$this->publisher->createSnapshot($page);
} else {
$this->backend->createAndPublish('sonata.page.create_snapshot', [
'pageId' => $page->getId(),
]);
}

return ['queued' => true];
}
Expand Down
8 changes: 4 additions & 4 deletions src/Controller/PageAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\PageBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Sonata\PageBundle\Publisher\Publisher;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand All @@ -40,11 +41,10 @@ public function batchActionSnapshot($query)
throw new AccessDeniedException();
}

$publisher = $this->get(Publisher::class);

foreach ($query->execute() as $page) {
$this->get('sonata.notification.backend')
->createAndPublish('sonata.page.create_snapshot', [
'pageId' => $page->getId(),
]);
$publisher->createSnapshot($page);
}

return new RedirectResponse($this->admin->generateUrl('list', [
Expand Down
7 changes: 2 additions & 5 deletions src/Controller/SiteAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\PageBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController as Controller;
use Sonata\PageBundle\Publisher\Publisher;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
Expand Down Expand Up @@ -50,11 +51,7 @@ public function snapshotsAction()
$this->admin->setSubject($object);

if ('POST' === $request->getMethod()) {
$this->get('sonata.notification.backend')
->createAndPublish('sonata.page.create_snapshots', [
'siteId' => $object->getId(),
'mode' => 'async',
]);
$this->get(Publisher::class)->createSnapshots($object);

$this->addFlash('sonata_flash_success', $this->admin->trans('flash_snapshots_created_success'));

Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Sonata\PageBundle\DependencyInjection;

use Sonata\PageBundle\Model\Template;
use Sonata\PageBundle\Publisher\SimplePublisher;
use Sonata\PageBundle\Template\Matrix\Parser;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -368,6 +369,10 @@ public function getConfigTreeBuilder()
->info($directPublicationInfo)
->defaultValue(false)
->end()

->scalarNode('publisher')
->defaultValue(SimplePublisher::class)
->end()
;

return $treeBuilder;
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/SonataPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sonata\Doctrine\Mapper\DoctrineCollector;
use Sonata\EasyExtendsBundle\Mapper\DoctrineCollector as DeprecatedDoctrineCollector;
use Sonata\PageBundle\Model\Template;
use Sonata\PageBundle\Publisher\Publisher;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -113,6 +114,8 @@ public function load(array $configs, ContainerBuilder $container)
->replaceArgument(1, $config['ignore_route_patterns'])
->replaceArgument(2, $config['ignore_uri_patterns']);

$container->setAlias(Publisher::class, $config['publisher']);

if (isset($bundles['SonataDoctrineBundle'])) {
$this->registerSonataDoctrineMapping($config);
} else {
Expand Down
61 changes: 61 additions & 0 deletions src/Publisher/NotificationPublisher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\PageBundle\Publisher;

use Sonata\NotificationBundle\Backend\BackendInterface;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Model\SiteInterface;

final class NotificationPublisher implements Publisher
{
/**
* @var BackendInterface
*/
private $backend;

public function __construct(BackendInterface $backend)
{
$this->backend = $backend;
}

public function removeSnapshots(SiteInterface $site, int $keep = 0): void
{
$this->backend->createAndPublish('sonata.page.cleanup_snapshot', [
'siteId' => $site->getId(),
'keepSnapshots' => $keep,
]);
}

public function removeSnapshot(PageInterface $page, int $keep = 0): void
{
$this->backend->createAndPublish('sonata.page.cleanup_snapshot', [
'pageId' => $page->getId(),
'keepSnapshots' => $keep,
]);
}

public function createSnapshots(SiteInterface $site): void
{
$this->backend->createAndPublish('sonata.page.create_snapshots', [
'siteId' => $site->getId(),
]);
}

public function createSnapshot(PageInterface $page): void
{
$this->backend->createAndPublish('sonata.page.create_snapshot', [
'pageId' => $page->getId(),
]);
}
}
Loading

0 comments on commit 55163fa

Please sign in to comment.