From 964383cc7a74078dab9e80aeed2c226992087c1c Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Wed, 20 Mar 2013 23:33:51 +0100 Subject: welcome in the age of newness --- appinfo/app.php | 5 +- appinfo/bootstrap.php | 139 --------------------- appinfo/routes.php | 201 ++++-------------------------- controller/foldercontroller.php | 16 ++- dependencyinjection/dicontainer.php | 70 +++++++++++ tests/controller/FolderControllerTest.php | 44 +++---- 6 files changed, 129 insertions(+), 346 deletions(-) delete mode 100644 appinfo/bootstrap.php create mode 100644 dependencyinjection/dicontainer.php diff --git a/appinfo/app.php b/appinfo/app.php index be667547a..96f56f1fc 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -12,7 +12,6 @@ namespace OCA\News; -require_once \OC_App::getAppPath('news') . '/appinfo/bootstrap.php'; \OCP\App::addNavigationEntry( array( @@ -22,10 +21,10 @@ require_once \OC_App::getAppPath('news') . '/appinfo/bootstrap.php'; 'icon' => \OC_Helper::imagePath( 'news', 'news.svg' ), 'name' => \OC_L10N::get('news')->t('News') )); - +/* \OC_Search::registerProvider('OC_Search_Provider_News'); \OCP\Backgroundjob::addRegularTask( 'OCA\News\Backgroundjob', 'run' ); \OCP\Share::registerBackend('news_item', 'OCA\News\Share_Backend_News_Item'); - +*/ diff --git a/appinfo/bootstrap.php b/appinfo/bootstrap.php deleted file mode 100644 index ae011cc45..000000000 --- a/appinfo/bootstrap.php +++ /dev/null @@ -1,139 +0,0 @@ -share(function($c){ - return new API($c['AppName']); - }); - - - $newsContainer['Request'] = $newsContainer->share(function($c){ - return new Request($_GET, $_POST, $_FILES); - }); - - - $newsContainer['Security'] = $newsContainer->share(function($c) { - return new Security($c['AppName']); - }); - - - /** - * MAPPERS - */ - $newsContainer['ItemMapper'] = $newsContainer->share(function($c){ - return new ItemMapper($c['API']->getUserId()); - }); - - $newsContainer['FeedMapper'] = $newsContainer->share(function($c){ - return new FeedMapper($c['API']->getUserId()); - }); - - $newsContainer['FolderMapper'] = $newsContainer->share(function($c){ - return new FolderMapper($c['API']->getUserId()); - }); - - - /** - * CONTROLLERS - */ - $newsContainer['NewsController'] = function($c){ - return new NewsController($c['Request'], $c['API'], $c['FeedMapper'], - $c['FolderMapper']); - }; - - $newsContainer['NewsAjaxController'] = function($c){ - return new NewsAjaxController($c['Request'], $c['API'], $c['FeedMapper'], - $c['FolderMapper'], $c['ItemMapper']); - }; - - /** - * BUSINESS LAYER OBJECTS - */ - $newsContainer['FolderBl'] = $newsContainer->share(function($c){ - return new FolderBl($c['FolderMapper']); - }); - - $newsContainer['FeedBl'] = $newsContainer->share(function($c){ - return new FeedBl($c['FeedMapper']); - }); - - /** - * EXTERNAL API LAYER - */ - $newsContainer['FolderApi'] = $newsContainer->share(function($c){ - return new FolderApi($c['FolderBl']); - }); - - $newsContainer['FeedApi'] = $newsContainer->share(function($c){ - return new FeedApi($c['FeedBl']); - }); - - - return $newsContainer; -} \ No newline at end of file diff --git a/appinfo/routes.php b/appinfo/routes.php index 767e59f09..ab9e173fc 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -1,206 +1,53 @@ +* @copyright 2012 Alessandro Cosentino cosenal@gmail.com +* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU AFFERO GENERAL PUBLIC LICENSE for more details. * -* This file is licensed under the Affero General Public License version 3 or later. -* See the COPYING-README file +* You should have received a copy of the GNU Affero General Public +* License along with this library. If not, see . * */ namespace OCA\News; -require_once \OC_App::getAppPath('news') . '/appinfo/bootstrap.php'; - -/** - * Shortcut for calling a controller method and printing the result - * @param string $controllerName: the name of the controller under which it is - * stored in the DI container - * @param string $methodName: the method that you want to call - * @param array $urlParams: an array with variables extracted from the routes - * @param bool $disableAdminCheck: disables the check for adminuser rights - * @param bool $isAjax: if the request is an ajax request - */ -function callController($controllerName, $methodName, $urlParams, $disableAdminCheck=true, - $isAjax=false){ - $container = createDIContainer(); - - // run security checks - $security = $container['Security']; - runSecurityChecks($security, $isAjax, $disableAdminCheck); - - // call the controller and render the page - $controller = $container[$controllerName]; - $response = $controller->$methodName($urlParams); - echo $response->render(); -} - +use \OCA\AppFramework\App; -/** - * Shortcut for calling an ajax controller method and printing the result - * @param string $controllerName: the name of the controller under which it is - * stored in the DI container - * @param string $methodName: the method that you want to call - * @param array $urlParams: an array with variables extracted from the routes - * @param bool $disableAdminCheck: disables the check for adminuser rights - */ -function callAjaxController($controllerName, $methodName, $urlParams, $disableAdminCheck=true){ - callController($controllerName, $methodName, $urlParams, $disableAdminCheck, true); -} +use \OCA\News\DependencyInjection\DIContainer; /** - * Runs the security checks and exits on error - * @param Security $security: the security object - * @param bool $isAjax: if true, the ajax checks will be run, otherwise the normal - * checks - * @param bool $disableAdminCheck: disables the check for adminuser rights - */ -function runSecurityChecks($security, $isAjax=false, $disableAdminCheck=true){ - if($disableAdminCheck){ - $security->setIsAdminCheck(false); - } - - if($isAjax){ - $security->runAJAXChecks(); - } else { - $security->runChecks(); - } -} - - -/************************* - * Define your routes here + * Webinterface */ - -/** - * Normal Routes - */ -$this->create('news_index', '/')->action( +$this->create('news_index', '/')->get()->action( function($params){ - callController('NewsController', 'index', $params, true); + //App::main('FolderController', 'getAll', $params, new DIContainer()); } ); -$this->create('news_index_feed', '/feed/{feedid}')->action( - function($params){ - callController('NewsController', 'index', $params, true); - } -); -$this->create('news_export_opml', '/export/opml')->action( +$this->create('news_folders', '/folders')->get()->action( function($params){ - callController('NewsController', 'exportOPML', $params, true); + App::main('FolderController', 'getAll', $params, new DIContainer()); } ); -/** - * AJAX Routes - */ -$this->create('news_ajax_init', '/ajax/init')->action( - function($params){ - callAjaxController('NewsAjaxController', 'init', $params); - } -); - -$this->create('news_ajax_setshowall', '/ajax/setshowall')->action( - function($params){ - callAjaxController('NewsAjaxController', 'setShowAll', $params); - } -); - - -/** - * Folders - */ -$this->create('news_ajax_collapsefolder', '/ajax/collapsefolder')->action( - function($params){ - callAjaxController('NewsAjaxController', 'collapseFolder', $params); - } -); - -$this->create('news_ajax_changefoldername', '/ajax/changefoldername')->action( - function($params){ - callAjaxController('NewsAjaxController', 'changeFolderName', $params); - } -); - -$this->create('news_ajax_createfolder', '/ajax/createfolder')->action( - function($params){ - callAjaxController('NewsAjaxController', 'createFolder', $params); - } -); - -$this->create('news_ajax_deletefolder', '/ajax/deletefolder')->action( - function($params){ - callAjaxController('NewsAjaxController', 'deleteFolder', $params); - } -); - - -/** - * Feeds - */ -$this->create('news_ajax_loadfeed', '/ajax/loadfeed')->action( - function($params){ - callAjaxController('NewsAjaxController', 'loadFeed', $params); - } -); - -$this->create('news_ajax_deletefeed', '/ajax/deletefeed')->action( - function($params){ - callAjaxController('NewsAjaxController', 'deleteFeed', $params); - } -); - -$this->create('news_ajax_movefeedtofolder', '/ajax/movefeedtofolder')->action( - function($params){ - callAjaxController('NewsAjaxController', 'moveFeedToFolder', $params); - } -); - -$this->create('news_ajax_updatefeed', '/ajax/updatefeed')->action( - function($params){ - callAjaxController('NewsAjaxController', 'updateFeed', $params); - } -); - -$this->create('news_ajax_createfeed', '/ajax/createfeed')->action( - function($params){ - callAjaxController('NewsAjaxController', 'createFeed', $params); - } -); - - -/** - * Items - */ -$this->create('news_ajax_setitemstatus', '/ajax/setitemstatus')->action( - function($params){ - callAjaxController('NewsAjaxController', 'setItemStatus', $params); - } -); - -$this->create('news_ajax_setallitemsread', '/ajax/setallitemsread')->action( - function($params){ - callAjaxController('NewsAjaxController', 'setAllItemsRead', $params); - } -); - - -/** - * Import stuff - */ -$this->create('news_ajax_importOPML', '/import')->action( - function($params){ - callAjaxController('NewsAjaxController', 'uploadOPML', $params); - } -); - /** * External API diff --git a/controller/foldercontroller.php b/controller/foldercontroller.php index 0f3ec83a2..1bf7da664 100644 --- a/controller/foldercontroller.php +++ b/controller/foldercontroller.php @@ -28,16 +28,17 @@ namespace OCA\News\Controller; use \OCA\AppFramework\Controller\Controller; use \OCA\AppFramework\Core\API; use \OCA\AppFramework\Http\Request; -use \OCA\AppFramework\Db\DoesNotExistException; -use \OCA\AppFramework\Db\MultipleObjectsReturnedException; + +use \OCA\News\Bl\FolderBl; class FolderController extends Controller { + private $folderBl; - public function __construct(API $api, Request $request, $folderMapper){ + public function __construct(API $api, Request $request, FolderBl $folderBl){ parent::__construct($api, $request); - $this->folderMapper = $folderMapper; + $this->folderBl = $folderBl; } @@ -49,8 +50,11 @@ class FolderController extends Controller { * Returns all folders */ public function getAll(){ - $folders = $this->folderMapper->getAll(); - return $this->renderJSON($folders); + $folders = $this->folderBl->findAll($this->api->getUserId()); + $result = array( + 'folders' => $folders + ); + return $this->renderJSON($result); } diff --git a/dependencyinjection/dicontainer.php b/dependencyinjection/dicontainer.php new file mode 100644 index 000000000..874360b9c --- /dev/null +++ b/dependencyinjection/dicontainer.php @@ -0,0 +1,70 @@ +. +* +*/ + +namespace OCA\News\DependencyInjection; + +use OCA\AppFramework\DependencyInjection\DIContainer as BaseContainer; + +use OCA\News\Controller\FolderController; +use OCA\News\Bl\FolderBl; +use OCA\News\Db\FolderMapper; + + +class DIContainer extends BaseContainer { + + + /** + * Define your dependencies in here + */ + public function __construct(){ + // tell parent container about the app name + parent::__construct('news'); + + + /** + * CONTROLLERS + */ + $this['FolderController'] = $this->share(function($c){ + return new FolderController($c['API'], $c['Request'], $c['FolderBl']); + }); + + /** + * Business + */ + $this['FolderBl'] = $this->share(function($c){ + return new FolderBl($c['FolderMapper']); + }); + + /** + * MAPPERS + */ + $this['FolderMapper'] = $this->share(function($c){ + return new FolderMapper($c['API']); + }); + + + } +} + diff --git a/tests/controller/FolderControllerTest.php b/tests/controller/FolderControllerTest.php index 4dfe5e200..52836b092 100644 --- a/tests/controller/FolderControllerTest.php +++ b/tests/controller/FolderControllerTest.php @@ -31,6 +31,8 @@ use \OCA\AppFramework\Utility\ControllerTestUtility; use \OCA\AppFramework\Db\DoesNotExistException; use \OCA\AppFramework\Db\MultipleObjectsReturnedException; +use \OCA\News\Db\Folder; + require_once(__DIR__ . "/../classloader.php"); @@ -38,7 +40,7 @@ require_once(__DIR__ . "/../classloader.php"); class FolderControllerTest extends ControllerTestUtility { private $api; - private $folderMapper; + private $folderBl; private $request; private $controller; @@ -48,20 +50,21 @@ class FolderControllerTest extends ControllerTestUtility { */ public function setUp(){ $this->api = $this->getAPIMock(); - $this->folderMapper = $this->getMock('FolderMapper', - array('getAll', 'setCollapsed')); + $this->folderBl = $this->getMockBuilder('\OCA\News\Bl\FolderBl') + ->disableOriginalConstructor() + ->getMock(); $this->request = new Request(); $this->controller = new FolderController($this->api, $this->request, - $this->folderMapper); - + $this->folderBl); } + /** * getAll */ public function testGetAllCalled(){ - $this->folderMapper->expects($this->once()) - ->method('getAll') + $this->folderBl->expects($this->once()) + ->method('findAll') ->will($this->returnValue( array() )); $this->controller->getAll(); @@ -70,18 +73,21 @@ class FolderControllerTest extends ControllerTestUtility { public function testGetAllReturnsFolders(){ $return = array( - 'folder1' => 'name1', - 'folder2' => 'name2' + new Folder(), + new Folder(), ); - $this->folderMapper->expects($this->once()) - ->method('getAll') + $this->folderBl->expects($this->once()) + ->method('findAll') ->will($this->returnValue($return)); $response = $this->controller->getAll(); - $this->assertEquals($return, $response->getParams()); + $expected = array( + 'folders' => $return + ); + $this->assertEquals($expected, $response->getParams()); } - + public function testGetAllAnnotations(){ $methodName = 'getAll'; $annotations = array('IsAdminExemption', 'IsSubAdminExemption', 'Ajax'); @@ -91,10 +97,6 @@ class FolderControllerTest extends ControllerTestUtility { public function testGetAllReturnsJSON(){ - $this->folderMapper->expects($this->once()) - ->method('getAll') - ->will($this->returnValue( array() )); - $response = $this->controller->getAll(); $this->assertTrue($response instanceof JSONResponse); @@ -106,7 +108,7 @@ class FolderControllerTest extends ControllerTestUtility { *//* public function testCollapseCalled(){ $urlParams = array('folderId' => 1); - $this->folderMapper->expects($this->once()) + $this->folderBl->expects($this->once()) ->method('setCollapsed') ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)); $this->controller->setURLParams($urlParams); @@ -117,7 +119,7 @@ class FolderControllerTest extends ControllerTestUtility { public function testCollapseReturnsNoParams(){ $urlParams = array('folderId' => 1); - $this->folderMapper->expects($this->once()) + $this->folderBl->expects($this->once()) ->method('setCollapsed') ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)); $this->controller->setURLParams($urlParams); @@ -137,7 +139,7 @@ class FolderControllerTest extends ControllerTestUtility { public function testCollapseReturnsJSON(){ $urlParams = array('folderId' => 1); - $this->folderMapper->expects($this->once()) + $this->folderBl->expects($this->once()) ->method('setCollapsed') ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)); $this->controller->setURLParams($urlParams); @@ -150,7 +152,7 @@ class FolderControllerTest extends ControllerTestUtility { private function collapseException($ex){ $urlParams = array('folderId' => 1); - $this->folderMapper->expects($this->once()) + $this->folderBl->expects($this->once()) ->method('setCollapsed') ->with($this->equalTo($urlParams['folderId']), $this->equalTo(true)) ->will($this->throwException($ex)); -- cgit v1.2.3