diff options
28 files changed, 433 insertions, 751 deletions
diff --git a/dependencyinjection/dicontainer.php b/app/news.php index ef8ffd0f6..8b886f61d 100644 --- a/dependencyinjection/dicontainer.php +++ b/app/news.php @@ -23,12 +23,11 @@ * */ -namespace OCA\News\DependencyInjection; +namespace OCA\News\App; use \OC\Files\View; +use \OCP\AppFramework\App; -use \OCA\AppFramework\DependencyInjection\DIContainer as BaseContainer; -use \OCA\AppFramework\Middleware\MiddlewareDispatcher; use \OCA\News\Core\API; @@ -37,7 +36,10 @@ use \OCA\News\Controller\FolderController; use \OCA\News\Controller\FeedController; use \OCA\News\Controller\ItemController; use \OCA\News\Controller\ExportController; -use \OCA\News\Controller\UserSettingsController; +use \OCA\News\Controller\ApiController; +use \OCA\News\Controller\FolderApiController; +use \OCA\News\Controller\FeedApiController; +use \OCA\News\Controller\ItemApiController; use \OCA\News\BusinessLayer\FolderBusinessLayer; use \OCA\News\BusinessLayer\FeedBusinessLayer; @@ -49,11 +51,6 @@ use \OCA\News\Db\ItemMapper; use \OCA\News\Db\StatusFlag; use \OCA\News\Db\MapperFactory; -use \OCA\News\API\NewsAPI; -use \OCA\News\API\FolderAPI; -use \OCA\News\API\FeedAPI; -use \OCA\News\API\ItemAPI; - use \OCA\News\Utility\Config; use \OCA\News\Utility\OPMLExporter; use \OCA\News\Utility\Updater; @@ -70,6 +67,7 @@ use \OCA\News\ArticleEnhancer\RegexArticleEnhancer; use \OCA\News\Middleware\CORSMiddleware; + require_once __DIR__ . '/../3rdparty/htmlpurifier/library/HTMLPurifier.auto.php'; // to prevent clashes with installed app framework versions @@ -77,199 +75,217 @@ if(!class_exists('\SimplePie')) { require_once __DIR__ . '/../3rdparty/simplepie/autoloader.php'; } -class DIContainer extends BaseContainer { +class News extends App { - /** - * Define your dependencies in here - */ - public function __construct(){ - // tell parent container about the app name - parent::__construct('news'); + public function __construct(array $urlParams=array()){ + parent::__construct('news', $urlParams); + + $container = $this->getContainer(); - $this['API'] = $this->share(function($c){ - return new API($c['news']); - }); /** - * Configuration values + * Controllers */ - $this['configView'] = $this->share(function($c) { - $view = new View('/news/config'); - if (!$view->file_exists('')) { - $view->mkdir(''); - } - - return $view; + $container->registerService('PageController', function($c) { + return new PageController( + $c['API'], + $c['Request'] + ); }); - $this['Config'] = $this->share(function($c) { - $config = new Config($c['configView'], $c['API']); - $config->read('config.ini', true); - return $config; + $container->registerService('FolderController', function($c) { + return new FolderController( + $c['API'], + $c['Request'], + $c['FolderBusinessLayer'], + $c['FeedBusinessLayer'], + $c['ItemBusinessLayer'] + ); }); - // set by config class from config file - // look up defaults in utility/config.php - $this['autoPurgeMinimumInterval'] = $this['Config']->getAutoPurgeMinimumInterval(); - $this['autoPurgeCount'] = $this['Config']->getAutoPurgeCount(); - $this['simplePieCacheDuration'] = $this['Config']->getSimplePieCacheDuration(); - $this['feedFetcherTimeout'] = $this['Config']->getFeedFetcherTimeout(); - $this['useCronUpdates'] = $this['Config']->getUseCronUpdates(); - - - $this['simplePieCacheDirectory'] = $this->share(function($c) { - $directory = $c['API']->getSystemValue('datadirectory') . - '/news/cache/simplepie'; - - if(!is_dir($directory)) { - mkdir($directory, 0770, true); - } - return $directory; + $container->registerService('FeedController', function($c) { + return new FeedController( + $c['API'], + $c['Request'], + $c['FolderBusinessLayer'], + $c['FeedBusinessLayer'], + $c['ItemBusinessLayer'] + ); }); - $this['HTMLPurifier'] = $this->share(function($c) { - $directory = $c['API']->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/)%'); //allow YouTube and Vimeo - return new \HTMLPurifier($config); + $container->registerService('ItemController', function($c) { + return new ItemController( + $c['API'], + $c['Request'], + $c['FeedBusinessLayer'], + $c['ItemBusinessLayer'] + ); }); - - /** - * CONTROLLERS - */ - $this['PageController'] = $this->share(function($c){ - return new PageController($c['API'], $c['Request']); + $container->registerService('ExportController', function($c) { + return new ExportController( + $c['API'], + $c['Request'], + $c['FeedBusinessLayer'], + $c['FolderBusinessLayer'], + $c['ItemBusinessLayer'], + $c['OPMLExporter']); }); - $this['FolderController'] = $this->share(function($c){ - return new FolderController($c['API'], $c['Request'], - $c['FolderBusinessLayer'], - $c['FeedBusinessLayer'], - $c['ItemBusinessLayer']); + $container->registerService('ApiController', function($c) { + return new ApiController( + $c['API'], + $c['Request'], + $c['Updater'] + ); }); - $this['FeedController'] = $this->share(function($c){ - return new FeedController($c['API'], $c['Request'], + $container->registerService('FolderApiController', function($c) { + return new FolderApiController( + $c['API'], + $c['Request'], $c['FolderBusinessLayer'], - $c['FeedBusinessLayer'], - $c['ItemBusinessLayer']); + $c['ItemBusinessLayer'] + ); }); - $this['ItemController'] = $this->share(function($c){ - return new ItemController($c['API'], $c['Request'], + $container->registerService('FeedApiController', function($c) { + return new FeedApiController( + $c['API'], + $c['Request'], + $c['FolderBusinessLayer'], $c['FeedBusinessLayer'], - $c['ItemBusinessLayer']); - }); - - $this['ExportController'] = $this->share(function($c){ - return new ExportController($c['API'], $c['Request'], - $c['FeedBusinessLayer'], - $c['FolderBusinessLayer'], - $c['ItemBusinessLayer'], - $c['OPMLExporter']); + $c['ItemBusinessLayer'] + ); }); - $this['UserSettingsController'] = $this->share(function($c){ - return new UserSettingsController($c['API'], $c['Request'], - $c['ItemBusinessLayer']); + $container->registerService('ItemApiController', function($c) { + return new ItemApiController( + $c['API'], + $c['Request'], + $c['ItemBusinessLayer'] + ); }); /** * Business Layer */ - $this['FolderBusinessLayer'] = $this->share(function($c){ + $container->registerService('FolderBusinessLayer', function($c) { return new FolderBusinessLayer( $c['FolderMapper'], $c['API'], $c['TimeFactory'], - $c['autoPurgeMinimumInterval']); + $c['Config'] + ); }); - $this['FeedBusinessLayer'] = $this->share(function($c){ + $container->registerService('FeedBusinessLayer', function($c) { return new FeedBusinessLayer( $c['FeedMapper'], $c['Fetcher'], $c['ItemMapper'], $c['API'], $c['TimeFactory'], - $c['autoPurgeMinimumInterval'], + $c['Config'] $c['Enhancer'], - $c['HTMLPurifier']); + $c['HTMLPurifier'] + ); }); - $this['ItemBusinessLayer'] = $this->share(function($c){ + $container->registerService('ItemBusinessLayer', function($c) { return new ItemBusinessLayer( $c['ItemMapper'], $c['StatusFlag'], $c['TimeFactory'], - $c['autoPurgeCount']); + $c['Config'] + ); }); /** - * MAPPERS + * Mappers */ - $this['MapperFactory'] = $this->share(function($c){ - return new MapperFactory($c['API']); + $container->registerService('MapperFactory', function($c) { + return new MapperFactory( + $c['API'] + ); }); - $this['FolderMapper'] = $this->share(function($c){ - return new FolderMapper($c['API']); + $container->registerService('FolderMapper', function($c) { + return new FolderMapper( + $c['API'] + ); }); - $this['FeedMapper'] = $this->share(function($c){ - return new FeedMapper($c['API']); + $container->registerService('FeedMapper', function($c) { + return new FeedMapper( + $c['API'] + ); }); - $this['ItemMapper'] = $this->share(function($c){ - return $c['MapperFactory']->getItemMapper($c['API']); + $container->registerService('ItemMapper', function($c) { + return $c['MapperFactory']->getItemMapper( + $c['API'] + ); }); - + /** - * External API + * Utility */ - $this['NewsAPI'] = $this->share(function($c){ - return new NewsAPI($c['API'], $c['Request'], $c['Updater']); + $container->registerService('API', function($c){ + return new API( + $c['news'] + ); }); - $this['FolderAPI'] = $this->share(function($c){ - return new FolderAPI($c['API'], $c['Request'], - $c['FolderBusinessLayer'], - $c['ItemBusinessLayer']); + $container->registerService('ConfigView', function($c) { + $view = new View('/news/config'); + if (!$view->file_exists('')) { + $view->mkdir(''); + } + + return $view; }); - $this['FeedAPI'] = $this->share(function($c){ - return new FeedAPI($c['API'], $c['Request'], - $c['FolderBusinessLayer'], - $c['FeedBusinessLayer'], - $c['ItemBusinessLayer']); + $container->registerService('Config', function($c) { + $config = new Config($c['ConfigView'], $c['API']); + $config->read('config.ini', true); + return $config; }); - $this['ItemAPI'] = $this->share(function($c){ - return new ItemAPI($c['API'], $c['Request'], - $c['ItemBusinessLayer']); + $container->registerService('simplePieCacheDirectory', function($c) { + $directory = $c['API']->getSystemValue('datadirectory') . + '/news/cache/simplepie'; + + if(!is_dir($directory)) { + mkdir($directory, 0770, true); + } + return $directory; }); - /** - * Utility - */ - $this['Enhancer'] = $this->share(function($c){ + $container->registerService('HTMLPurifier', function($c) { + $directory = $c['API']->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/)%'); //allow YouTube and Vimeo + return new \HTMLPurifier($config); + }); + + $container->registerService('Enhancer', function($c) { $enhancer = new Enhancer(); // register simple enhancers from config json file @@ -281,7 +297,7 @@ class DIContainer extends BaseContainer { $articleEnhancer = new XPathArticleEnhancer( $c['SimplePieAPIFactory'], $config, - $c['feedFetcherTimeout'] + $c['Config'] ); $enhancer->registerEnhancer($feed, $articleEnhancer); } @@ -299,7 +315,10 @@ class DIContainer extends BaseContainer { return $enhancer; }); - $this['Fetcher'] = $this->share(function($c){ + /** + * Fetchers + */ + $container->registerService('Fetcher', function($c) { $fetcher = new Fetcher(); // register fetchers in order @@ -309,59 +328,54 @@ class DIContainer extends BaseContainer { return $fetcher; }); - $this['FeedFetcher'] = $this->share(function($c){ + $container->registerService('FeedFetcher', function($c) { return new FeedFetcher( $c['API'], $c['SimplePieAPIFactory'], $c['FaviconFetcher'], $c['TimeFactory'], $c['simplePieCacheDirectory'], - $c['simplePieCacheDuration'], - $c['feedFetcherTimeout']); + $c['Config'] + ); }); - $this['StatusFlag'] = $this->share(function($c){ + $container->registerService('StatusFlag', function($c) { return new StatusFlag(); }); - $this['OPMLExporter'] = $this->share(function($c){ + $container->registerService('OPMLExporter', function($c) { return new OPMLExporter(); }); - $this['Updater'] = $this->share(function($c){ - return new Updater($c['FolderBusinessLayer'], - $c['FeedBusinessLayer'], - $c['ItemBusinessLayer']); + $container->registerService('Updater', function($c) { + return new Updater( + $c['FolderBusinessLayer'], + $c['FeedBusinessLayer'], + $c['ItemBusinessLayer'] + ); }); - $this['SimplePieAPIFactory'] = $this->share(function($c){ + $container->registerService('SimplePieAPIFactory', function($c) { return new SimplePieAPIFactory(); }); - $this['FaviconFetcher'] = $this->share(function($c){ - return new FaviconFetcher($c['SimplePieAPIFactory']); - }); - - $this['TimeFactory'] = $this->share(function($c){ - return new TimeFactory(); + $container->registerService('FaviconFetcher', function($c) { + return new FaviconFetcher( + $c['SimplePieAPIFactory'] + ); }); - /** * Middleware */ - $this['MiddlewareDispatcher'] = $this->share(function($c){ - $dispatcher = new MiddlewareDispatcher(); - $dispatcher->registerMiddleware($c['HttpMiddleware']); - $dispatcher->registerMiddleware($c['SecurityMiddleware']); - $dispatcher->registerMiddleware($c['CORSMiddleware']); - return $dispatcher; - }); - - $this['CORSMiddleware'] = $this->share(function($c){ - return new CORSMiddleware($c['Request']); + $container->registerService('CORSMiddleware', function($c) { + return new CORSMiddleware( + $c['Request'] + ); }); + $container->registerMiddleWare($container['CORSMiddleware']); + } } diff --git a/appinfo/routes.php b/appinfo/routes.php index 1eb0af953..d5d40c1a4 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -27,410 +27,95 @@ namespace OCA\News; use \OCP\AppFramework\App; -use \OCA\News\DependencyInjection\DIContainer; - -/** - * Webinterface - */ -$this->create('news_index', '/')->get()->action( - function($params){ - App::main('PageController', 'index', $params, new DIContainer()); - } -); - -/** - * Folders - */ -$this->create('news_folders', '/folders')->get()->action( - function($params){ - App::main('FolderController', 'folders', $params, new DIContainer()); - } -); - -$this->create('news_folders_open', '/folders/{folderId}/open')->post()->action( - function($params){ - App::main('FolderController', 'open', $params, new DIContainer()); - } -); - -$this->create('news_folders_collapse', '/folders/{folderId}/collapse')->post()->action( - function($params){ - App::main('FolderController', 'collapse', $params, new DIContainer()); - } -); - -$this->create('news_folders_create', '/folders')->post()->action( - function($params){ - App::main('FolderController', 'create', $params, new DIContainer()); - } -); - -$this->create('news_folders_delete', '/folders/{folderId}')->delete()->action( - function($params){ - App::main('FolderController', 'delete', $params, new DIContainer()); - } -); - -$this->create('news_folders_restore', '/folders/{folderId}/restore')->post()->action( - function($params){ - App::main('FolderController', 'restore', $params, new DIContainer()); - } -); - -$this->create('news_folders_rename', '/folders/{folderId}/rename')->post()->action( - function($params){ - App::main('FolderController', 'rename', $params, new DIContainer()); - } -); - -$this->create('news_folders_read', '/folders/{folderId}/read')->post()->action( - function($params){ - App::main('FolderController', 'read', $params, new DIContainer()); - } -); - -/** - * Feeds - */ -$this->create('news_feeds', '/feeds')->get()->action( - function($params){ - App::main('FeedController', 'feeds', $params, new DIContainer()); - } -); - -$this->create('news_feeds_active', '/feeds/active')->get()->action( - function($params){ - App::main('FeedController', 'active', $params, new DIContainer()); - } -); - -$this->create('news_feeds_create', '/feeds')->post()->action( - function($params){ - App::main('FeedController', 'create', $params, new DIContainer()); - } -); - -$this->create('news_feeds_delete', '/feeds/{feedId}')->delete()->action( - function($params){ - App::main('FeedController', 'delete', $params, new DIContainer()); - } -); - -$this->create('news_feeds_restore', '/feeds/{feedId}/restore')->post()->action( - function($params){ - App::main('FeedController', 'restore', $params, new DIContainer()); - } -); - -$this->create('news_feeds_update', '/feeds/{feedId}/update')->post()->action( - function($params){ - App::main('FeedController', 'update', $params, new DIContainer()); - } -); - -$this->create('news_feeds_move', '/feeds/{feedId}/move')->post()->action( - function($params){ - App::main('FeedController', 'move', $params, new DIContainer()); - } -); - -$this->create('news_feeds_rename', '/feeds/{feedId}/rename')->post()->action( - function($params){ - App::main('FeedController', 'rename', $params, new DIContainer()); - } -); - -$this->create('news_feeds_read', '/feeds/{feedId}/read')->post()->action( - function($params){ - App::main('FeedController', 'read', $params, new DIContainer()); - } -); - -$this->create('news_feeds_import_articles', '/feeds/import/articles') -->post()->action( - function($params){ - App::main('FeedController', 'importArticles', $params, - new DIContainer()); - } -); - -/** - * Items - */ -$this->create('news_items', '/items')->get()->action( - function($params){ - App::main('ItemController', 'items', $params, new DIContainer()); - } -); - -$this->create('news_items_new', '/items/new')->get()->action( - function($params){ - App::main('ItemController', 'newItems', $params, new DIContainer()); - } -); - -$this->create('news_items_read', '/items/{itemId}/read')->post()->action( - function($params){ - App::main('ItemController', 'read', $params, new DIContainer()); - } -); - -$this->create('news_items_unread', '/items/{itemId}/unread')->post()->action( - function($params){ - App::main('ItemController', 'unread', $params, new DIContainer()); - } -); - -$this->create('news_items_star', '/items/{feedId}/{guidHash}/star')->post()->action( - function($params){ - App::main('ItemController', 'star', $params, new DIContainer()); - } -); - -$this->create('news_items_unstar', '/items/{feedId}/{guidHash}/unstar')->post()->action( - function($params){ - App::main('ItemController', 'unstar', $params, new DIContainer()); - } -); - -$this->create('news_items_all_read', '/items/read')->post()->action( - function($params){ - App::main('ItemController', 'readAll', $params, new DIContainer()); - } -); - -/** - * Export - */ -$this->create('news_export_opml', '/export/opml')->get()->action( - function($params){ - App::main('ExportController', 'opml', $params, new DIContainer()); - } -); - -$this->create('news_export_articles', '/export/articles')->get()->action( - function($params){ - App::main('ExportController', 'articles', $params, new DIContainer()); - } -); - -/** - * User Settings - */ -$this->create('news_usersettings_iscompact', '/usersettings/compact')->get()->action( - function($params){ - App::main('UserSettingsController', 'isCompactView', $params, new DIContainer()); - } -); - -$this->create('news_usersettings_setcompact', '/usersettings/compact')->post()->action( - function($params){ - App::main('UserSettingsController', 'setCompactView', $params, new DIContainer()); - } -); - -$this->create('news_usersettings_read', '/usersettings/read')->get()->action( - function($params){ - App::main('UserSettingsController', 'read', $params, new DIContainer()); - } -); - -$this->create('news_usersettings_read_show', '/usersettings/read/show')->post()->action( - function($params){ - App::main('UserSettingsController', 'show', $params, new DIContainer()); - } -); - -$this->create('news_usersettings_read_hide', '/usersettings/read/hide')->post()->action( - function($params){ - App::main('UserSettingsController', 'hide', $params, new DIContainer()); - } -); - -$this->create('news_usersettings_language', '/usersettings/language')->get()->action( - function($params){ - App::main('UserSettingsController', 'getLanguage', $params, new DIContainer()); - } -); - - -/** - * Generic API - */ -$this->create('news_api_version', '/api/v1-2/version')->get()->action( - function($params) { - return App::main('NewsAPI', 'version', $params, new DIContainer()); - } -); - -$this->create('news_api_before_update', '/api/v1-2/cleanup/before-update')->get()->action( - function($params) { - return App::main('NewsAPI', 'beforeUpdate', $params, new DIContainer()); - } -); - -$this->create('news_api_after_update', '/api/v1-2/cleanup/after-update')->get()->action( - function($params) { - return App::main('NewsAPI', 'afterUpdate', $params, new DIContainer()); - } -); - -/** - * Folder API - */ -$this->create('news_api_folders_get_all', '/api/v1-2/folders')->get()->action( - function($params) { - return App::main('FolderAPI', 'getAll', $params, new DIContainer()); - } -); - -$this->create('news_api_folders_create', '/api/v1-2/folders')->post()->action( - function($params) { - return App::main('FolderAPI', 'create', $params, new DIContainer()); - } -); - -$this->create('news_api_folders_delete', '/api/v1-2/folders/{folderId}')->delete()->action( - function($params) { - return App::main('FolderAPI', 'delete', $params, new DIContainer()); - } -); - -$this->create('news_api_folders_update', '/api/v1-2/folders/{folderId}')->put()->action( - function($params) { - return App::main('FolderAPI', 'update', $params, new DIContainer()); - } -); - -$this->create('news_api_folders_read', '/api/v1-2/folders/{folderId}/read')->put()->action( - function($params) { - return App::main('FolderAPI', 'read', $params, new DIContainer()); - } -); - -/** - * Feed API - */ +use \OCA\News\App\News; + +$app = new News(); +$app->registerRoutes(array( + 'routes' => array( + ///////////////// Website + // page + array('name' => 'page#index', 'url' => '/', 'verb' => 'GET'), + array('name' => 'page#settings', 'url' => '/settings', 'verb' => 'GET'), |