From 5c7fdb08534018ba86ec4a38f45b63cbbf67a72f Mon Sep 17 00:00:00 2001 From: Daniel Opitz Date: Wed, 24 Oct 2018 00:39:22 +0200 Subject: remove some wrapping functions, define argument and response types, removed configuration of AppConfig as its not existing anymore --- lib/AppInfo/Application.php | 348 ++++++++++++++------------------------------ 1 file changed, 111 insertions(+), 237 deletions(-) (limited to 'lib') diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index fef3152e3..9394e790b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -15,287 +15,161 @@ namespace OCA\News\AppInfo; use HTMLPurifier; use HTMLPurifier_Config; - -use OCP\BackgroundJob\IJobList; -use PicoFeed\Config\Config as PicoFeedConfig; -use PicoFeed\Reader\Reader as PicoFeedReader; - -use OCP\ILogger; -use OCP\INavigationManager; -use OCP\IURLGenerator; -use OCP\IConfig; -use OCP\IDBConnection; -use OCP\AppFramework\App; -use OCP\Files\IRootFolder; - -use OCA\News\Config\AppConfig; use OCA\News\Config\Config; -use OCA\News\Service\FeedService; -use OCA\News\Db\MapperFactory; use OCA\News\Db\ItemMapper; -use OCA\News\Fetcher\Fetcher; +use OCA\News\Db\MapperFactory; use OCA\News\Fetcher\FeedFetcher; +use OCA\News\Fetcher\Fetcher; use OCA\News\Fetcher\YoutubeFetcher; -use OCA\News\Explore\RecommendedSites; use OCA\News\Utility\ProxyConfigParser; +use OCP\AppFramework\App; +use OCP\Files\IRootFolder; +use OCP\Files\Node; +use OCP\IConfig; +use OCP\IContainer; +use OCP\ILogger; +use PicoFeed\Config\Config as PicoFeedConfig; +use PicoFeed\Reader\Reader as PicoFeedReader; class Application extends App { - public function __construct(array $urlParams=[]) + public function __construct(array $urlParams = []) { parent::__construct('news', $urlParams); + $container = $this->getContainer(); + // files - $this->registerFileContents('checksums', 'checksum.json'); - $this->registerFileContents('info', '../../appinfo/info.xml'); + $container->registerService('checksums', function () { + return file_get_contents(__DIR__ . '/checksum.json'); + }); + $container->registerService('info', function () { + return file_get_contents(__DIR__ . '/../../appinfo/info.xml'); + }); // parameters - $this->registerParameter('exploreDir', __DIR__ . '/../Explore/feeds'); - $this->registerParameter('configFile', 'config.ini'); + $container->registerParameter('exploreDir', __DIR__ . '/../Explore/feeds'); + $container->registerParameter('configFile', 'config.ini'); // factories - $this->registerFactory(ItemMapper::class, MapperFactory::class); - - - /** - * App config parser - */ - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - AppConfig::class, function ($c) { - $config = new AppConfig( - $c->query(INavigationManager::class), - $c->query(IURLGenerator::class), - $c->query(IJobList::class) - ); - - $config->loadConfig($c->query('info')); - - return $config; - } - ); + $container->registerService(ItemMapper::class, function (IContainer $c) { + return $c->query(MapperFactory::class)->build(); + }); /** * Core */ - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - 'LoggerParameters', function ($c) { - return ['app' => $c->query('AppName')]; + $container->registerService('LoggerParameters', function (IContainer $c): array { + return ['app' => $c->query('AppName')]; + }); + + $container->registerService('databaseType', function (IContainer $c) { + return $c->query(IConfig::class)->getSystemValue('dbtype'); + }); + + $container->registerService('ConfigView', function (IContainer $c): Node { + /** @var IRootFolder $fs */ + $fs = $c->query(IRootFolder::class); + $path = 'news/config'; + if ($fs->nodeExists($path)) { + return $fs->get($path); + } else { + return $fs->newFolder($path); } - ); + }); - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - 'databaseType', function ($c) { - return $c->query(IConfig::class)->getSystemValue('dbtype'); - } - ); - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - 'ConfigView', function ($c) { - $fs = $c->query(IRootFolder::class); - $path = 'news/config'; - if ($fs->nodeExists($path)) { - return $fs->get($path); - } else { - return $fs->newFolder($path); - } - } - ); + $container->registerService(Config::class, function (IContainer $c): Config { + $config = new Config( + $c->query('ConfigView'), + $c->query(ILogger::class), + $c->query('LoggerParameters') + ); + $config->read($c->query('configFile'), true); + return $config; + }); + $container->registerService(HTMLPurifier::class, function (IContainer $c): HTMLPurifier { + $directory = $c->query(IConfig::class)->getSystemValue('datadirectory') . '/news/cache/purifier'; - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - Config::class, function ($c) { - $config = new Config( - $c->query('ConfigView'), - $c->query(ILogger::class), - $c->query('LoggerParameters') - ); - $config->read($c->query('configFile'), true); - return $config; + if (!is_dir($directory)) { + mkdir($directory, 0770, true); } - ); - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - HTMLPurifier::class, function ($c) { - $directory = $c->query(IConfig::class) - ->getSystemValue('datadirectory') . '/news/cache/purifier'; - - if(!is_dir($directory)) { - mkdir($directory, 0770, true); - } - - $config = HTMLPurifier_Config::createDefault(); - $config->set('HTML.ForbiddenAttributes', 'class'); - $config->set('Cache.SerializerPath', $directory); - $config->set('HTML.SafeIframe', true); - $config->set( - 'URI.SafeIframeRegexp', - '%^https://(?:www\.)?(' . - 'youtube(?:-nocookie)?.com/embed/|' . - 'player.vimeo.com/video/|' . - 'vk.com/video_ext.php)%' - ); //allow YouTube and Vimeo - $def = $config->getHTMLDefinition(true); - $def->addAttribute('iframe', 'allowfullscreen', 'Bool'); - return new HTMLPurifier($config); - } - ); + $config = HTMLPurifier_Config::createDefault(); + $config->set('HTML.ForbiddenAttributes', 'class'); + $config->set('Cache.SerializerPath', $directory); + $config->set('HTML.SafeIframe', true); + $config->set( + 'URI.SafeIframeRegexp', + '%^https://(?:www\.)?(' . + 'youtube(?:-nocookie)?.com/embed/|' . + 'player.vimeo.com/video/|' . + 'vk.com/video_ext.php)%' + ); //allow YouTube and Vimeo + $def = $config->getHTMLDefinition(true); + $def->addAttribute('iframe', 'allowfullscreen', 'Bool'); + return new HTMLPurifier($config); + }); /** * Fetchers */ - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - PicoFeedConfig::class, function ($c) { - // FIXME: move this into a separate class for testing? - $config = $c->query(Config::class); - $proxy = $c->query(ProxyConfigParser::class); - - $userAgent = 'NextCloud-News/1.0'; - - $pico = new PicoFeedConfig(); - $pico->setClientUserAgent($userAgent) - ->setClientTimeout($config->getFeedFetcherTimeout()) - ->setMaxRedirections($config->getMaxRedirects()) - ->setMaxBodySize($config->getMaxSize()) - ->setParserHashAlgo('md5'); - - // proxy settings - $proxySettings = $proxy->parse(); - $host = $proxySettings['host']; - $port = $proxySettings['port']; - $user = $proxySettings['user']; - $password = $proxySettings['password']; - - if ($host) { - $pico->setProxyHostname($host); - - if ($port) { - $pico->setProxyPort($port); - } - } - - if ($user) { - $pico->setProxyUsername($user) - ->setProxyPassword($password); + $container->registerService(PicoFeedConfig::class, function (IContainer $c): PicoFeedConfig { + // FIXME: move this into a separate class for testing? + $config = $c->query(Config::class); + $proxy = $c->query(ProxyConfigParser::class); + + $userAgent = 'NextCloud-News/1.0'; + + $pico = new PicoFeedConfig(); + $pico->setClientUserAgent($userAgent) + ->setClientTimeout($config->getFeedFetcherTimeout()) + ->setMaxRedirections($config->getMaxRedirects()) + ->setMaxBodySize($config->getMaxSize()) + ->setParserHashAlgo('md5'); + + // proxy settings + $proxySettings = $proxy->parse(); + $host = $proxySettings['host']; + $port = $proxySettings['port']; + $user = $proxySettings['user']; + $password = $proxySettings['password']; + + if ($host) { + $pico->setProxyHostname($host); + + if ($port) { + $pico->setProxyPort($port); } - - return $pico; } - ); - $this->registerService( - PicoFeedReader::class, function ($c) { - return new PicoFeedReader($c->query(PicoFeedConfig::class)); + if ($user) { + $pico->setProxyUsername($user) + ->setProxyPassword($password); } - ); - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - Fetcher::class, function ($c) { - $fetcher = new Fetcher(); - - // register fetchers in order, the most generic fetcher should be - // the last one - $fetcher->registerFetcher($c->query(YoutubeFetcher::class)); - $fetcher->registerFetcher($c->query(FeedFetcher::class)); - - return $fetcher; - } - ); + return $pico; + }); + $container->registerService(PicoFeedReader::class, function (IContainer $c): PicoFeedReader { + return new PicoFeedReader($c->query(PicoFeedConfig::class)); + }); - } + $container->registerService(Fetcher::class, function (IContainer $c): Fetcher { + $fetcher = new Fetcher(); - /** - * Registers the content of a file under a key - * - * @param string $key - * @param string $file path relative to this file, __DIR__ will be prepended - */ - private function registerFileContents($key, $file) - { - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - $key, function () use ($file) { - return file_get_contents(__DIR__ . '/' . $file); - } - ); - } - - /** - * Shortcut for registering a service - * - * @param string $key - * @param closure $factory - * @param boolean $shared - */ - private function registerService($key, $factory, $shared=true) - { - $this->getContainer()->registerService($key, $factory, $shared); - } + // register fetchers in order, the most generic fetcher should be + // the last one + $fetcher->registerFetcher($c->query(YoutubeFetcher::class)); + $fetcher->registerFetcher($c->query(FeedFetcher::class)); - /** - * Shortcut for registering a parameter - * - * @param string $key - * @param mixed $value - */ - private function registerParameter($key, $value) - { - $this->getContainer()->registerParameter($key, $value); - } + return $fetcher; + }); - /** - * Register a class containing the app construction logic instead of the - * inlining everything in this class to enhance testability - * - * @param string $key fully qualified class name - * @param string $factory fully qualified factory class name - */ - private function registerFactory($key, $factory) - { - /** - * @noinspection PhpParamsInspection -*/ - $this->registerService( - $key, function ($c) use ($factory) { - return $c->query($factory)->build(); - } - ); - } - /** - * Register the additional config parameters found in the info.xml - */ - public function registerConfig() - { - $this->getContainer()->query(AppConfig::class)->registerAll(); } } -- cgit v1.2.3