Commit 49686906 authored by Tino Goratsch's avatar Tino Goratsch

refactored the feeds module to use the new compiler pass functionality

parent 0f0ab35d
......@@ -19,21 +19,28 @@ class Index extends Core\Controller\AbstractFrontendAction
use Core\Cache\CacheResponseTrait;
/**
* @var \ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator
* @var \ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator
*/
protected $feedGenerator;
/**
* @var Feeds\Utility\AvailableFeedsRegistrar
*/
protected $availableFeedsRegistrar;
/**
* @param \ACP3\Core\Controller\Context\FrontendContext $context
* @param \ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator $feedGenerator
* @param \ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator $feedGenerator
* @param Feeds\Utility\AvailableFeedsRegistrar $availableFeedsRegistrar
*/
public function __construct(
Core\Controller\Context\FrontendContext $context,
Feeds\Helper\FeedGenerator $feedGenerator)
{
Feeds\View\Renderer\FeedGenerator $feedGenerator,
Feeds\Utility\AvailableFeedsRegistrar $availableFeedsRegistrar
) {
parent::__construct($context);
$this->feedGenerator = $feedGenerator;
$this->availableFeedsRegistrar = $availableFeedsRegistrar;
}
/**
......@@ -49,17 +56,21 @@ class Index extends Core\Controller\AbstractFrontendAction
$this->config->getSettings(Schema::MODULE_NAME)['cache_lifetime']
);
$this->feedGenerator
->setTitle($this->config->getSettings(Schema::MODULE_NAME)['site_title'])
->setDescription($this->translator->t($feed, $feed));
try {
$feedItems = $this->availableFeedsRegistrar
->getFeedItemsByModuleName($feed)
->fetchFeedItems();
$this->eventDispatcher->dispatch(
'feeds.events.displayFeed.' . strtolower($feed),
new Feeds\Event\DisplayFeed($this->feedGenerator)
);
$this->feedGenerator
->setTitle($this->config->getSettings(Schema::MODULE_NAME)['site_title'])
->setDescription($this->translator->t($feed, $feed))
->assign($feedItems);
$this->setContentType('text/xml');
return $this->response->setContent($this->feedGenerator->generateFeed());
} catch (\InvalidArgumentException $e) {
$this->setContentType('text/xml');
return $this->response->setContent($this->feedGenerator->generateFeed());
}
}
throw new Core\Controller\Exception\ResultNotExistsException();
......
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Feeds\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
class FeedAvailabilityCompilerPass implements CompilerPassInterface
{
/**
* You can modify the container here before it is dumped to PHP code.
*
* @param ContainerBuilder $container
*/
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('feeds.utility.available_feeds_registrar');
$plugins = $container->findTaggedServiceIds('feeds.available_module');
foreach ($plugins as $serviceId => $tags) {
$definition->addMethodCall(
'registerModule',
[new Reference($serviceId)]
);
}
}
}
......@@ -6,7 +6,7 @@
namespace ACP3\Modules\ACP3\Feeds\Event;
use ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator;
use ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator;
use Symfony\Component\EventDispatcher\Event;
/**
......@@ -16,12 +16,12 @@ use Symfony\Component\EventDispatcher\Event;
class DisplayFeed extends Event
{
/**
* @var \ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator
* @var \ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator
*/
protected $feedGenerator;
/**
* @param \ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator $feedGenerator
* @param \ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator $feedGenerator
*/
public function __construct(FeedGenerator $feedGenerator)
{
......@@ -29,7 +29,7 @@ class DisplayFeed extends Event
}
/**
* @return \ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator
* @return \ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator
*/
public function getFeedGenerator()
{
......
......@@ -8,6 +8,7 @@ namespace ACP3\Modules\ACP3\Feeds\Event\Listener;
use ACP3\Core\View;
use ACP3\Modules\ACP3\Feeds\Utility\AvailableFeedsRegistrar;
class OnLayoutHeadListener
{
......@@ -15,18 +16,28 @@ class OnLayoutHeadListener
* @var View
*/
private $view;
/**
* @var AvailableFeedsRegistrar
*/
private $availableFeedsRegistrar;
/**
* OnLayoutHeadListener constructor.
* @param View $view
* @param AvailableFeedsRegistrar $availableFeedsRegistrar
*/
public function __construct(View $view)
{
public function __construct(
View $view,
AvailableFeedsRegistrar $availableFeedsRegistrar
) {
$this->view = $view;
$this->availableFeedsRegistrar = $availableFeedsRegistrar;
}
public function renderFeedLinkTags()
{
echo $this->view->fetchTemplate('Feeds/Partials/feed_link_tags.tpl');
$this->view->assign('available_feeds', $this->availableFeedsRegistrar->getAvailableModuleNames());
$this->view->displayTemplate('Feeds/Partials/feed_link_tags.tpl');
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Feeds;
use ACP3\Modules\ACP3\Feeds\DependencyInjection\FeedAvailabilityCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class ModuleRegistration extends \ACP3\Modules\ACP3\Search\ModuleRegistration
{
public function build(ContainerBuilder $containerBuilder)
{
$containerBuilder->addCompilerPass(new FeedAvailabilityCompilerPass());
}
}
<link rel="alternate"
type="application/rss+xml"
href="{uri args="feeds/index/index/feed_news"}"
title="{site_title} - {lang t="news|news"}">
<link rel="alternate"
type="application/rss+xml"
href="{uri args="feeds/index/index/feed_files"}"
title="{site_title} - {lang t="files|files"}">
{foreach $available_feeds as $feed}
<link rel="alternate"
type="application/rss+xml"
href="{uri args="feeds/index/index/feed_`$feed`"}"
title="{site_title} - {lang t="`$feed`|`$feed`"}">
{/foreach}
......@@ -11,5 +11,5 @@ services:
class: ACP3\Modules\ACP3\Feeds\Controller\Frontend\Index\Index
arguments:
- '@core.context.frontend'
- '@feeds.helper.feedgenerator'
- '@feeds.view.renderer.feedgenerator'
- '@feeds.utility.available_feeds_registrar'
......@@ -3,5 +3,6 @@ services:
class: ACP3\Modules\ACP3\Feeds\Event\Listener\OnLayoutHeadListener
arguments:
- '@core.view'
- '@feeds.utility.available_feeds_registrar'
tags:
- { name: core.eventListener, event: core.layout.head, method: renderFeedLinkTags }
......@@ -5,8 +5,11 @@ imports:
- { resource: components/validation.yml }
services:
feeds.helper.feedgenerator:
class: ACP3\Modules\ACP3\Feeds\Helper\FeedGenerator
feeds.view.renderer.feedgenerator:
class: ACP3\Modules\ACP3\Feeds\View\Renderer\FeedGenerator
arguments:
- '@core.config'
- '@core.router'
feeds.utility.available_feeds_registrar:
class: ACP3\Modules\ACP3\Feeds\Utility\AvailableFeedsRegistrar
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Feeds\Utility;
class AvailableFeedsRegistrar
{
/**
* @var FeedAvailabilityInterface[]
*/
protected $availableModules = [];
/**
* @param FeedAvailabilityInterface $searchAvailability
* @return $this
*/
public function registerModule(FeedAvailabilityInterface $searchAvailability)
{
$this->availableModules[$searchAvailability->getModuleName()] = $searchAvailability;
return $this;
}
/**
* @return array
*/
public function getAvailableModuleNames()
{
return array_keys($this->availableModules);
}
/**
* @param string $moduleName
* @return FeedAvailabilityInterface
*/
public function getFeedItemsByModuleName($moduleName)
{
if (isset($this->availableModules[$moduleName])) {
return $this->availableModules[$moduleName];
}
throw new \InvalidArgumentException('There are no available feeds items for the requested module "' . $moduleName . '".');
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Feeds\Utility;
interface FeedAvailabilityInterface
{
/**
* @return string
*/
public function getModuleName();
/**
* @return array
*/
public function fetchFeedItems();
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Feeds\Helper;
namespace ACP3\Modules\ACP3\Feeds\View\Renderer;
use ACP3\Core\Router\RouterInterface;
use ACP3\Core\Settings\SettingsInterface;
......@@ -9,7 +13,7 @@ use FeedWriter\ATOM;
/**
* Renderer for the output of RSS and ATOM News feeds
* @package ACP3\Modules\ACP3\Feeds\Helpe
* @package ACP3\Modules\ACP3\Feeds\View\Renderer
*/
class FeedGenerator
{
......@@ -46,8 +50,8 @@ class FeedGenerator
*/
public function __construct(
SettingsInterface $config,
RouterInterface $router)
{
RouterInterface $router
) {
$this->config = $config;
$this->router = $router;
}
......
<?php
namespace ACP3\Modules\ACP3\Files\Event\Listener;
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\Files\Feeds;
use ACP3\Core\Date;
use ACP3\Core\Helpers\StringFormatter;
use ACP3\Core\Router\RouterInterface;
use ACP3\Modules\ACP3\Feeds\Event\DisplayFeed;
use ACP3\Modules\ACP3\Feeds\Utility\FeedAvailabilityInterface;
use ACP3\Modules\ACP3\Files\Installer\Schema;
use ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository;
/**
* Class OnDisplayFeedListener
* @package ACP3\Modules\ACP3\Files\Event\Listener
*/
class OnDisplayFeedListener
class FeedAvailability implements FeedAvailabilityInterface
{
/**
* @var \ACP3\Core\Date
......@@ -33,9 +36,9 @@ class OnDisplayFeedListener
/**
* OnDisplayFeedListener constructor.
*
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Router\RouterInterface $router
* @param \ACP3\Core\Helpers\StringFormatter $formatter
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Router\RouterInterface $router
* @param \ACP3\Core\Helpers\StringFormatter $formatter
* @param \ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository $filesRepository
*/
public function __construct(
......@@ -51,9 +54,17 @@ class OnDisplayFeedListener
}
/**
* @param \ACP3\Modules\ACP3\Feeds\Event\DisplayFeed $displayFeed
* @return string
*/
public function getModuleName()
{
return Schema::MODULE_NAME;
}
/**
* @return array
*/
public function onDisplayFeed(DisplayFeed $displayFeed)
public function fetchFeedItems()
{
$items = [];
$results = $this->filesRepository->getAll($this->date->getCurrentDateTime(), 10);
......@@ -68,6 +79,6 @@ class OnDisplayFeedListener
];
}
$displayFeed->getFeedGenerator()->assign($items);
return $items;
}
}
......@@ -12,16 +12,6 @@ services:
tags:
- { name: core.eventListener, event: files.model.files.before_delete, method: execute }
files.event.on_display_feed_listener:
class: ACP3\Modules\ACP3\Files\Event\Listener\OnDisplayFeedListener
arguments:
- '@core.date'
- '@core.router'
- '@core.helpers.string_formatter'
- '@files.model.filesrepository'
tags:
- { name: core.eventListener, event: feeds.events.displayFeed.files, method: onDisplayFeed }
files.event.update_file_cache_on_model_after_save_listener:
class: ACP3\Modules\ACP3\Files\Event\Listener\UpdateFileCacheOnModelAfterSaveListener
arguments:
......
services:
files.feeds.feed_availability:
class: ACP3\Modules\ACP3\Files\Feeds\FeedAvailability
arguments:
- '@core.date'
- '@core.router'
- '@core.helpers.string_formatter'
- '@files.model.filesrepository'
tags:
- { name: feeds.available_module }
......@@ -2,6 +2,7 @@ imports:
- { resource: components/cache.yml }
- { resource: components/controllers.yml }
- { resource: components/events.yml }
- { resource: components/feeds.yml }
- { resource: components/installer.yml }
- { resource: components/models.yml }
- { resource: components/search.yml }
......
<?php
namespace ACP3\Modules\ACP3\News\Event\Listener;
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Modules\ACP3\News\Feeds;
use ACP3\Core\Date;
use ACP3\Core\Helpers\StringFormatter;
use ACP3\Core\Router\RouterInterface;
use ACP3\Modules\ACP3\Feeds\Event\DisplayFeed;
use ACP3\Modules\ACP3\Feeds\Utility\FeedAvailabilityInterface;
use ACP3\Modules\ACP3\News\Installer\Schema;
use ACP3\Modules\ACP3\News\Model\Repository\NewsRepository;
/**
* Class OnDisplayFeedListener
* @package ACP3\Modules\ACP3\News\Event\Listener
*/
class OnDisplayFeedListener
class FeedAvailability implements FeedAvailabilityInterface
{
/**
* @var \ACP3\Core\Date
......@@ -31,12 +34,11 @@ class OnDisplayFeedListener
protected $newsRepository;
/**
* OnDisplayFeedListener constructor.
*
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Router\RouterInterface $router
* @param \ACP3\Core\Helpers\StringFormatter $formatter
* @param \ACP3\Modules\ACP3\News\Model\Repository\NewsRepository $newsRepository
* FeedAvailability constructor.
* @param Date $date
* @param RouterInterface $router
* @param StringFormatter $formatter
* @param NewsRepository $newsRepository
*/
public function __construct(
Date $date,
......@@ -51,9 +53,17 @@ class OnDisplayFeedListener
}
/**
* @param \ACP3\Modules\ACP3\Feeds\Event\DisplayFeed $displayFeed
* @return string
*/
public function getModuleName()
{
return Schema::MODULE_NAME;
}
/**
* @return array
*/
public function onDisplayFeed(DisplayFeed $displayFeed)
public function fetchFeedItems()
{
$items = [];
$results = $this->newsRepository->getAll($this->date->getCurrentDateTime(), 10);
......@@ -68,6 +78,6 @@ class OnDisplayFeedListener
];
}
$displayFeed->getFeedGenerator()->assign($items);
return $items;
}
}
......@@ -10,16 +10,6 @@ services:
tags:
- { name: core.eventListener, event: news.model.news.after_delete, method: execute }
news.event.on_display_feed_listener:
class: ACP3\Modules\ACP3\News\Event\Listener\OnDisplayFeedListener
arguments:
- '@core.date'
- '@core.router'
- '@core.helpers.string_formatter'
- '@news.model.newsrepository'
tags:
- { name: core.eventListener, event: feeds.events.displayFeed.news, method: onDisplayFeed }
news.event.update_news_cache_on_model_after_save_listener:
class: ACP3\Modules\ACP3\News\Event\Listener\UpdateNewsCacheOnModelAfterSaveListener
arguments:
......
services:
news.feeds.feed_availability:
class: ACP3\Modules\ACP3\News\Feeds\FeedAvailability
arguments:
- '@core.date'
- '@core.router'
- '@core.helpers.string_formatter'
- '@news.model.newsrepository'
tags:
- { name: feeds.available_module }
......@@ -2,6 +2,7 @@ imports:
- { resource: components/cache.yml }
- { resource: components/controllers.yml }
- { resource: components/events.yml }
- { resource: components/feeds.yml }
- { resource: components/installer.yml }
- { resource: components/models.yml }
- { resource: components/search.yml }
......
......@@ -4,7 +4,6 @@ namespace ACP3\Modules\ACP3\Search;
use ACP3\Core;
use ACP3\Modules\ACP3\Search\Utility\AvailableModulesRegistrar;
use Symfony\Component\EventDispatcher\EventDispatcher;
/**
* Class Helpers
......@@ -24,10 +23,6 @@ class Helpers
* @var \ACP3\Core\Helpers\Forms
*/
protected $formsHelper;
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcher
*/
protected $eventDispatcher;
/**
* @var AvailableModulesRegistrar
*/
......@@ -38,19 +33,16 @@ class Helpers
* @param \ACP3\Core\Modules $modules
* @param \ACP3\Core\Helpers\Forms $formsHelper
* @param AvailableModulesRegistrar $availableModulesRegistrar
* @param \Symfony\Component\EventDispatcher\EventDispatcher $eventDispatcher
*/
public function __construct(
Core\ACL $acl,
Core\Modules $modules,
Core\Helpers\Forms $formsHelper,
AvailableModulesRegistrar $availableModulesRegistrar,
EventDispatcher $eventDispatcher
AvailableModulesRegistrar $availableModulesRegistrar
) {
$this->acl = $acl;
$this->modules = $modules;
$this->formsHelper = $formsHelper;
$this->eventDispatcher = $eventDispatcher;
$this->availableModulesRegistrar = $availableModulesRegistrar;
}
......
......@@ -11,7 +11,6 @@ services:
- '@core.modules'
- '@core.helpers.forms'
- '@search.utility.available_modules_registrar'
- '@core.event_dispatcher'
search.utility.available_modules_registrar:
class: ACP3\Modules\ACP3\Search\Utility\AvailableModulesRegistrar
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment