From 2a7ca79b2da82183436bd56d42414225c4bdc8d6 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 19 Nov 2014 22:00:50 +0100 Subject: [PATCH 01/22] Added Route layer proof of concept --- src/Dispatcher.php | 3 ++ src/RouteRepository.php | 29 ++++++++++++++ src/Routes/AbstractRoute.php | 11 +++++ .../AuthController.php => Routes/Login.php} | 16 +++++--- src/di.php | 8 +++- tests/DispatcherTest.php | 7 ++++ tests/RouteRepositoryTest.php | 40 +++++++++++++++++++ 7 files changed, 107 insertions(+), 7 deletions(-) create mode 100644 src/RouteRepository.php create mode 100644 src/Routes/AbstractRoute.php rename src/{Controllers/AuthController.php => Routes/Login.php} (89%) create mode 100644 tests/RouteRepositoryTest.php diff --git a/src/Dispatcher.php b/src/Dispatcher.php index b785142b..8a87a99d 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -24,6 +24,7 @@ final class Dispatcher HttpHelper $httpHelper, AuthService $authService, TokenService $tokenService, + RouteRepository $routeRepository, ControllerRepository $controllerRepository) { $this->router = $router; @@ -38,6 +39,8 @@ final class Dispatcher foreach ($controllerRepository->getControllers() as $controller) $controller->registerRoutes($router); + + $routeRepository->injectRoutes($router); } public function run($requestMethod, $requestUri) diff --git a/src/RouteRepository.php b/src/RouteRepository.php new file mode 100644 index 00000000..dc754041 --- /dev/null +++ b/src/RouteRepository.php @@ -0,0 +1,29 @@ +routes = $routes; + } + + public function getRoutes() + { + return $this->routes; + } + + public function injectRoutes(Router $router) + { + foreach ($this->routes as $route) + { + foreach ($route->getMethods() as $method) + { + $method = strtolower($method); + $router->$method($route->getUrl(), [$route, 'work']); + } + } + } +} diff --git a/src/Routes/AbstractRoute.php b/src/Routes/AbstractRoute.php new file mode 100644 index 00000000..efae6d59 --- /dev/null +++ b/src/Routes/AbstractRoute.php @@ -0,0 +1,11 @@ +tokenViewProxy = $tokenViewProxy; } - public function registerRoutes(Router $router) + public function getMethods() { - $router->post('/api/login', [$this, 'login']); - $router->put('/api/login', [$this, 'login']); + return ['POST', 'PUT']; } - public function login() + public function getUrl() + { + return '/api/login'; + } + + public function work() { if (isset($this->inputReader->userNameOrEmail) && isset($this->inputReader->password)) { diff --git a/src/di.php b/src/di.php index b43799a0..49fe2d2d 100644 --- a/src/di.php +++ b/src/di.php @@ -11,6 +11,7 @@ $publicDataDirectory = __DIR__ return [ \Szurubooru\Config::class => DI\object()->constructor($dataDirectory, $publicDataDirectory), + \Szurubooru\RouteRepository::class => DI\object()->constructor(DI\link('routes')), \Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')), \Szurubooru\Upgrades\UpgradeRepository::class => DI\object()->constructor(DI\link('upgrades')), @@ -56,7 +57,6 @@ return [ 'controllers' => DI\factory(function (DI\container $container) { return [ - $container->get(\Szurubooru\Controllers\AuthController::class), $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\PostController::class), @@ -70,4 +70,10 @@ return [ $container->get(\Szurubooru\Controllers\TagController::class), ]; }), + + 'routes' => DI\factory(function (DI\container $container) { + return [ + $container->get(\Szurubooru\Routes\Login::class), + ]; + }), ]; diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index ea603433..228a893c 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -3,6 +3,7 @@ namespace Szurubooru\Tests; use Szurubooru\ControllerRepository; use Szurubooru\Dispatcher; use Szurubooru\Helpers\HttpHelper; +use Szurubooru\RouteRepository; use Szurubooru\Router; use Szurubooru\Services\AuthService; use Szurubooru\Services\TokenService; @@ -16,6 +17,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase private $authServiceMock; private $tokenServiceMock; private $controllerRepositoryMock; + private $routeRepositoryMock; public function setUp() { @@ -26,6 +28,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase $this->authServiceMock = $this->mock(AuthService::class); $this->tokenServiceMock = $this->mock(TokenService::class); $this->controllerRepositoryMock = $this->mock(ControllerRepository::class); + $this->routeRepositoryMock = $this->mock(RouteRepository::class); $this->configMock->set('misc/dumpSqlIntoQueries', 0); } @@ -39,6 +42,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase ->withConsecutive([$this->equalTo(500)], [$this->equalTo(200)]); $this->routerMock->expects($this->once())->method('handle')->willReturn($expected); $this->controllerRepositoryMock->method('getControllers')->willReturn([]); + $this->routeRepositoryMock->expects($this->once())->method('injectRoutes'); $dispatcher = $this->getDispatcher(); $actual = $dispatcher->run('GET', '/'); @@ -55,6 +59,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase $this->routerMock->expects($this->once())->method('handle')->willReturn($classData); $this->controllerRepositoryMock->method('getControllers')->willReturn([]); + $this->routeRepositoryMock->expects($this->once())->method('injectRoutes'); $dispatcher = $this->getDispatcher(); $actual = $dispatcher->run('GET', '/'); @@ -68,6 +73,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase $this->httpHelperMock->expects($this->once())->method('getRequestHeader')->with($this->equalTo('X-Authorization-Token'))->willReturn('test'); $this->tokenServiceMock->expects($this->once())->method('getByName'); $this->controllerRepositoryMock->method('getControllers')->willReturn([]); + $this->routeRepositoryMock->expects($this->once())->method('injectRoutes'); $dispatcher = $this->getDispatcher(); $dispatcher->run('GET', '/'); @@ -82,6 +88,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase $this->httpHelperMock, $this->authServiceMock, $this->tokenServiceMock, + $this->routeRepositoryMock, $this->controllerRepositoryMock); } } diff --git a/tests/RouteRepositoryTest.php b/tests/RouteRepositoryTest.php new file mode 100644 index 00000000..39c3ca0f --- /dev/null +++ b/tests/RouteRepositoryTest.php @@ -0,0 +1,40 @@ +assertNotEmpty($routeRepository->getRoutes()); + } + + public function testRouteInjection() + { + $routerMock = $this->mock(Router::class); + $routeMock = $this->mock(AbstractRoute::class); + $routeMock->expects($this->once())->method('getMethods')->willReturn(['POST', 'GET']); + $routeMock->expects($this->atLeast(1))->method('getUrl')->willReturn('/test'); + $routerMock->expects($this->once())->method('post')->with('/test', $this->anything()); + $routerMock->expects($this->once())->method('get')->with('/test', $this->anything()); + $routeRepository = new RouteRepository([$routeMock]); + $routeRepository->injectRoutes($routerMock); + } + + public function testRouteCallbackInjection() + { + $router = new Router(); + $routeMock = $this->mock(AbstractRoute::class); + $routeMock->expects($this->once())->method('getMethods')->willReturn(['POST', 'GET']); + $routeMock->expects($this->atLeast(1))->method('getUrl')->willReturn('/test'); + $routeMock->expects($this->atLeast(1))->method('work'); + $routeRepository = new RouteRepository([$routeMock]); + $routeRepository->injectRoutes($router); + $router->handle('GET', '/test'); + } +} From 7c182f57a0805809f4c78b48292e6b1b09503c5d Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 20 Nov 2014 09:43:48 +0100 Subject: [PATCH 02/22] Added support for arguments in new Routes --- src/Route.php | 13 ++++++++++++- src/Routes/AbstractRoute.php | 12 ++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Route.php b/src/Route.php index c71f7f4b..78b3f3ca 100644 --- a/src/Route.php +++ b/src/Route.php @@ -19,8 +19,19 @@ final class Route if (!preg_match($this->regex, $query, $matches)) return false; $routeArguments = $this->getRouteArguments($matches); + $func = $this->route; - $output = $func(...array_values($routeArguments)); + if (is_array($this->route) && $this->route[1] === 'work') + { + foreach ($matches as $key => $value) + $this->route[0]->setArgument($key, $value); + $output = $func(); + } + else + { + $output = $func(...array_values($routeArguments)); + } + return true; } diff --git a/src/Routes/AbstractRoute.php b/src/Routes/AbstractRoute.php index efae6d59..03db8ac8 100644 --- a/src/Routes/AbstractRoute.php +++ b/src/Routes/AbstractRoute.php @@ -3,9 +3,21 @@ namespace Szurubooru\Routes; abstract class AbstractRoute { + protected $arguments = []; + public abstract function getMethods(); public abstract function getUrl(); public abstract function work(); + + public function setArgument($argName, $argValue) + { + $this->arguments[$argName] = $argValue; + } + + protected function getArgument($argName) + { + return $this->arguments[$argName]; + } } From 333c538f1e8bcf73998015111247caa0a13dec3a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 20 Nov 2014 10:12:34 +0100 Subject: [PATCH 03/22] Split comment controller to routes --- src/Controllers/CommentController.php | 155 ------------------- src/Routes/Comments/AbstractCommentRoute.php | 15 ++ src/Routes/Comments/AddComment.php | 54 +++++++ src/Routes/Comments/DeleteComment.php | 57 +++++++ src/Routes/Comments/EditComment.php | 58 +++++++ src/Routes/Comments/GetComments.php | 87 +++++++++++ src/Routes/Comments/GetPostComments.php | 68 ++++++++ src/di.php | 6 +- 8 files changed, 344 insertions(+), 156 deletions(-) delete mode 100644 src/Controllers/CommentController.php create mode 100644 src/Routes/Comments/AbstractCommentRoute.php create mode 100644 src/Routes/Comments/AddComment.php create mode 100644 src/Routes/Comments/DeleteComment.php create mode 100644 src/Routes/Comments/EditComment.php create mode 100644 src/Routes/Comments/GetComments.php create mode 100644 src/Routes/Comments/GetPostComments.php diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php deleted file mode 100644 index 03416b08..00000000 --- a/src/Controllers/CommentController.php +++ /dev/null @@ -1,155 +0,0 @@ -privilegeService = $privilegeService; - $this->authService = $authService; - $this->postService = $postService; - $this->commentService = $commentService; - $this->commentViewProxy = $commentViewProxy; - $this->postViewProxy = $postViewProxy; - $this->inputReader = $inputReader; - } - - public function registerRoutes(Router $router) - { - $router->get('/api/comments', [$this, 'getComments']); - $router->get('/api/comments/:postNameOrId', [$this, 'getPostComments']); - $router->post('/api/comments/:postNameOrId', [$this, 'addComment']); - $router->put('/api/comments/:commentId', [$this, 'editComment']); - $router->delete('/api/comments/:commentId', [$this, 'deleteComment']); - } - - public function getComments() - { - $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); - - $filter = new PostFilter(); - $filter->setPageSize(10); - $filter->setPageNumber($this->inputReader->page); - $filter->setOrder([ - PostFilter::ORDER_LAST_COMMENT_TIME => - PostFilter::ORDER_DESC]); - - $this->postService->decorateFilterFromBrowsingSettings($filter); - - $requirement = new Requirement(); - $requirement->setValue(new RequirementRangedValue()); - $requirement->getValue()->setMinValue(1); - $requirement->setType(PostFilter::REQUIREMENT_COMMENT_COUNT); - $filter->addRequirement($requirement); - - $result = $this->postService->getFiltered($filter); - $posts = $result->getEntities(); - - $data = []; - foreach ($posts as $post) - { - $data[] = [ - 'post' => $this->postViewProxy->fromEntity($post), - 'comments' => $this->commentViewProxy->fromArray( - array_reverse($this->commentService->getByPost($post)), - $this->getCommentsFetchConfig()), - ]; - } - - return [ - 'data' => $data, - 'pageSize' => $result->getPageSize(), - 'totalRecords' => $result->getTotalRecords()]; - } - - public function getPostComments($postNameOrId) - { - $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); - $post = $this->postService->getByNameOrId($postNameOrId); - - $filter = new CommentFilter(); - $filter->setOrder([ - CommentFilter::ORDER_ID => - CommentFilter::ORDER_ASC]); - - $requirement = new Requirement(); - $requirement->setValue(new RequirementSingleValue($post->getId())); - $requirement->setType(CommentFilter::REQUIREMENT_POST_ID); - $filter->addRequirement($requirement); - - $result = $this->commentService->getFiltered($filter); - $entities = $this->commentViewProxy->fromArray($result->getEntities(), $this->getCommentsFetchConfig()); - return ['data' => $entities]; - } - - public function addComment($postNameOrId) - { - $this->privilegeService->assertPrivilege(Privilege::ADD_COMMENTS); - - $post = $this->postService->getByNameOrId($postNameOrId); - $comment = $this->commentService->createComment($post, $this->inputReader->text); - return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); - } - - public function editComment($commentId) - { - $comment = $this->commentService->getById($commentId); - - $this->privilegeService->assertPrivilege( - ($comment->getUser() && $this->privilegeService->isLoggedIn($comment->getUser())) - ? Privilege::EDIT_OWN_COMMENTS - : Privilege::EDIT_ALL_COMMENTS); - - $comment = $this->commentService->updateComment($comment, $this->inputReader->text); - return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); - } - - public function deleteComment($commentId) - { - $comment = $this->commentService->getById($commentId); - - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($comment->getUser()) - ? Privilege::DELETE_OWN_COMMENTS - : Privilege::DELETE_ALL_COMMENTS); - - return $this->commentService->deleteComment($comment); - } - - private function getCommentsFetchConfig() - { - return - [ - CommentViewProxy::FETCH_OWN_SCORE => true, - ]; - } -} diff --git a/src/Routes/Comments/AbstractCommentRoute.php b/src/Routes/Comments/AbstractCommentRoute.php new file mode 100644 index 00000000..32448df9 --- /dev/null +++ b/src/Routes/Comments/AbstractCommentRoute.php @@ -0,0 +1,15 @@ + true, + ]; + } +} diff --git a/src/Routes/Comments/AddComment.php b/src/Routes/Comments/AddComment.php new file mode 100644 index 00000000..460e9a5b --- /dev/null +++ b/src/Routes/Comments/AddComment.php @@ -0,0 +1,54 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['POST']; + } + + public function getUrl() + { + return '/api/comments/:postNameOrId'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::ADD_COMMENTS); + + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $comment = $this->commentService->createComment($post, $this->inputReader->text); + return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); + } +} diff --git a/src/Routes/Comments/DeleteComment.php b/src/Routes/Comments/DeleteComment.php new file mode 100644 index 00000000..815ee3ef --- /dev/null +++ b/src/Routes/Comments/DeleteComment.php @@ -0,0 +1,57 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/comments/:commentId'; + } + + public function work() + { + $comment = $this->commentService->getById($this->getArgument('commentId')); + + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($comment->getUser()) + ? Privilege::DELETE_OWN_COMMENTS + : Privilege::DELETE_ALL_COMMENTS); + + return $this->commentService->deleteComment($comment); + } +} diff --git a/src/Routes/Comments/EditComment.php b/src/Routes/Comments/EditComment.php new file mode 100644 index 00000000..2f1d2897 --- /dev/null +++ b/src/Routes/Comments/EditComment.php @@ -0,0 +1,58 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/comments/:commentId'; + } + + public function work() + { + $comment = $this->commentService->getById($this->getArgument('commentId')); + + $this->privilegeService->assertPrivilege( + ($comment->getUser() && $this->privilegeService->isLoggedIn($comment->getUser())) + ? Privilege::EDIT_OWN_COMMENTS + : Privilege::EDIT_ALL_COMMENTS); + + $comment = $this->commentService->updateComment($comment, $this->inputReader->text); + return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); + } +} diff --git a/src/Routes/Comments/GetComments.php b/src/Routes/Comments/GetComments.php new file mode 100644 index 00000000..e0390d32 --- /dev/null +++ b/src/Routes/Comments/GetComments.php @@ -0,0 +1,87 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/comments'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); + + $filter = new PostFilter(); + $filter->setPageSize(10); + $filter->setPageNumber($this->inputReader->page); + $filter->setOrder([ + PostFilter::ORDER_LAST_COMMENT_TIME => + PostFilter::ORDER_DESC]); + + $this->postService->decorateFilterFromBrowsingSettings($filter); + + $requirement = new Requirement(); + $requirement->setValue(new RequirementRangedValue()); + $requirement->getValue()->setMinValue(1); + $requirement->setType(PostFilter::REQUIREMENT_COMMENT_COUNT); + $filter->addRequirement($requirement); + + $result = $this->postService->getFiltered($filter); + $posts = $result->getEntities(); + + $data = []; + foreach ($posts as $post) + { + $data[] = [ + 'post' => $this->postViewProxy->fromEntity($post), + 'comments' => $this->commentViewProxy->fromArray( + array_reverse($this->commentService->getByPost($post)), + $this->getCommentsFetchConfig()), + ]; + } + + return [ + 'data' => $data, + 'pageSize' => $result->getPageSize(), + 'totalRecords' => $result->getTotalRecords()]; + } +} diff --git a/src/Routes/Comments/GetPostComments.php b/src/Routes/Comments/GetPostComments.php new file mode 100644 index 00000000..7a20d478 --- /dev/null +++ b/src/Routes/Comments/GetPostComments.php @@ -0,0 +1,68 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->commentService = $commentService; + $this->commentViewProxy = $commentViewProxy; + $this->postViewProxy = $postViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/comments/:postNameOrId'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + + $filter = new CommentFilter(); + $filter->setOrder([ + CommentFilter::ORDER_ID => + CommentFilter::ORDER_ASC]); + + $requirement = new Requirement(); + $requirement->setValue(new RequirementSingleValue($post->getId())); + $requirement->setType(CommentFilter::REQUIREMENT_POST_ID); + $filter->addRequirement($requirement); + + $result = $this->commentService->getFiltered($filter); + $entities = $this->commentViewProxy->fromArray($result->getEntities(), $this->getCommentsFetchConfig()); + return ['data' => $entities]; + } +} diff --git a/src/di.php b/src/di.php index 49fe2d2d..7deef7d5 100644 --- a/src/di.php +++ b/src/di.php @@ -66,7 +66,6 @@ return [ $container->get(\Szurubooru\Controllers\HistoryController::class), $container->get(\Szurubooru\Controllers\FavoritesController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), - $container->get(\Szurubooru\Controllers\CommentController::class), $container->get(\Szurubooru\Controllers\TagController::class), ]; }), @@ -74,6 +73,11 @@ return [ 'routes' => DI\factory(function (DI\container $container) { return [ $container->get(\Szurubooru\Routes\Login::class), + $container->get(\Szurubooru\Routes\Comments\AddComment::class), + $container->get(\Szurubooru\Routes\Comments\EditComment::class), + $container->get(\Szurubooru\Routes\Comments\DeleteComment::class), + $container->get(\Szurubooru\Routes\Comments\GetComments::class), + $container->get(\Szurubooru\Routes\Comments\GetPostComments::class), ]; }), ]; From 602d7a1f4531afab7000d76651fca133baec002a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Thu, 20 Nov 2014 14:06:37 +0100 Subject: [PATCH 04/22] Split favorites controller to routes --- src/Routes/Favorites/AddToFavorites.php | 52 +++++++++++++++++++ src/Routes/Favorites/GetFavoriteUsers.php | 48 +++++++++++++++++ .../Favorites/RemoveFromFavorites.php} | 43 ++++++--------- src/di.php | 4 +- 4 files changed, 119 insertions(+), 28 deletions(-) create mode 100644 src/Routes/Favorites/AddToFavorites.php create mode 100644 src/Routes/Favorites/GetFavoriteUsers.php rename src/{Controllers/FavoritesController.php => Routes/Favorites/RemoveFromFavorites.php} (51%) diff --git a/src/Routes/Favorites/AddToFavorites.php b/src/Routes/Favorites/AddToFavorites.php new file mode 100644 index 00000000..e1ecc1cd --- /dev/null +++ b/src/Routes/Favorites/AddToFavorites.php @@ -0,0 +1,52 @@ +privilegeService = $privilegeService; + $this->authService = $authService; + $this->postService = $postService; + $this->favoritesService = $favoritesService; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId/favorites'; + } + + public function work() + { + $this->privilegeService->assertLoggedIn(); + $user = $this->authService->getLoggedInUser(); + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $this->favoritesService->addFavorite($user, $post); + + $users = $this->favoritesService->getFavoriteUsers($post); + return ['data' => $this->userViewProxy->fromArray($users)]; + } +} diff --git a/src/Routes/Favorites/GetFavoriteUsers.php b/src/Routes/Favorites/GetFavoriteUsers.php new file mode 100644 index 00000000..0c99a66e --- /dev/null +++ b/src/Routes/Favorites/GetFavoriteUsers.php @@ -0,0 +1,48 @@ +privilegeService = $privilegeService; + $this->authService = $authService; + $this->postService = $postService; + $this->favoritesService = $favoritesService; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId/favorites'; + } + + public function work() + { + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $users = $this->favoritesService->getFavoriteUsers($post); + return ['data' => $this->userViewProxy->fromArray($users)]; + } +} diff --git a/src/Controllers/FavoritesController.php b/src/Routes/Favorites/RemoveFromFavorites.php similarity index 51% rename from src/Controllers/FavoritesController.php rename to src/Routes/Favorites/RemoveFromFavorites.php index da8875c3..b96037ea 100644 --- a/src/Controllers/FavoritesController.php +++ b/src/Routes/Favorites/RemoveFromFavorites.php @@ -1,13 +1,13 @@ userViewProxy = $userViewProxy; } - public function registerRoutes(Router $router) + public function getMethods() { - $router->get('/api/posts/:postNameOrId/favorites', [$this, 'getFavoriteUsers']); - $router->post('/api/posts/:postNameOrId/favorites', [$this, 'addFavorite']); - $router->delete('/api/posts/:postNameOrId/favorites', [$this, 'deleteFavorite']); + return ['DELETE']; } - public function getFavoriteUsers($postNameOrId) + public function getUrl() { - $post = $this->postService->getByNameOrId($postNameOrId); + return '/api/posts/:postNameOrId/favorites'; + } + + public function work() + { + $this->privilegeService->assertLoggedIn(); + $user = $this->authService->getLoggedInUser(); + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $this->favoritesService->deleteFavorite($user, $post); + $users = $this->favoritesService->getFavoriteUsers($post); return ['data' => $this->userViewProxy->fromArray($users)]; } - - public function addFavorite($postNameOrId) - { - $this->privilegeService->assertLoggedIn(); - $user = $this->authService->getLoggedInUser(); - $post = $this->postService->getByNameOrId($postNameOrId); - $this->favoritesService->addFavorite($user, $post); - return $this->getFavoriteUsers($postNameOrId); - } - - public function deleteFavorite($postNameOrId) - { - $this->privilegeService->assertLoggedIn(); - $user = $this->authService->getLoggedInUser(); - $post = $this->postService->getByNameOrId($postNameOrId); - $this->favoritesService->deleteFavorite($user, $post); - return $this->getFavoriteUsers($postNameOrId); - } } diff --git a/src/di.php b/src/di.php index 7deef7d5..05a26113 100644 --- a/src/di.php +++ b/src/di.php @@ -64,7 +64,6 @@ return [ $container->get(\Szurubooru\Controllers\PostNotesController::class), $container->get(\Szurubooru\Controllers\GlobalParamController::class), $container->get(\Szurubooru\Controllers\HistoryController::class), - $container->get(\Szurubooru\Controllers\FavoritesController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\TagController::class), ]; @@ -78,6 +77,9 @@ return [ $container->get(\Szurubooru\Routes\Comments\DeleteComment::class), $container->get(\Szurubooru\Routes\Comments\GetComments::class), $container->get(\Szurubooru\Routes\Comments\GetPostComments::class), + $container->get(\Szurubooru\Routes\Favorites\GetFavoriteUsers::class), + $container->get(\Szurubooru\Routes\Favorites\AddToFavorites::class), + $container->get(\Szurubooru\Routes\Favorites\RemoveFromFavorites::class), ]; }), ]; From 40b16f586b62251877daddbbf987feb86438cb2b Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 10:26:00 +0100 Subject: [PATCH 05/22] Fixed RouteRepositoryTest using real DB --- tests/RouteRepositoryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/RouteRepositoryTest.php b/tests/RouteRepositoryTest.php index 39c3ca0f..6b050d3f 100644 --- a/tests/RouteRepositoryTest.php +++ b/tests/RouteRepositoryTest.php @@ -4,9 +4,9 @@ use Szurubooru\Injector; use Szurubooru\RouteRepository; use Szurubooru\Router; use Szurubooru\Routes\AbstractRoute; -use Szurubooru\Tests\AbstractTestCase; +use Szurubooru\Tests\AbstractDatabaseTestCase; -final class RouteRepositoryTest extends AbstractTestCase +final class RouteRepositoryTest extends AbstractDatabaseTestCase { public function testFactory() { From a3b02adb7f8187e45165c1aa453c175ad5f540d4 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 10:26:41 +0100 Subject: [PATCH 06/22] Moved global param controller to routes --- src/Controllers/GlobalParamController.php | 30 -------------------- src/Routes/GetGlobals.php | 34 +++++++++++++++++++++++ src/di.php | 2 +- 3 files changed, 35 insertions(+), 31 deletions(-) delete mode 100644 src/Controllers/GlobalParamController.php create mode 100644 src/Routes/GetGlobals.php diff --git a/src/Controllers/GlobalParamController.php b/src/Controllers/GlobalParamController.php deleted file mode 100644 index 9b644fbb..00000000 --- a/src/Controllers/GlobalParamController.php +++ /dev/null @@ -1,30 +0,0 @@ -globalParamDao = $globalParamDao; - } - - public function registerRoutes(Router $router) - { - $router->get('/api/globals', [$this, 'getGlobals']); - } - - public function getGlobals() - { - $globals = $this->globalParamDao->findAll(); - $return = []; - foreach ($globals as $global) - { - $return[$global->getKey()] = $global->getValue(); - } - return $return; - } -} diff --git a/src/Routes/GetGlobals.php b/src/Routes/GetGlobals.php new file mode 100644 index 00000000..a721833c --- /dev/null +++ b/src/Routes/GetGlobals.php @@ -0,0 +1,34 @@ +globalParamDao = $globalParamDao; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/globals'; + } + + public function work() + { + $globals = $this->globalParamDao->findAll(); + $result = []; + foreach ($globals as $global) + { + $result[$global->getKey()] = $global->getValue(); + } + return $result; + } +} diff --git a/src/di.php b/src/di.php index 05a26113..d846c0f4 100644 --- a/src/di.php +++ b/src/di.php @@ -62,7 +62,6 @@ return [ $container->get(\Szurubooru\Controllers\PostController::class), $container->get(\Szurubooru\Controllers\PostContentController::class), $container->get(\Szurubooru\Controllers\PostNotesController::class), - $container->get(\Szurubooru\Controllers\GlobalParamController::class), $container->get(\Szurubooru\Controllers\HistoryController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\TagController::class), @@ -72,6 +71,7 @@ return [ 'routes' => DI\factory(function (DI\container $container) { return [ $container->get(\Szurubooru\Routes\Login::class), + $container->get(\Szurubooru\Routes\GetGlobals::class), $container->get(\Szurubooru\Routes\Comments\AddComment::class), $container->get(\Szurubooru\Routes\Comments\EditComment::class), $container->get(\Szurubooru\Routes\Comments\DeleteComment::class), From a11436aa8cb7a178b10fb2d81c02252923bff23f Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 10:30:29 +0100 Subject: [PATCH 07/22] Added more tests to route repository test --- tests/RouteRepositoryTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/RouteRepositoryTest.php b/tests/RouteRepositoryTest.php index 6b050d3f..8d07208a 100644 --- a/tests/RouteRepositoryTest.php +++ b/tests/RouteRepositoryTest.php @@ -14,6 +14,27 @@ final class RouteRepositoryTest extends AbstractDatabaseTestCase $this->assertNotEmpty($routeRepository->getRoutes()); } + public function testRouteMethods() + { + $routeRepository = Injector::get(RouteRepository::class); + foreach ($routeRepository->getRoutes() as $route) + { + foreach ($route->getMethods() as $method) + { + $this->assertContains($method, ['GET', 'POST', 'PUT', 'DELETE']); + } + } + } + + public function testRouteUrls() + { + $routeRepository = Injector::get(RouteRepository::class); + foreach ($routeRepository->getRoutes() as $route) + { + $this->assertEquals(0, strpos($route->getUrl(), '/api/')); + } + } + public function testRouteInjection() { $routerMock = $this->mock(Router::class); From 7ff961fc2124dce09bc2c26f819029457b0bf43f Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 10:33:15 +0100 Subject: [PATCH 08/22] Moved history controller to routes --- .../GetHistory.php} | 17 +++++++++++------ src/di.php | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) rename src/{Controllers/HistoryController.php => Routes/GetHistory.php} (84%) diff --git a/src/Controllers/HistoryController.php b/src/Routes/GetHistory.php similarity index 84% rename from src/Controllers/HistoryController.php rename to src/Routes/GetHistory.php index 2291f8c0..9f205339 100644 --- a/src/Controllers/HistoryController.php +++ b/src/Routes/GetHistory.php @@ -1,14 +1,14 @@ snapshotViewProxy = $snapshotViewProxy; } - public function registerRoutes(Router $router) + public function getMethods() { - $router->get('/api/history', [$this, 'getFiltered']); + return ['GET']; } - public function getFiltered() + public function getUrl() + { + return '/api/history'; + } + + public function work() { $this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY); diff --git a/src/di.php b/src/di.php index d846c0f4..c71e2586 100644 --- a/src/di.php +++ b/src/di.php @@ -62,7 +62,6 @@ return [ $container->get(\Szurubooru\Controllers\PostController::class), $container->get(\Szurubooru\Controllers\PostContentController::class), $container->get(\Szurubooru\Controllers\PostNotesController::class), - $container->get(\Szurubooru\Controllers\HistoryController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\TagController::class), ]; @@ -72,6 +71,7 @@ return [ return [ $container->get(\Szurubooru\Routes\Login::class), $container->get(\Szurubooru\Routes\GetGlobals::class), + $container->get(\Szurubooru\Routes\GetHistory::class), $container->get(\Szurubooru\Routes\Comments\AddComment::class), $container->get(\Szurubooru\Routes\Comments\EditComment::class), $container->get(\Szurubooru\Routes\Comments\DeleteComment::class), From 193d1c5f7af3e0a0673e3377739769b886f22148 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 11:27:51 +0100 Subject: [PATCH 09/22] Moved post content controller to routes --- .../Posts/GetPostContent.php} | 33 +++++------- src/Routes/Posts/GetPostThumbnail.php | 50 +++++++++++++++++++ src/di.php | 3 +- 3 files changed, 65 insertions(+), 21 deletions(-) rename src/{Controllers/PostContentController.php => Routes/Posts/GetPostContent.php} (52%) create mode 100644 src/Routes/Posts/GetPostThumbnail.php diff --git a/src/Controllers/PostContentController.php b/src/Routes/Posts/GetPostContent.php similarity index 52% rename from src/Controllers/PostContentController.php rename to src/Routes/Posts/GetPostContent.php index ae8c58d7..1c53007c 100644 --- a/src/Controllers/PostContentController.php +++ b/src/Routes/Posts/GetPostContent.php @@ -1,45 +1,45 @@ config = $config; $this->fileDao = $fileDao; $this->postService = $postService; $this->networkingService = $networkingService; - $this->postThumbnailService = $postThumbnailService; } - public function registerRoutes(Router $router) + public function getMethods() { - $router->get('/api/posts/:postName/content', [$this, 'getPostContent']); - $router->get('/api/posts/:postName/thumbnail/:size', [$this, 'getPostThumbnail']); + return ['GET']; } - public function getPostContent($postName) + public function getUrl() { - $post = $this->postService->getByName($postName); + return '/api/posts/:postName/content'; + } + + public function work() + { + $post = $this->postService->getByName($this->getArgument('postName')); $customFileName = sprintf('%s_%s.%s', $this->config->basic->serviceName, @@ -54,11 +54,4 @@ final class PostContentController extends AbstractController $this->networkingService->serveFile($this->fileDao->getFullPath($post->getContentPath()), $customFileName); } - - public function getPostThumbnail($postName, $size) - { - $post = $this->postService->getByName($postName); - $thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size); - $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); - } } diff --git a/src/Routes/Posts/GetPostThumbnail.php b/src/Routes/Posts/GetPostThumbnail.php new file mode 100644 index 00000000..253d662f --- /dev/null +++ b/src/Routes/Posts/GetPostThumbnail.php @@ -0,0 +1,50 @@ +config = $config; + $this->fileDao = $fileDao; + $this->postService = $postService; + $this->networkingService = $networkingService; + $this->postThumbnailService = $postThumbnailService; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/posts/:postName/thumbnail/:size'; + } + + public function work() + { + $size = $this->getArgument('size'); + $post = $this->postService->getByName($this->getArgument('postName')); + $thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size); + $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); + } +} diff --git a/src/di.php b/src/di.php index c71e2586..d2bb4c16 100644 --- a/src/di.php +++ b/src/di.php @@ -60,7 +60,6 @@ return [ $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\PostController::class), - $container->get(\Szurubooru\Controllers\PostContentController::class), $container->get(\Szurubooru\Controllers\PostNotesController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\TagController::class), @@ -80,6 +79,8 @@ return [ $container->get(\Szurubooru\Routes\Favorites\GetFavoriteUsers::class), $container->get(\Szurubooru\Routes\Favorites\AddToFavorites::class), $container->get(\Szurubooru\Routes\Favorites\RemoveFromFavorites::class), + $container->get(\Szurubooru\Routes\Posts\GetPostContent::class), + $container->get(\Szurubooru\Routes\Posts\GetPostThumbnail::class), ]; }), ]; From d8a4e1ec4e42f8f88aee5bd0d06c052e89e9f6c9 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 12:45:47 +0100 Subject: [PATCH 10/22] Moved post controller to routes --- src/Controllers/PostController.php | 179 ------------------------- src/Routes/Posts/.UpdatePost.php.swp | Bin 0 -> 12288 bytes src/Routes/Posts/AbstractPostRoute.php | 29 ++++ src/Routes/Posts/CreatePost.php | 52 +++++++ src/Routes/Posts/DeletePost.php | 37 +++++ src/Routes/Posts/FeaturePost.php | 41 ++++++ src/Routes/Posts/GetFeaturedPost.php | 42 ++++++ src/Routes/Posts/GetPost.php | 41 ++++++ src/Routes/Posts/GetPostContent.php | 3 +- src/Routes/Posts/GetPostThumbnail.php | 3 +- src/Routes/Posts/GetPosts.php | 61 +++++++++ src/Routes/Posts/UpdatePost.php | 62 +++++++++ src/di.php | 8 +- 13 files changed, 374 insertions(+), 184 deletions(-) delete mode 100644 src/Controllers/PostController.php create mode 100644 src/Routes/Posts/.UpdatePost.php.swp create mode 100644 src/Routes/Posts/AbstractPostRoute.php create mode 100644 src/Routes/Posts/CreatePost.php create mode 100644 src/Routes/Posts/DeletePost.php create mode 100644 src/Routes/Posts/FeaturePost.php create mode 100644 src/Routes/Posts/GetFeaturedPost.php create mode 100644 src/Routes/Posts/GetPost.php create mode 100644 src/Routes/Posts/GetPosts.php create mode 100644 src/Routes/Posts/UpdatePost.php diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php deleted file mode 100644 index eeb76d37..00000000 --- a/src/Controllers/PostController.php +++ /dev/null @@ -1,179 +0,0 @@ -config = $config; - $this->authService = $authService; - $this->privilegeService = $privilegeService; - $this->postService = $postService; - $this->postFeatureService = $postFeatureService; - $this->postSearchParser = $postSearchParser; - $this->inputReader = $inputReader; - $this->userViewProxy = $userViewProxy; - $this->postViewProxy = $postViewProxy; - $this->snapshotViewProxy = $snapshotViewProxy; - } - - public function registerRoutes(Router $router) - { - $router->post('/api/posts', [$this, 'createPost']); - $router->get('/api/posts', [$this, 'getFiltered']); - $router->get('/api/posts/featured', [$this, 'getFeatured']); - $router->get('/api/posts/:postNameOrId', [$this, 'getByNameOrId']); - $router->get('/api/posts/:postNameOrId/history', [$this, 'getHistory']); - $router->put('/api/posts/:postNameOrId', [$this, 'updatePost']); - $router->delete('/api/posts/:postNameOrId', [$this, 'deletePost']); - $router->post('/api/posts/:postNameOrId/feature', [$this, 'featurePost']); - $router->put('/api/posts/:postNameOrId/feature', [$this, 'featurePost']); - } - - public function getFeatured() - { - $post = $this->postFeatureService->getFeaturedPost(); - $user = $this->postFeatureService->getFeaturedPostUser(); - return [ - 'user' => $this->userViewProxy->fromEntity($user), - 'post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()), - ]; - } - - public function getByNameOrId($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); - } - - public function getHistory($postNameOrId) - { - $this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY); - $post = $this->getByNameOrId($postNameOrId); - return ['data' => $this->snapshotViewProxy->fromArray($this->postService->getHistory($post))]; - } - - public function getFiltered() - { - $this->privilegeService->assertPrivilege(Privilege::LIST_POSTS); - - $filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader); - $filter->setPageSize($this->config->posts->postsPerPage); - $this->postService->decorateFilterFromBrowsingSettings($filter); - - $result = $this->postService->getFiltered($filter); - $entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig()); - return [ - 'data' => $entities, - 'pageSize' => $result->getPageSize(), - 'totalRecords' => $result->getTotalRecords()]; - } - - public function createPost() - { - $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS); - $formData = new UploadFormData($this->inputReader); - - $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS); - - if ($formData->anonymous) - $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS_ANONYMOUSLY); - - $post = $this->postService->createPost($formData); - return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); - } - - public function updatePost($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - $formData = new PostEditFormData($this->inputReader); - - if ($formData->content !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_CONTENT); - - if ($formData->thumbnail !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_THUMBNAIL); - - if ($formData->safety !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SAFETY); - - if ($formData->source !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SOURCE); - - if ($formData->tags !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_TAGS); - - $this->postService->updatePost($post, $formData); - $post = $this->postService->getByNameOrId($postNameOrId); - return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); - } - - public function deletePost($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - $this->postService->deletePost($post); - } - - public function featurePost($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - $this->postFeatureService->featurePost($post); - } - - private function getFullFetchConfig() - { - return - [ - PostViewProxy::FETCH_RELATIONS => true, - PostViewProxy::FETCH_TAGS => true, - PostViewProxy::FETCH_USER => true, - PostViewProxy::FETCH_HISTORY => true, - PostViewProxy::FETCH_OWN_SCORE => true, - PostViewProxy::FETCH_FAVORITES => true, - PostViewProxy::FETCH_NOTES => true, - ]; - } - - private function getLightFetchConfig() - { - return - [ - PostViewProxy::FETCH_TAGS => true, - ]; - } -} diff --git a/src/Routes/Posts/.UpdatePost.php.swp b/src/Routes/Posts/.UpdatePost.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..686c478627671bf97bc1950d2491ff2046d26e76 GIT binary patch literal 12288 zcmeI2&uZOP*C6b*I$AMNN*p0y$heT=|E4FEYplH1w+bh<)tC?9s zirQXVZdLz*_QZ+aD-icqz4ub})JknH?SVr@5A-{ZcfD~O1&N%{Tjis8cixYA-Pa_z7hJYCp-Z^c&dAz=t z2aS^gqyQ;E3XlS%04YEUkOHK@Yo>tm$Jh(hbO7YRSReeNixeOQNC8rS6d(mi0aAbz zAO%PPQh*d71zr;c%qC+K2N^q?KtcHbzy2G*^P`OY488>$pb9R66X35SjQt6of+ye` z@Ffu72KX3c!3ps1IAgznC*XVV0C3O%CMbf-U&{m_y_f zMCT`?8MHjny=p2GK1i4dXD;l!w!_;zay6yoLRG@&spwNSTU;!Zua(v-E7jV1t#GZ{ zOK-P~)MOYuomXaC8mH&y4Htz@?MtfaN`19h8kJ0Xfw1^~rz#}tE ze)l2LZjITq44ZObj(QUn1c=@~Tk)dXDU2@-7R8v1t0mJQ1Xjap zudibRNhJbHrJ%X?z)9|*S)tBoAU2`+K!j true, + PostViewProxy::FETCH_TAGS => true, + PostViewProxy::FETCH_USER => true, + PostViewProxy::FETCH_HISTORY => true, + PostViewProxy::FETCH_OWN_SCORE => true, + PostViewProxy::FETCH_FAVORITES => true, + PostViewProxy::FETCH_NOTES => true, + ]; + } + + private function getLightFetchConfig() + { + return + [ + PostViewProxy::FETCH_TAGS => true, + ]; + } +} diff --git a/src/Routes/Posts/CreatePost.php b/src/Routes/Posts/CreatePost.php new file mode 100644 index 00000000..88036ad6 --- /dev/null +++ b/src/Routes/Posts/CreatePost.php @@ -0,0 +1,52 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->inputReader = $inputReader; + $this->postViewProxy = $postViewProxy; + } + + public function getMethods() + { + return ['POST']; + } + + public function getUrl() + { + return '/api/posts'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS); + $formData = new UploadFormData($this->inputReader); + + $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS); + + if ($formData->anonymous) + $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS_ANONYMOUSLY); + + $post = $this->postService->createPost($formData); + return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); + } +} diff --git a/src/Routes/Posts/DeletePost.php b/src/Routes/Posts/DeletePost.php new file mode 100644 index 00000000..5d8f940f --- /dev/null +++ b/src/Routes/Posts/DeletePost.php @@ -0,0 +1,37 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::DELETE_POSTS); + + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $this->postService->deletePost($post); + } +} diff --git a/src/Routes/Posts/FeaturePost.php b/src/Routes/Posts/FeaturePost.php new file mode 100644 index 00000000..5eb8c902 --- /dev/null +++ b/src/Routes/Posts/FeaturePost.php @@ -0,0 +1,41 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->postFeatureService = $postFeatureService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId/feature'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::FEATURE_POSTS); + + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $this->postFeatureService->featurePost($post); + } +} diff --git a/src/Routes/Posts/GetFeaturedPost.php b/src/Routes/Posts/GetFeaturedPost.php new file mode 100644 index 00000000..8bb8371d --- /dev/null +++ b/src/Routes/Posts/GetFeaturedPost.php @@ -0,0 +1,42 @@ +postFeatureService = $postFeatureService; + $this->userViewProxy = $userViewProxy; + $this->postViewProxy = $postViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/posts/featured'; + } + + public function work() + { + $post = $this->postFeatureService->getFeaturedPost(); + $user = $this->postFeatureService->getFeaturedPostUser(); + return [ + 'user' => $this->userViewProxy->fromEntity($user), + 'post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()), + ]; + } +} diff --git a/src/Routes/Posts/GetPost.php b/src/Routes/Posts/GetPost.php new file mode 100644 index 00000000..650ad8f8 --- /dev/null +++ b/src/Routes/Posts/GetPost.php @@ -0,0 +1,41 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->postViewProxy = $postViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS); + + $post = $this->postService->getByNameOrId($this->getArgument(postNameOrId)); + return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); + } +} diff --git a/src/Routes/Posts/GetPostContent.php b/src/Routes/Posts/GetPostContent.php index 1c53007c..6e655871 100644 --- a/src/Routes/Posts/GetPostContent.php +++ b/src/Routes/Posts/GetPostContent.php @@ -4,11 +4,10 @@ use Szurubooru\Config; use Szurubooru\Dao\PublicFileDao; use Szurubooru\Entities\Post; use Szurubooru\Helpers\MimeHelper; -use Szurubooru\Routes\AbstractRoute; use Szurubooru\Services\NetworkingService; use Szurubooru\Services\PostService; -class GetPostContent extends AbstractRoute +class GetPostContent extends AbstractPostRoute { private $config; private $fileDao; diff --git a/src/Routes/Posts/GetPostThumbnail.php b/src/Routes/Posts/GetPostThumbnail.php index 253d662f..e6069b28 100644 --- a/src/Routes/Posts/GetPostThumbnail.php +++ b/src/Routes/Posts/GetPostThumbnail.php @@ -3,12 +3,11 @@ namespace Szurubooru\Routes\Posts; use Szurubooru\Config; use Szurubooru\Dao\PublicFileDao; use Szurubooru\Entities\Post; -use Szurubooru\Routes\AbstractRoute; use Szurubooru\Services\NetworkingService; use Szurubooru\Services\PostService; use Szurubooru\Services\PostThumbnailService; -class GetPostThumbnail extends AbstractRoute +class GetPostThumbnail extends AbstractPostRoute { private $config; private $fileDao; diff --git a/src/Routes/Posts/GetPosts.php b/src/Routes/Posts/GetPosts.php new file mode 100644 index 00000000..d6b91bc9 --- /dev/null +++ b/src/Routes/Posts/GetPosts.php @@ -0,0 +1,61 @@ +config = $config; + $this->privilegeService = $privilegeService; + $this->postService = $postService; + $this->postSearchParser = $postSearchParser; + $this->inputReader = $inputReader; + $this->postViewProxy = $postViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/posts'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_POSTS); + + $filter = $this->postSearchParser->createFilterFromInputReader($this->inputReader); + $filter->setPageSize($this->config->posts->postsPerPage); + $this->postService->decorateFilterFromBrowsingSettings($filter); + + $result = $this->postService->getFiltered($filter); + $entities = $this->postViewProxy->fromArray($result->getEntities(), $this->getLightFetchConfig()); + return [ + 'data' => $entities, + 'pageSize' => $result->getPageSize(), + 'totalRecords' => $result->getTotalRecords()]; + } +} diff --git a/src/Routes/Posts/UpdatePost.php b/src/Routes/Posts/UpdatePost.php new file mode 100644 index 00000000..f69dfd66 --- /dev/null +++ b/src/Routes/Posts/UpdatePost.php @@ -0,0 +1,62 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId'; + } + + public function work() + { + $postNameOrId = $this->getArgument('postNameOrId'); + $post = $this->postService->getByNameOrId($postNameOrId); + $formData = new PostEditFormData($this->inputReader); + + if ($formData->content !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_CONTENT); + + if ($formData->thumbnail !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_THUMBNAIL); + + if ($formData->safety !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SAFETY); + + if ($formData->source !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_SOURCE); + + if ($formData->tags !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_POST_TAGS); + + $this->postService->updatePost($post, $formData); + $post = $this->postService->getByNameOrId($postNameOrId); + return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); + } +} diff --git a/src/di.php b/src/di.php index d2bb4c16..912237a7 100644 --- a/src/di.php +++ b/src/di.php @@ -59,7 +59,6 @@ return [ return [ $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), - $container->get(\Szurubooru\Controllers\PostController::class), $container->get(\Szurubooru\Controllers\PostNotesController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\TagController::class), @@ -79,8 +78,15 @@ return [ $container->get(\Szurubooru\Routes\Favorites\GetFavoriteUsers::class), $container->get(\Szurubooru\Routes\Favorites\AddToFavorites::class), $container->get(\Szurubooru\Routes\Favorites\RemoveFromFavorites::class), + $container->get(\Szurubooru\Routes\Posts\CreatePost::class), + $container->get(\Szurubooru\Routes\Posts\DeletePost::class), + $container->get(\Szurubooru\Routes\Posts\FeaturePost::class), + $container->get(\Szurubooru\Routes\Posts\GetFeaturedPost::class), + $container->get(\Szurubooru\Routes\Posts\GetPost::class), $container->get(\Szurubooru\Routes\Posts\GetPostContent::class), $container->get(\Szurubooru\Routes\Posts\GetPostThumbnail::class), + $container->get(\Szurubooru\Routes\Posts\GetPosts::class), + $container->get(\Szurubooru\Routes\Posts\UpdatePost::class), ]; }), ]; From 9de6e7e739fd37bcda9ed26d9e1cc80baf7e2dd7 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 12:54:33 +0100 Subject: [PATCH 11/22] Moved post notes controller to routes --- src/Controllers/PostNotesController.php | 77 ----------------------- src/Routes/Posts/Notes/AddPostNote.php | 54 ++++++++++++++++ src/Routes/Posts/Notes/DeletePostNote.php | 49 +++++++++++++++ src/Routes/Posts/Notes/GetPostNotes.php | 49 +++++++++++++++ src/Routes/Posts/Notes/UpdatePostNote.php | 54 ++++++++++++++++ src/di.php | 5 +- 6 files changed, 210 insertions(+), 78 deletions(-) delete mode 100644 src/Controllers/PostNotesController.php create mode 100644 src/Routes/Posts/Notes/AddPostNote.php create mode 100644 src/Routes/Posts/Notes/DeletePostNote.php create mode 100644 src/Routes/Posts/Notes/GetPostNotes.php create mode 100644 src/Routes/Posts/Notes/UpdatePostNote.php diff --git a/src/Controllers/PostNotesController.php b/src/Controllers/PostNotesController.php deleted file mode 100644 index 83124ded..00000000 --- a/src/Controllers/PostNotesController.php +++ /dev/null @@ -1,77 +0,0 @@ -inputReader = $inputReader; - $this->postService = $postService; - $this->postNotesService = $postNotesService; - $this->privilegeService = $privilegeService; - $this->postNoteViewProxy = $postNoteViewProxy; - } - - public function registerRoutes(Router $router) - { - $router->get('/api/notes/:postNameOrId', [$this, 'getPostNotes']); - $router->post('/api/notes/:postNameOrId', [$this, 'addPostNote']); - $router->put('/api/notes/:postNoteId', [$this, 'editPostNote']); - $router->delete('/api/notes/:postNoteId', [$this, 'deletePostNote']); - } - - public function getPostNotes($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - $postNotes = $this->postNotesService->getByPost($post); - return $this->postNoteViewProxy->fromArray($postNotes); - } - - public function addPostNote($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - - $this->privilegeService->assertPrivilege(Privilege::ADD_POST_NOTES); - - $formData = new PostNoteFormData($this->inputReader); - $postNote = $this->postNotesService->createPostNote($post, $formData); - return $this->postNoteViewProxy->fromEntity($postNote); - } - - public function editPostNote($postNoteId) - { - $postNote = $this->postNotesService->getById($postNoteId); - - $this->privilegeService->assertPrivilege(Privilege::EDIT_POST_NOTES); - - $formData = new PostNoteFormData($this->inputReader); - $postNote = $this->postNotesService->updatePostNote($postNote, $formData); - return $this->postNoteViewProxy->fromEntity($postNote); - } - - public function deletePostNote($postNoteId) - { - $postNote = $this->postNotesService->getById($postNoteId); - $this->privilegeService->assertPrivilege(Privilege::DELETE_POST_NOTES); - return $this->postNotesService->deletePostNote($postNote); - } -} diff --git a/src/Routes/Posts/Notes/AddPostNote.php b/src/Routes/Posts/Notes/AddPostNote.php new file mode 100644 index 00000000..07d4e42c --- /dev/null +++ b/src/Routes/Posts/Notes/AddPostNote.php @@ -0,0 +1,54 @@ +inputReader = $inputReader; + $this->postService = $postService; + $this->postNotesService = $postNotesService; + $this->privilegeService = $privilegeService; + $this->postNoteViewProxy = $postNoteViewProxy; + } + + public function getMethods() + { + return ['POST']; + } + + public function getUrl() + { + return '/api/notes/:postNameOrId'; + } + + public function work() + { + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + + $this->privilegeService->assertPrivilege(Privilege::ADD_POST_NOTES); + + $formData = new PostNoteFormData($this->inputReader); + $postNote = $this->postNotesService->createPostNote($post, $formData); + return $this->postNoteViewProxy->fromEntity($postNote); + } +} diff --git a/src/Routes/Posts/Notes/DeletePostNote.php b/src/Routes/Posts/Notes/DeletePostNote.php new file mode 100644 index 00000000..1579fb63 --- /dev/null +++ b/src/Routes/Posts/Notes/DeletePostNote.php @@ -0,0 +1,49 @@ +inputReader = $inputReader; + $this->postService = $postService; + $this->postNotesService = $postNotesService; + $this->privilegeService = $privilegeService; + $this->postNoteViewProxy = $postNoteViewProxy; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/notes/:postNoteId'; + } + + public function work() + { + $postNote = $this->postNotesService->getById($this->getArgument('postNoteId')); + $this->privilegeService->assertPrivilege(Privilege::DELETE_POST_NOTES); + return $this->postNotesService->deletePostNote($postNote); + } +} diff --git a/src/Routes/Posts/Notes/GetPostNotes.php b/src/Routes/Posts/Notes/GetPostNotes.php new file mode 100644 index 00000000..929dd75a --- /dev/null +++ b/src/Routes/Posts/Notes/GetPostNotes.php @@ -0,0 +1,49 @@ +inputReader = $inputReader; + $this->postService = $postService; + $this->postNotesService = $postNotesService; + $this->privilegeService = $privilegeService; + $this->postNoteViewProxy = $postNoteViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/notes/:postNameOrId'; + } + + public function work() + { + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $postNotes = $this->postNotesService->getByPost($post); + return $this->postNoteViewProxy->fromArray($postNotes); + } +} diff --git a/src/Routes/Posts/Notes/UpdatePostNote.php b/src/Routes/Posts/Notes/UpdatePostNote.php new file mode 100644 index 00000000..11ee7825 --- /dev/null +++ b/src/Routes/Posts/Notes/UpdatePostNote.php @@ -0,0 +1,54 @@ +inputReader = $inputReader; + $this->postService = $postService; + $this->postNotesService = $postNotesService; + $this->privilegeService = $privilegeService; + $this->postNoteViewProxy = $postNoteViewProxy; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/notes/:postNoteId'; + } + + public function work() + { + $postNote = $this->postNotesService->getById($this->getArgument('postNoteId')); + + $this->privilegeService->assertPrivilege(Privilege::EDIT_POST_NOTES); + + $formData = new PostNoteFormData($this->inputReader); + $postNote = $this->postNotesService->updatePostNote($postNote, $formData); + return $this->postNoteViewProxy->fromEntity($postNote); + } +} diff --git a/src/di.php b/src/di.php index 912237a7..5f671570 100644 --- a/src/di.php +++ b/src/di.php @@ -59,7 +59,6 @@ return [ return [ $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), - $container->get(\Szurubooru\Controllers\PostNotesController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), $container->get(\Szurubooru\Controllers\TagController::class), ]; @@ -87,6 +86,10 @@ return [ $container->get(\Szurubooru\Routes\Posts\GetPostThumbnail::class), $container->get(\Szurubooru\Routes\Posts\GetPosts::class), $container->get(\Szurubooru\Routes\Posts\UpdatePost::class), + $container->get(\Szurubooru\Routes\Posts\Notes\AddPostNote::class), + $container->get(\Szurubooru\Routes\Posts\Notes\DeletePostNote::class), + $container->get(\Szurubooru\Routes\Posts\Notes\GetPostNotes::class), + $container->get(\Szurubooru\Routes\Posts\Notes\UpdatePostNote::class), ]; }), ]; From 76edbfeddb689e87cd4dca0ff204a2a529e954ae Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 15:49:53 +0100 Subject: [PATCH 12/22] Moved tag controller to routes --- src/Controllers/TagController.php | 127 --------------------------- src/Routes/Tags/.UpdateTag.php.swp | Bin 0 -> 12288 bytes src/Routes/Tags/AbstractTagRoute.php | 17 ++++ src/Routes/Tags/DeleteTag.php | 36 ++++++++ src/Routes/Tags/GetTag.php | 41 +++++++++ src/Routes/Tags/GetTagSiblings.php | 45 ++++++++++ src/Routes/Tags/GetTags.php | 56 ++++++++++++ src/Routes/Tags/MergeTags.php | 43 +++++++++ src/Routes/Tags/UpdateTag.php | 62 +++++++++++++ src/di.php | 7 +- 10 files changed, 306 insertions(+), 128 deletions(-) delete mode 100644 src/Controllers/TagController.php create mode 100644 src/Routes/Tags/.UpdateTag.php.swp create mode 100644 src/Routes/Tags/AbstractTagRoute.php create mode 100644 src/Routes/Tags/DeleteTag.php create mode 100644 src/Routes/Tags/GetTag.php create mode 100644 src/Routes/Tags/GetTagSiblings.php create mode 100644 src/Routes/Tags/GetTags.php create mode 100644 src/Routes/Tags/MergeTags.php create mode 100644 src/Routes/Tags/UpdateTag.php diff --git a/src/Controllers/TagController.php b/src/Controllers/TagController.php deleted file mode 100644 index 1b0be3bd..00000000 --- a/src/Controllers/TagController.php +++ /dev/null @@ -1,127 +0,0 @@ -privilegeService = $privilegeService; - $this->tagService = $tagService; - $this->tagViewProxy = $tagViewProxy; - $this->tagSearchParser = $tagSearchParser; - $this->inputReader = $inputReader; - } - - public function registerRoutes(Router $router) - { - $router->get('/api/tags', [$this, 'getTags']); - $router->get('/api/tags/:tagName', [$this, 'getTag']); - $router->get('/api/tags/:tagName/siblings', [$this, 'getTagSiblings']); - $router->put('/api/tags/:tagName', [$this, 'updateTag']); - $router->put('/api/tags/:tagName/merge', [$this, 'mergeTag']); - $router->delete('/api/tags/:tagName', [$this, 'deleteTag']); - } - - public function getTag($tagName) - { - $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); - - $tag = $this->tagService->getByName($tagName); - return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); - } - - public function getTags() - { - $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); - - $filter = $this->tagSearchParser->createFilterFromInputReader($this->inputReader); - $filter->setPageSize(50); - - $result = $this->tagService->getFiltered($filter); - $entities = $this->tagViewProxy->fromArray($result->getEntities(), $this->getFullFetchConfig()); - return [ - 'data' => $entities, - 'pageSize' => $result->getPageSize(), - 'totalRecords' => $result->getTotalRecords()]; - } - - public function getTagSiblings($tagName) - { - $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); - $tag = $this->tagService->getByName($tagName); - $result = $this->tagService->getSiblings($tagName); - $entities = $this->tagViewProxy->fromArray($result); - return [ - 'data' => $entities, - ]; - } - - public function updateTag($tagName) - { - $tag = $this->tagService->getByName($tagName); - $formData = new TagEditFormData($this->inputReader); - - if ($formData->name !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_NAME); - - if ($formData->category !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_CATEGORY); - - if ($formData->banned !== null) - $this->privilegeService->assertPrivilege(Privilege::BAN_TAGS); - - if ($formData->implications !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_IMPLICATIONS); - - if ($formData->suggestions !== null) - $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_SUGGESTIONS); - - $tag = $this->tagService->updateTag($tag, $formData); - return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); - } - - public function deleteTag($tagName) - { - $tag = $this->tagService->getByName($tagName); - $this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS); - return $this->tagService->deleteTag($tag); - } - - public function mergeTag($tagName) - { - $targetTagName = $this->inputReader->targetTag; - $sourceTag = $this->tagService->getByName($tagName); - $targetTag = $this->tagService->getByName($targetTagName); - $this->privilegeService->assertPrivilege(Privilege::MERGE_TAGS); - return $this->tagService->mergeTag($sourceTag, $targetTag); - } - - private function getFullFetchConfig() - { - return - [ - TagViewProxy::FETCH_IMPLICATIONS => true, - TagViewProxy::FETCH_SUGGESTIONS => true, - TagViewProxy::FETCH_HISTORY => true, - ]; - } -} diff --git a/src/Routes/Tags/.UpdateTag.php.swp b/src/Routes/Tags/.UpdateTag.php.swp new file mode 100644 index 0000000000000000000000000000000000000000..886672dce8c65fa9c11bc08e2a1bdd3874f97258 GIT binary patch literal 12288 zcmeI2&ube;6vroqK$RwKNLuJ2hfQOU3{jLHg#;^BkzH9)6Dz`!OhO#o)oQd}vf5o{ zW@9U%z4hKg=|50f+R#A$hLA!}Ewq;&8^|S)gg|e-wBK24H!In~rT7%y;t$fyyf^Q? z@64>hp3do&+LCep)CUaDVaC2~_^qeE9AN4_#)Qc0-d&#!U)g6vSV&~4DIep^&fD|AFNC8sdHBrFqGWPKsjICr)5dQzK{|4~)+l>7Iz5#c@3b+W4fERBu_8dF{ z55aff3$O`H@Cld)$G{YLa)_~?!B608Ai))o1=HXJ*bk<_%Y&F3JOjUhU%>-#AN&Zu z0$+m5;2m%j{PiYd_rMR}b8rEi2k(O44`3c}4_pV=z!~ruwf+gd2RFbqpzCh~dPo6M zfD|AFNC8rS6!?D%e9AKT$q24O;Th9v-I4i{GVK+IZ#0CzImnlK!tYl+<)}e!8Y3rT zE1Rpu&~+ENvep-TujkmgnVBLc(^ke@Y=%aYi`$OH^QADpGa1j+^F{w7QRo-~fn%^4oDpV^DJwWY?D+Cq6^V5c9rj%DsuWtHmH zC=qZfLXUR^_9FwW^cl{!?} zf*Cjkc*??T?2luzgEvC8g*)zlBim@VvNtEXEOE8&cV*(Xy<~kDQwm_F_@p79}>(Xf>=89%eyaJ+A;giP(#VvOW9gS=X)1n>q)wN7r0#(C(m;decfybzM{ z@!+r;U#+z@3-1CYLQCbaX4(VWc_b&KoOGwN8S8(gM`Ar5zsT+~%f*Y#NN$cBd{c3+ zD~)moS(uhW_0bh`#zM)BW^(y_bvVCVg+&r;l1(FiO-`~*42t7%E^#;DLawHkXq!!Z xwsbD(v$o9;zZ8`4T~|*S?=pu+HN?$9k?Aun1JfF*ck80Pjv$Nd;)nW%{l7L*hgtvt literal 0 HcmV?d00001 diff --git a/src/Routes/Tags/AbstractTagRoute.php b/src/Routes/Tags/AbstractTagRoute.php new file mode 100644 index 00000000..c06f4cfc --- /dev/null +++ b/src/Routes/Tags/AbstractTagRoute.php @@ -0,0 +1,17 @@ + true, + TagViewProxy::FETCH_SUGGESTIONS => true, + TagViewProxy::FETCH_HISTORY => true, + ]; + } +} diff --git a/src/Routes/Tags/DeleteTag.php b/src/Routes/Tags/DeleteTag.php new file mode 100644 index 00000000..14fea75d --- /dev/null +++ b/src/Routes/Tags/DeleteTag.php @@ -0,0 +1,36 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/tags/:tagName'; + } + + public function work() + { + $tag = $this->tagService->getByName($this->getArgument('tagName')); + $this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS); + return $this->tagService->deleteTag($tag); + } +} diff --git a/src/Routes/Tags/GetTag.php b/src/Routes/Tags/GetTag.php new file mode 100644 index 00000000..661f4385 --- /dev/null +++ b/src/Routes/Tags/GetTag.php @@ -0,0 +1,41 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/tags/:tagName'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); + + $tag = $this->tagService->getByName($this->getArgument('tagName')); + return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); + } +} diff --git a/src/Routes/Tags/GetTagSiblings.php b/src/Routes/Tags/GetTagSiblings.php new file mode 100644 index 00000000..36043de5 --- /dev/null +++ b/src/Routes/Tags/GetTagSiblings.php @@ -0,0 +1,45 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/tags/:tagName/siblings'; + } + + public function work() + { + $tagName = $this->getArgument('tagName'); + $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); + $tag = $this->tagService->getByName($tagName); + $result = $this->tagService->getSiblings($tagName); + $entities = $this->tagViewProxy->fromArray($result); + return [ + 'data' => $entities, + ]; + } +} diff --git a/src/Routes/Tags/GetTags.php b/src/Routes/Tags/GetTags.php new file mode 100644 index 00000000..ce162c0d --- /dev/null +++ b/src/Routes/Tags/GetTags.php @@ -0,0 +1,56 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + $this->tagSearchParser = $tagSearchParser; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/tags'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); + + $filter = $this->tagSearchParser->createFilterFromInputReader($this->inputReader); + $filter->setPageSize(50); + + $result = $this->tagService->getFiltered($filter); + $entities = $this->tagViewProxy->fromArray($result->getEntities(), $this->getFullFetchConfig()); + return [ + 'data' => $entities, + 'pageSize' => $result->getPageSize(), + 'totalRecords' => $result->getTotalRecords()]; + } +} diff --git a/src/Routes/Tags/MergeTags.php b/src/Routes/Tags/MergeTags.php new file mode 100644 index 00000000..13411a53 --- /dev/null +++ b/src/Routes/Tags/MergeTags.php @@ -0,0 +1,43 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/tags/:tagName/merge'; + } + + public function work() + { + $tagName = $this->getArgument('tagName'); + $targetTagName = $this->inputReader->targetTag; + $sourceTag = $this->tagService->getByName($tagName); + $targetTag = $this->tagService->getByName($targetTagName); + $this->privilegeService->assertPrivilege(Privilege::MERGE_TAGS); + return $this->tagService->mergeTag($sourceTag, $targetTag); + } +} diff --git a/src/Routes/Tags/UpdateTag.php b/src/Routes/Tags/UpdateTag.php new file mode 100644 index 00000000..15c80c3c --- /dev/null +++ b/src/Routes/Tags/UpdateTag.php @@ -0,0 +1,62 @@ +privilegeService = $privilegeService; + $this->tagService = $tagService; + $this->tagViewProxy = $tagViewProxy; + $this->inputReader = $inputReader; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/tags/:tagName'; + } + + public function work() + { + $tag = $this->tagService->getByName($this->getArgument('tagName')); + $formData = new TagEditFormData($this->inputReader); + + if ($formData->name !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_NAME); + + if ($formData->category !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_CATEGORY); + + if ($formData->banned !== null) + $this->privilegeService->assertPrivilege(Privilege::BAN_TAGS); + + if ($formData->implications !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_IMPLICATIONS); + + if ($formData->suggestions !== null) + $this->privilegeService->assertPrivilege(Privilege::CHANGE_TAG_SUGGESTIONS); + + $tag = $this->tagService->updateTag($tag, $formData); + return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); + } +} diff --git a/src/di.php b/src/di.php index 5f671570..e2952279 100644 --- a/src/di.php +++ b/src/di.php @@ -60,7 +60,6 @@ return [ $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), - $container->get(\Szurubooru\Controllers\TagController::class), ]; }), @@ -90,6 +89,12 @@ return [ $container->get(\Szurubooru\Routes\Posts\Notes\DeletePostNote::class), $container->get(\Szurubooru\Routes\Posts\Notes\GetPostNotes::class), $container->get(\Szurubooru\Routes\Posts\Notes\UpdatePostNote::class), + $container->get(\Szurubooru\Routes\Tags\DeleteTag::class), + $container->get(\Szurubooru\Routes\Tags\GetTag::class), + $container->get(\Szurubooru\Routes\Tags\GetTagSiblings::class), + $container->get(\Szurubooru\Routes\Tags\GetTags::class), + $container->get(\Szurubooru\Routes\Tags\MergeTags::class), + $container->get(\Szurubooru\Routes\Tags\UpdateTag::class), ]; }), ]; From 06cc77643842ea4b617ea031034203d5fa880acc Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 22:05:07 +0100 Subject: [PATCH 13/22] Removed .swp files... --- src/Routes/Posts/.UpdatePost.php.swp | Bin 12288 -> 0 bytes src/Routes/Tags/.UpdateTag.php.swp | Bin 12288 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/Routes/Posts/.UpdatePost.php.swp delete mode 100644 src/Routes/Tags/.UpdateTag.php.swp diff --git a/src/Routes/Posts/.UpdatePost.php.swp b/src/Routes/Posts/.UpdatePost.php.swp deleted file mode 100644 index 686c478627671bf97bc1950d2491ff2046d26e76..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2&uZOP*C6b*I$AMNN*p0y$heT=|E4FEYplH1w+bh<)tC?9s zirQXVZdLz*_QZ+aD-icqz4ub})JknH?SVr@5A-{ZcfD~O1&N%{Tjis8cixYA-Pa_z7hJYCp-Z^c&dAz=t z2aS^gqyQ;E3XlS%04YEUkOHK@Yo>tm$Jh(hbO7YRSReeNixeOQNC8rS6d(mi0aAbz zAO%PPQh*d71zr;c%qC+K2N^q?KtcHbzy2G*^P`OY488>$pb9R66X35SjQt6of+ye` z@Ffu72KX3c!3ps1IAgznC*XVV0C3O%CMbf-U&{m_y_f zMCT`?8MHjny=p2GK1i4dXD;l!w!_;zay6yoLRG@&spwNSTU;!Zua(v-E7jV1t#GZ{ zOK-P~)MOYuomXaC8mH&y4Htz@?MtfaN`19h8kJ0Xfw1^~rz#}tE ze)l2LZjITq44ZObj(QUn1c=@~Tk)dXDU2@-7R8v1t0mJQ1Xjap zudibRNhJbHrJ%X?z)9|*S)tBoAU2`+K!jhp3do&+LCep)CUaDVaC2~_^qeE9AN4_#)Qc0-d&#!U)g6vSV&~4DIep^&fD|AFNC8sdHBrFqGWPKsjICr)5dQzK{|4~)+l>7Iz5#c@3b+W4fERBu_8dF{ z55aff3$O`H@Cld)$G{YLa)_~?!B608Ai))o1=HXJ*bk<_%Y&F3JOjUhU%>-#AN&Zu z0$+m5;2m%j{PiYd_rMR}b8rEi2k(O44`3c}4_pV=z!~ruwf+gd2RFbqpzCh~dPo6M zfD|AFNC8rS6!?D%e9AKT$q24O;Th9v-I4i{GVK+IZ#0CzImnlK!tYl+<)}e!8Y3rT zE1Rpu&~+ENvep-TujkmgnVBLc(^ke@Y=%aYi`$OH^QADpGa1j+^F{w7QRo-~fn%^4oDpV^DJwWY?D+Cq6^V5c9rj%DsuWtHmH zC=qZfLXUR^_9FwW^cl{!?} zf*Cjkc*??T?2luzgEvC8g*)zlBim@VvNtEXEOE8&cV*(Xy<~kDQwm_F_@p79}>(Xf>=89%eyaJ+A;giP(#VvOW9gS=X)1n>q)wN7r0#(C(m;decfybzM{ z@!+r;U#+z@3-1CYLQCbaX4(VWc_b&KoOGwN8S8(gM`Ar5zsT+~%f*Y#NN$cBd{c3+ zD~)moS(uhW_0bh`#zM)BW^(y_bvVCVg+&r;l1(FiO-`~*42t7%E^#;DLawHkXq!!Z xwsbD(v$o9;zZ8`4T~|*S?=pu+HN?$9k?Aun1JfF*ck80Pjv$Nd;)nW%{l7L*hgtvt From 969f70318b945f7ecd5a2a2d9a0cbcccf305b52d Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 21:31:03 +0100 Subject: [PATCH 14/22] Moved user controller to routes --- src/Controllers/UserController.php | 176 ----------------------- src/Routes/Users/AbstractUserRoute.php | 7 + src/Routes/Users/ActivateAccount.php | 29 ++++ src/Routes/Users/CreateUser.php | 46 ++++++ src/Routes/Users/DeleteUser.php | 43 ++++++ src/Routes/Users/FinishActivation.php | 34 +++++ src/Routes/Users/FinishPasswordReset.php | 34 +++++ src/Routes/Users/GetUser.php | 46 ++++++ src/Routes/Users/GetUsers.php | 59 ++++++++ src/Routes/Users/PasswordReset.php | 27 ++++ src/Routes/Users/UpdateUser.php | 96 +++++++++++++ src/di.php | 10 +- 12 files changed, 430 insertions(+), 177 deletions(-) delete mode 100644 src/Controllers/UserController.php create mode 100644 src/Routes/Users/AbstractUserRoute.php create mode 100644 src/Routes/Users/ActivateAccount.php create mode 100644 src/Routes/Users/CreateUser.php create mode 100644 src/Routes/Users/DeleteUser.php create mode 100644 src/Routes/Users/FinishActivation.php create mode 100644 src/Routes/Users/FinishPasswordReset.php create mode 100644 src/Routes/Users/GetUser.php create mode 100644 src/Routes/Users/GetUsers.php create mode 100644 src/Routes/Users/PasswordReset.php create mode 100644 src/Routes/Users/UpdateUser.php diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php deleted file mode 100644 index 8e436017..00000000 --- a/src/Controllers/UserController.php +++ /dev/null @@ -1,176 +0,0 @@ -config = $config; - $this->privilegeService = $privilegeService; - $this->userService = $userService; - $this->tokenService = $tokenService; - $this->userSearchParser = $userSearchParser; - $this->inputReader = $inputReader; - $this->userViewProxy = $userViewProxy; - } - - public function registerRoutes(Router $router) - { - $router->post('/api/users', [$this, 'createUser']); - $router->get('/api/users', [$this, 'getFiltered']); - $router->get('/api/users/:userNameOrEmail', [$this, 'getByNameOrEmail']); - $router->put('/api/users/:userNameOrEmail', [$this, 'updateUser']); - $router->delete('/api/users/:userNameOrEmail', [$this, 'deleteUser']); - $router->post('/api/password-reset/:userNameOrEmail', [$this, 'passwordReset']); - $router->post('/api/finish-password-reset/:tokenName', [$this, 'finishPasswordReset']); - $router->post('/api/activation/:userNameOrEmail', [$this, 'activation']); - $router->post('/api/finish-activation/:tokenName', [$this, 'finishActivation']); - } - - public function getByNameOrEmail($userNameOrEmail) - { - if (!$this->privilegeService->isLoggedIn($userNameOrEmail)) - $this->privilegeService->assertPrivilege(Privilege::VIEW_USERS); - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - return $this->userViewProxy->fromEntity($user); - } - - public function getFiltered() - { - $this->privilegeService->assertPrivilege(Privilege::LIST_USERS); - - $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); - $filter->setPageSize($this->config->users->usersPerPage); - $result = $this->userService->getFiltered($filter); - $entities = $this->userViewProxy->fromArray($result->getEntities()); - return [ - 'data' => $entities, - 'pageSize' => $result->getPageSize(), - 'totalRecords' => $result->getTotalRecords()]; - } - - public function createUser() - { - $this->privilegeService->assertPrivilege(Privilege::REGISTER); - $formData = new RegistrationFormData($this->inputReader); - $user = $this->userService->createUser($formData); - return $this->userViewProxy->fromEntity($user); - } - - public function updateUser($userNameOrEmail) - { - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - $formData = new UserEditFormData($this->inputReader); - - if ($formData->avatarStyle !== null || $formData->avatarContent !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_AVATAR_STYLE - : Privilege::CHANGE_ALL_AVATAR_STYLES); - } - - if ($formData->userName !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_NAME - : Privilege::CHANGE_ALL_NAMES); - } - - if ($formData->password !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_PASSWORD - : Privilege::CHANGE_ALL_PASSWORDS); - } - - if ($formData->email !== null) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::CHANGE_OWN_EMAIL_ADDRESS - : Privilege::CHANGE_ALL_EMAIL_ADDRESSES); - } - - if ($formData->accessRank) - { - $this->privilegeService->assertPrivilege(Privilege::CHANGE_ACCESS_RANK); - } - - if ($formData->browsingSettings) - { - $this->privilegeService->assertLoggedIn($userNameOrEmail); - } - - if ($formData->banned !== null) - { - $this->privilegeService->assertPrivilege(Privilege::BAN_USERS); - } - - $user = $this->userService->updateUser($user, $formData); - return $this->userViewProxy->fromEntity($user); - } - - public function deleteUser($userNameOrEmail) - { - $this->privilegeService->assertPrivilege( - $this->privilegeService->isLoggedIn($userNameOrEmail) - ? Privilege::DELETE_OWN_ACCOUNT - : Privilege::DELETE_ACCOUNTS); - - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - return $this->userService->deleteUser($user); - } - - public function passwordReset($userNameOrEmail) - { - $user = $this->userService->getByNameOrEmail($userNameOrEmail); - return $this->userService->sendPasswordResetEmail($user); - } - - public function activation($userNameOrEmail) - { - $user = $this->userService->getByNameOrEmail($userNameOrEmail, true); - return $this->userService->sendActivationEmail($user); - } - - public function finishPasswordReset($tokenName) - { - $token = $this->tokenService->getByName($tokenName); - return ['newPassword' => $this->userService->finishPasswordReset($token)]; - } - - public function finishActivation($tokenName) - { - $token = $this->tokenService->getByName($tokenName); - $this->userService->finishActivation($token); - } -} diff --git a/src/Routes/Users/AbstractUserRoute.php b/src/Routes/Users/AbstractUserRoute.php new file mode 100644 index 00000000..9a2f3e35 --- /dev/null +++ b/src/Routes/Users/AbstractUserRoute.php @@ -0,0 +1,7 @@ +userService = $userService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/activation/:userNameOrEmail'; + } + + public function work() + { + $user = $this->userService->getByNameOrEmail($this->getArgument('userNameOrEmail'), true); + return $this->userService->sendActivationEmail($user); + } +} diff --git a/src/Routes/Users/CreateUser.php b/src/Routes/Users/CreateUser.php new file mode 100644 index 00000000..62b2f5b9 --- /dev/null +++ b/src/Routes/Users/CreateUser.php @@ -0,0 +1,46 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['POST']; + } + + public function getUrl() + { + return '/api/users'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::REGISTER); + $formData = new RegistrationFormData($this->inputReader); + $user = $this->userService->createUser($formData); + return $this->userViewProxy->fromEntity($user); + } +} diff --git a/src/Routes/Users/DeleteUser.php b/src/Routes/Users/DeleteUser.php new file mode 100644 index 00000000..5b5438f1 --- /dev/null +++ b/src/Routes/Users/DeleteUser.php @@ -0,0 +1,43 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/users/:userNameOrEmail'; + } + + public function work() + { + $userNameOrEmail = $this->getArgument('userNameOrEmail'); + + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::DELETE_OWN_ACCOUNT + : Privilege::DELETE_ALL_ACCOUNTS); + + $user = $this->userService->getByNameOrEmail($userNameOrEmail); + return $this->userService->deleteUser($user); + } +} diff --git a/src/Routes/Users/FinishActivation.php b/src/Routes/Users/FinishActivation.php new file mode 100644 index 00000000..8946cf10 --- /dev/null +++ b/src/Routes/Users/FinishActivation.php @@ -0,0 +1,34 @@ +userService = $userService; + $this->tokenService = $tokenService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/finish-activation/:tokenName'; + } + + public function work() + { + $token = $this->tokenService->getByName($this->getArgument('tokenName')); + $this->userService->finishActivation($token); + } +} diff --git a/src/Routes/Users/FinishPasswordReset.php b/src/Routes/Users/FinishPasswordReset.php new file mode 100644 index 00000000..c4ee8cb5 --- /dev/null +++ b/src/Routes/Users/FinishPasswordReset.php @@ -0,0 +1,34 @@ +userService = $userService; + $this->tokenService = $tokenService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/finish-password-reset/:tokenName'; + } + + public function work() + { + $token = $this->tokenService->getByName($this->getArgument('tokenName')); + return ['newPassword' => $this->userService->finishPasswordReset($token)]; + } +} diff --git a/src/Routes/Users/GetUser.php b/src/Routes/Users/GetUser.php new file mode 100644 index 00000000..b31f9977 --- /dev/null +++ b/src/Routes/Users/GetUser.php @@ -0,0 +1,46 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + $this->userSearchParser = $userSearchParser; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/users/:userNameOrEmail'; + } + + public function work() + { + $userNameOrEmail = $this->getArgument('userNameOrEmail'); + if (!$this->privilegeService->isLoggedIn($userNameOrEmail)) + $this->privilegeService->assertPrivilege(Privilege::VIEW_USERS); + $user = $this->userService->getByNameOrEmail($userNameOrEmail); + return $this->userViewProxy->fromEntity($user); + } +} diff --git a/src/Routes/Users/GetUsers.php b/src/Routes/Users/GetUsers.php new file mode 100644 index 00000000..00f3dad8 --- /dev/null +++ b/src/Routes/Users/GetUsers.php @@ -0,0 +1,59 @@ +config = $config; + $this->privilegeService = $privilegeService; + $this->userService = $userService; + $this->userSearchParser = $userSearchParser; + $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/users'; + } + + public function work() + { + $this->privilegeService->assertPrivilege(Privilege::LIST_USERS); + + $filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader); + $filter->setPageSize($this->config->users->usersPerPage); + $result = $this->userService->getFiltered($filter); + $entities = $this->userViewProxy->fromArray($result->getEntities()); + return [ + 'data' => $entities, + 'pageSize' => $result->getPageSize(), + 'totalRecords' => $result->getTotalRecords()]; + } +} diff --git a/src/Routes/Users/PasswordReset.php b/src/Routes/Users/PasswordReset.php new file mode 100644 index 00000000..70c4df27 --- /dev/null +++ b/src/Routes/Users/PasswordReset.php @@ -0,0 +1,27 @@ +userService = $userService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/password-reset/:userNameOrEmail'; + } + + public function work() + { + $user = $this->userService->getByNameOrEmail($this->getArgument('userNameOrEmail')); + return $this->userService->sendPasswordResetEmail($user); + } +} diff --git a/src/Routes/Users/UpdateUser.php b/src/Routes/Users/UpdateUser.php new file mode 100644 index 00000000..ec340e9f --- /dev/null +++ b/src/Routes/Users/UpdateUser.php @@ -0,0 +1,96 @@ +privilegeService = $privilegeService; + $this->userService = $userService; + $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/users/:userNameOrEmail'; + } + + public function work() + { + $userNameOrEmail = $this->getArgument('userNameOrEmail'); + + $user = $this->userService->getByNameOrEmail($userNameOrEmail); + $formData = new UserEditFormData($this->inputReader); + + if ($formData->avatarStyle !== null || $formData->avatarContent !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_AVATAR_STYLE + : Privilege::CHANGE_ALL_AVATAR_STYLES); + } + + if ($formData->userName !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_NAME + : Privilege::CHANGE_ALL_NAMES); + } + + if ($formData->password !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_PASSWORD + : Privilege::CHANGE_ALL_PASSWORDS); + } + + if ($formData->email !== null) + { + $this->privilegeService->assertPrivilege( + $this->privilegeService->isLoggedIn($userNameOrEmail) + ? Privilege::CHANGE_OWN_EMAIL_ADDRESS + : Privilege::CHANGE_ALL_EMAIL_ADDRESSES); + } + + if ($formData->accessRank) + { + $this->privilegeService->assertPrivilege(Privilege::CHANGE_ACCESS_RANK); + } + + if ($formData->browsingSettings) + { + $this->privilegeService->assertLoggedIn($userNameOrEmail); + } + + if ($formData->banned !== null) + { + $this->privilegeService->assertPrivilege(Privilege::BAN_USERS); + } + + $user = $this->userService->updateUser($user, $formData); + return $this->userViewProxy->fromEntity($user); + } +} diff --git a/src/di.php b/src/di.php index e2952279..f4047fcf 100644 --- a/src/di.php +++ b/src/di.php @@ -57,7 +57,6 @@ return [ 'controllers' => DI\factory(function (DI\container $container) { return [ - $container->get(\Szurubooru\Controllers\UserController::class), $container->get(\Szurubooru\Controllers\UserAvatarController::class), $container->get(\Szurubooru\Controllers\ScoreController::class), ]; @@ -95,6 +94,15 @@ return [ $container->get(\Szurubooru\Routes\Tags\GetTags::class), $container->get(\Szurubooru\Routes\Tags\MergeTags::class), $container->get(\Szurubooru\Routes\Tags\UpdateTag::class), + $container->get(\Szurubooru\Routes\Users\ActivateAccount::class), + $container->get(\Szurubooru\Routes\Users\CreateUser::class), + $container->get(\Szurubooru\Routes\Users\DeleteUser::class), + $container->get(\Szurubooru\Routes\Users\FinishActivation::class), + $container->get(\Szurubooru\Routes\Users\FinishPasswordReset::class), + $container->get(\Szurubooru\Routes\Users\GetUser::class), + $container->get(\Szurubooru\Routes\Users\GetUsers::class), + $container->get(\Szurubooru\Routes\Users\PasswordReset::class), + $container->get(\Szurubooru\Routes\Users\UpdateUser::class), ]; }), ]; From 2195b2c9a1aa07c0fb7f9741c8cd30f273690297 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 21:42:05 +0100 Subject: [PATCH 15/22] Moved scores controller to routes --- src/Controllers/ScoreController.php | 89 ------------------------ src/Routes/Scores/AbstractScoreRoute.php | 48 +++++++++++++ src/Routes/Scores/GetCommentScore.php | 45 ++++++++++++ src/Routes/Scores/GetPostScore.php | 45 ++++++++++++ src/Routes/Scores/SetCommentScore.php | 45 ++++++++++++ src/Routes/Scores/SetPostScore.php | 45 ++++++++++++ src/di.php | 5 +- 7 files changed, 232 insertions(+), 90 deletions(-) delete mode 100644 src/Controllers/ScoreController.php create mode 100644 src/Routes/Scores/AbstractScoreRoute.php create mode 100644 src/Routes/Scores/GetCommentScore.php create mode 100644 src/Routes/Scores/GetPostScore.php create mode 100644 src/Routes/Scores/SetCommentScore.php create mode 100644 src/Routes/Scores/SetPostScore.php diff --git a/src/Controllers/ScoreController.php b/src/Controllers/ScoreController.php deleted file mode 100644 index c0efc8ae..00000000 --- a/src/Controllers/ScoreController.php +++ /dev/null @@ -1,89 +0,0 @@ -privilegeService = $privilegeService; - $this->authService = $authService; - $this->postService = $postService; - $this->commentService = $commentService; - $this->scoreService = $scoreService; - $this->inputReader = $inputReader; - } - - public function registerRoutes(Router $router) - { - $router->get('/api/posts/:postNameOrId/score', [$this, 'getPostScore']); - $router->post('/api/posts/:postNameOrId/score', [$this, 'setPostScore']); - $router->get('/api/comments/:commentId/score', [$this, 'getCommentScore']); - $router->post('/api/comments/:commentId/score', [$this, 'setCommentScore']); - } - - public function getPostScore($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - return $this->getScore($post); - } - - public function setPostScore($postNameOrId) - { - $post = $this->postService->getByNameOrId($postNameOrId); - return $this->setScore($post); - } - - public function getCommentScore($commentId) - { - $comment = $this->commentService->getById($commentId); - return $this->getScore($comment); - } - - public function setCommentScore($commentId) - { - $comment = $this->commentService->getById($commentId); - return $this->setScore($comment); - } - - private function setScore(Entity $entity) - { - $this->privilegeService->assertLoggedIn(); - $score = intval($this->inputReader->score); - $user = $this->authService->getLoggedInUser(); - $result = $this->scoreService->setUserScore($user, $entity, $score); - return [ - 'score' => $this->scoreService->getScoreValue($entity), - 'ownScore' => $result->getScore(), - ]; - } - - private function getScore(Entity $entity) - { - $user = $this->authService->getLoggedInUser(); - return [ - 'score' => $this->scoreService->getScoreValue($entity), - 'ownScore' => $this->scoreService->getUserScoreValue($user, $entity), - ]; - } -} diff --git a/src/Routes/Scores/AbstractScoreRoute.php b/src/Routes/Scores/AbstractScoreRoute.php new file mode 100644 index 00000000..44807816 --- /dev/null +++ b/src/Routes/Scores/AbstractScoreRoute.php @@ -0,0 +1,48 @@ +authService = $authService; + $this->inputReader = $inputReader; + $this->privilegeService = $privilegeService; + $this->scoreService = $scoreService; + } + + protected function getScore(Entity $entity) + { + $user = $this->authService->getLoggedInUser(); + return [ + 'score' => $this->scoreService->getScoreValue($entity), + 'ownScore' => $this->scoreService->getUserScoreValue($user, $entity), + ]; + } + + protected function setScore(Entity $entity) + { + $this->privilegeService->assertLoggedIn(); + $score = intval($this->inputReader->score); + $user = $this->authService->getLoggedInUser(); + $result = $this->scoreService->setUserScore($user, $entity, $score); + return [ + 'score' => $this->scoreService->getScoreValue($entity), + 'ownScore' => $result->getScore(), + ]; + } +} diff --git a/src/Routes/Scores/GetCommentScore.php b/src/Routes/Scores/GetCommentScore.php new file mode 100644 index 00000000..50d87ea3 --- /dev/null +++ b/src/Routes/Scores/GetCommentScore.php @@ -0,0 +1,45 @@ +commentService = $commentService; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/comments/:commentId/score'; + } + + public function work() + { + $comment = $this->commentService->getById($this->getArgument('commentId')); + return $this->getScore($comment); + } +} diff --git a/src/Routes/Scores/GetPostScore.php b/src/Routes/Scores/GetPostScore.php new file mode 100644 index 00000000..f56ea993 --- /dev/null +++ b/src/Routes/Scores/GetPostScore.php @@ -0,0 +1,45 @@ +postService = $postService; + } + + public function getMethods() + { + return ['GET']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId/score'; + } + + public function work() + { + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + return $this->getScore($post); + } +} diff --git a/src/Routes/Scores/SetCommentScore.php b/src/Routes/Scores/SetCommentScore.php new file mode 100644 index 00000000..91597e6c --- /dev/null +++ b/src/Routes/Scores/SetCommentScore.php @@ -0,0 +1,45 @@ +commentService = $commentService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/comments/:commentId/score'; + } + + public function work() + { + $comment = $this->commentService->getById($this->getArgument('commentId')); + return $this->setScore($comment); + } +} diff --git a/src/Routes/Scores/SetPostScore.php b/src/Routes/Scores/SetPostScore.php new file mode 100644 index 00000000..1256369e --- /dev/null +++ b/src/Routes/Scores/SetPostScore.php @@ -0,0 +1,45 @@ +postService = $postService; + } + + public function getMethods() + { + return ['POST', 'PUT']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId/score'; + } + + public function work() + { + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + return $this->setScore($post); + } +} diff --git a/src/di.php b/src/di.php index f4047fcf..930511e1 100644 --- a/src/di.php +++ b/src/di.php @@ -58,7 +58,6 @@ return [ 'controllers' => DI\factory(function (DI\container $container) { return [ $container->get(\Szurubooru\Controllers\UserAvatarController::class), - $container->get(\Szurubooru\Controllers\ScoreController::class), ]; }), @@ -103,6 +102,10 @@ return [ $container->get(\Szurubooru\Routes\Users\GetUsers::class), $container->get(\Szurubooru\Routes\Users\PasswordReset::class), $container->get(\Szurubooru\Routes\Users\UpdateUser::class), + $container->get(\Szurubooru\Routes\Scores\GetCommentScore::class), + $container->get(\Szurubooru\Routes\Scores\SetCommentScore::class), + $container->get(\Szurubooru\Routes\Scores\GetPostScore::class), + $container->get(\Szurubooru\Routes\Scores\SetPostScore::class), ]; }), ]; From e38152b9212589279d23eda144c31f016f22f549 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 21:44:38 +0100 Subject: [PATCH 16/22] Moved user avatar controller to routes --- .../Users/GetUserAvatar.php} | 18 +++++++++++++----- src/di.php | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) rename src/{Controllers/UserAvatarController.php => Routes/Users/GetUserAvatar.php} (85%) diff --git a/src/Controllers/UserAvatarController.php b/src/Routes/Users/GetUserAvatar.php similarity index 85% rename from src/Controllers/UserAvatarController.php rename to src/Routes/Users/GetUserAvatar.php index 6cd9c4ae..42c363fb 100644 --- a/src/Controllers/UserAvatarController.php +++ b/src/Routes/Users/GetUserAvatar.php @@ -1,5 +1,5 @@ thumbnailService = $thumbnailService; } - public function registerRoutes(Router $router) + public function getMethods() { - $router->get('/api/users/:userName/avatar/:size', [$this, 'getAvatarByName']); + return ['GET']; } - public function getAvatarByName($userName, $size) + public function getUrl() { + return '/api/users/:userName/avatar/:size'; + } + + public function work() + { + $userName = $this->getArgument('userName'); + $size = $this->getArgument('size'); + try { $user = $this->userService->getByName($userName); diff --git a/src/di.php b/src/di.php index 930511e1..e8073a69 100644 --- a/src/di.php +++ b/src/di.php @@ -57,7 +57,6 @@ return [ 'controllers' => DI\factory(function (DI\container $container) { return [ - $container->get(\Szurubooru\Controllers\UserAvatarController::class), ]; }), @@ -99,6 +98,7 @@ return [ $container->get(\Szurubooru\Routes\Users\FinishActivation::class), $container->get(\Szurubooru\Routes\Users\FinishPasswordReset::class), $container->get(\Szurubooru\Routes\Users\GetUser::class), + $container->get(\Szurubooru\Routes\Users\GetUserAvatar::class), $container->get(\Szurubooru\Routes\Users\GetUsers::class), $container->get(\Szurubooru\Routes\Users\PasswordReset::class), $container->get(\Szurubooru\Routes\Users\UpdateUser::class), From 3245c751879c4cc983aa3f302e168c404f3ae369 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 21:49:26 +0100 Subject: [PATCH 17/22] Removed controller layer --- src/ControllerRepository.php | 17 ----------------- src/Controllers/AbstractController.php | 8 -------- src/Dispatcher.php | 7 +------ src/di.php | 6 ------ tests/ControllerRepositoryTest.php | 13 ------------- tests/DispatcherTest.php | 9 +-------- 6 files changed, 2 insertions(+), 58 deletions(-) delete mode 100644 src/ControllerRepository.php delete mode 100644 src/Controllers/AbstractController.php delete mode 100644 tests/ControllerRepositoryTest.php diff --git a/src/ControllerRepository.php b/src/ControllerRepository.php deleted file mode 100644 index 2fc2e26b..00000000 --- a/src/ControllerRepository.php +++ /dev/null @@ -1,17 +0,0 @@ -controllers = $controllers; - } - - public function getControllers() - { - return $this->controllers; - } -} diff --git a/src/Controllers/AbstractController.php b/src/Controllers/AbstractController.php deleted file mode 100644 index feed4ab0..00000000 --- a/src/Controllers/AbstractController.php +++ /dev/null @@ -1,8 +0,0 @@ -router = $router; $this->config = $config; @@ -37,9 +35,6 @@ final class Dispatcher //if script fails prematurely, mark it as fail from advance $this->httpHelper->setResponseCode(500); - foreach ($controllerRepository->getControllers() as $controller) - $controller->registerRoutes($router); - $routeRepository->injectRoutes($router); } diff --git a/src/di.php b/src/di.php index e8073a69..73ef2304 100644 --- a/src/di.php +++ b/src/di.php @@ -12,7 +12,6 @@ return [ \Szurubooru\Config::class => DI\object()->constructor($dataDirectory, $publicDataDirectory), \Szurubooru\RouteRepository::class => DI\object()->constructor(DI\link('routes')), - \Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')), \Szurubooru\Upgrades\UpgradeRepository::class => DI\object()->constructor(DI\link('upgrades')), 'upgrades' => DI\factory(function (DI\container $container) { @@ -55,11 +54,6 @@ return [ ]; }), - 'controllers' => DI\factory(function (DI\container $container) { - return [ - ]; - }), - 'routes' => DI\factory(function (DI\container $container) { return [ $container->get(\Szurubooru\Routes\Login::class), diff --git a/tests/ControllerRepositoryTest.php b/tests/ControllerRepositoryTest.php deleted file mode 100644 index e9ca7063..00000000 --- a/tests/ControllerRepositoryTest.php +++ /dev/null @@ -1,13 +0,0 @@ -assertNotEmpty($controllerRepository->getControllers()); - } -} diff --git a/tests/DispatcherTest.php b/tests/DispatcherTest.php index 228a893c..63f0498c 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -1,6 +1,5 @@ httpHelperMock = $this->mock(HttpHelper::class); $this->authServiceMock = $this->mock(AuthService::class); $this->tokenServiceMock = $this->mock(TokenService::class); - $this->controllerRepositoryMock = $this->mock(ControllerRepository::class); $this->routeRepositoryMock = $this->mock(RouteRepository::class); $this->configMock->set('misc/dumpSqlIntoQueries', 0); } @@ -41,7 +38,6 @@ final class DispatcherTest extends AbstractDatabaseTestCase ->method('setResponseCode') ->withConsecutive([$this->equalTo(500)], [$this->equalTo(200)]); $this->routerMock->expects($this->once())->method('handle')->willReturn($expected); - $this->controllerRepositoryMock->method('getControllers')->willReturn([]); $this->routeRepositoryMock->expects($this->once())->method('injectRoutes'); $dispatcher = $this->getDispatcher(); @@ -58,7 +54,6 @@ final class DispatcherTest extends AbstractDatabaseTestCase $expected = ['bunny' => 5]; $this->routerMock->expects($this->once())->method('handle')->willReturn($classData); - $this->controllerRepositoryMock->method('getControllers')->willReturn([]); $this->routeRepositoryMock->expects($this->once())->method('injectRoutes'); $dispatcher = $this->getDispatcher(); @@ -72,7 +67,6 @@ final class DispatcherTest extends AbstractDatabaseTestCase { $this->httpHelperMock->expects($this->once())->method('getRequestHeader')->with($this->equalTo('X-Authorization-Token'))->willReturn('test'); $this->tokenServiceMock->expects($this->once())->method('getByName'); - $this->controllerRepositoryMock->method('getControllers')->willReturn([]); $this->routeRepositoryMock->expects($this->once())->method('injectRoutes'); $dispatcher = $this->getDispatcher(); @@ -88,7 +82,6 @@ final class DispatcherTest extends AbstractDatabaseTestCase $this->httpHelperMock, $this->authServiceMock, $this->tokenServiceMock, - $this->routeRepositoryMock, - $this->controllerRepositoryMock); + $this->routeRepositoryMock); } } From bd33b09f7b802e3b91a5a0c8a92b7ccc923f848a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 21:54:50 +0100 Subject: [PATCH 18/22] Fixed private methods --- src/Routes/Posts/AbstractPostRoute.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Routes/Posts/AbstractPostRoute.php b/src/Routes/Posts/AbstractPostRoute.php index da0ebc1f..5fd627ba 100644 --- a/src/Routes/Posts/AbstractPostRoute.php +++ b/src/Routes/Posts/AbstractPostRoute.php @@ -5,7 +5,7 @@ use Szurubooru\Controllers\ViewProxies\PostViewProxy; abstract class AbstractPostRoute extends AbstractRoute { - private function getFullFetchConfig() + protected function getFullFetchConfig() { return [ @@ -19,7 +19,7 @@ abstract class AbstractPostRoute extends AbstractRoute ]; } - private function getLightFetchConfig() + protected function getLightFetchConfig() { return [ From 9621810332280a82e7ba944b345cca397833c4bb Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 21:59:03 +0100 Subject: [PATCH 19/22] Fixed syntax error --- src/Routes/Posts/GetPost.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Routes/Posts/GetPost.php b/src/Routes/Posts/GetPost.php index 650ad8f8..722817a4 100644 --- a/src/Routes/Posts/GetPost.php +++ b/src/Routes/Posts/GetPost.php @@ -35,7 +35,7 @@ class GetPost extends AbstractPostRoute { $this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS); - $post = $this->postService->getByNameOrId($this->getArgument(postNameOrId)); + $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); } } From 8fb1b87ae5ff2d45de18553d5bd21d9a1d958f05 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 22:07:50 +0100 Subject: [PATCH 20/22] Fixed post editing --- src/Routes/Posts/UpdatePost.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Routes/Posts/UpdatePost.php b/src/Routes/Posts/UpdatePost.php index f69dfd66..9f2f21da 100644 --- a/src/Routes/Posts/UpdatePost.php +++ b/src/Routes/Posts/UpdatePost.php @@ -22,6 +22,8 @@ class UpdatePost extends AbstractPostRoute { $this->privilegeService = $privilegeService; $this->postService = $postService; + $this->inputReader = $inputReader; + $this->postViewProxy = $postViewProxy; } public function getMethods() From 2d1b5308f3d9b545a91495e4316bb653d2427a85 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Fri, 21 Nov 2014 21:57:39 +0100 Subject: [PATCH 21/22] Removed unneeded dependencies --- src/Routes/Posts/Notes/DeletePostNote.php | 14 +------------- src/Routes/Posts/Notes/UpdatePostNote.php | 4 ---- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/src/Routes/Posts/Notes/DeletePostNote.php b/src/Routes/Posts/Notes/DeletePostNote.php index 1579fb63..04408b41 100644 --- a/src/Routes/Posts/Notes/DeletePostNote.php +++ b/src/Routes/Posts/Notes/DeletePostNote.php @@ -1,33 +1,21 @@ inputReader = $inputReader; - $this->postService = $postService; $this->postNotesService = $postNotesService; $this->privilegeService = $privilegeService; - $this->postNoteViewProxy = $postNoteViewProxy; } public function getMethods() diff --git a/src/Routes/Posts/Notes/UpdatePostNote.php b/src/Routes/Posts/Notes/UpdatePostNote.php index 11ee7825..c4468ae0 100644 --- a/src/Routes/Posts/Notes/UpdatePostNote.php +++ b/src/Routes/Posts/Notes/UpdatePostNote.php @@ -6,26 +6,22 @@ use Szurubooru\Helpers\InputReader; use Szurubooru\Privilege; use Szurubooru\Routes\Posts\AbstractPostRoute; use Szurubooru\Services\PostNotesService; -use Szurubooru\Services\PostService; use Szurubooru\Services\PrivilegeService; class UpdatePostNote extends AbstractPostRoute { private $inputReader; - private $postService; private $postNotesService; private $privilegeService; private $postNoteViewProxy; public function __construct( InputReader $inputReader, - PostService $postService, PostNotesService $postNotesService, PrivilegeService $privilegeService, PostNoteViewProxy $postNoteViewProxy) { $this->inputReader = $inputReader; - $this->postService = $postService; $this->postNotesService = $postNotesService; $this->privilegeService = $privilegeService; $this->postNoteViewProxy = $postNoteViewProxy; From 48230a64adae859817bbc304a5398fcdbe97d890 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 22 Nov 2014 12:44:45 +0100 Subject: [PATCH 22/22] Simplified routing --- src/Route.php | 62 -------------------- src/Router.php | 45 ++++++++------ src/Routes/AbstractRoute.php | 14 +---- src/Routes/Comments/AddComment.php | 4 +- src/Routes/Comments/DeleteComment.php | 4 +- src/Routes/Comments/EditComment.php | 4 +- src/Routes/Comments/GetComments.php | 2 +- src/Routes/Comments/GetPostComments.php | 4 +- src/Routes/Favorites/AddToFavorites.php | 4 +- src/Routes/Favorites/GetFavoriteUsers.php | 4 +- src/Routes/Favorites/RemoveFromFavorites.php | 4 +- src/Routes/GetGlobals.php | 2 +- src/Routes/GetHistory.php | 2 +- src/Routes/Login.php | 2 +- src/Routes/Posts/CreatePost.php | 2 +- src/Routes/Posts/DeletePost.php | 4 +- src/Routes/Posts/FeaturePost.php | 4 +- src/Routes/Posts/GetFeaturedPost.php | 2 +- src/Routes/Posts/GetPost.php | 4 +- src/Routes/Posts/GetPostContent.php | 4 +- src/Routes/Posts/GetPostThumbnail.php | 6 +- src/Routes/Posts/GetPosts.php | 2 +- src/Routes/Posts/Notes/AddPostNote.php | 4 +- src/Routes/Posts/Notes/DeletePostNote.php | 4 +- src/Routes/Posts/Notes/GetPostNotes.php | 4 +- src/Routes/Posts/Notes/UpdatePostNote.php | 4 +- src/Routes/Posts/UpdatePost.php | 4 +- src/Routes/Scores/GetCommentScore.php | 4 +- src/Routes/Scores/GetPostScore.php | 4 +- src/Routes/Scores/SetCommentScore.php | 4 +- src/Routes/Scores/SetPostScore.php | 4 +- src/Routes/Tags/DeleteTag.php | 4 +- src/Routes/Tags/GetTag.php | 4 +- src/Routes/Tags/GetTagSiblings.php | 4 +- src/Routes/Tags/GetTags.php | 2 +- src/Routes/Tags/MergeTags.php | 4 +- src/Routes/Tags/UpdateTag.php | 4 +- src/Routes/Users/ActivateAccount.php | 4 +- src/Routes/Users/CreateUser.php | 2 +- src/Routes/Users/DeleteUser.php | 4 +- src/Routes/Users/FinishActivation.php | 4 +- src/Routes/Users/FinishPasswordReset.php | 4 +- src/Routes/Users/GetUser.php | 4 +- src/Routes/Users/GetUserAvatar.php | 6 +- src/Routes/Users/GetUsers.php | 2 +- src/Routes/Users/PasswordReset.php | 4 +- src/Routes/Users/UpdateUser.php | 4 +- tests/RouterTest.php | 24 +++----- 48 files changed, 115 insertions(+), 190 deletions(-) delete mode 100644 src/Route.php diff --git a/src/Route.php b/src/Route.php deleted file mode 100644 index 78b3f3ca..00000000 --- a/src/Route.php +++ /dev/null @@ -1,62 +0,0 @@ -query = $query; - $this->route = $route; - $this->regex = $this->getRegex(); - } - - public function handle($query, &$output) - { - $query = trim($query, '/'); - if (!preg_match($this->regex, $query, $matches)) - return false; - $routeArguments = $this->getRouteArguments($matches); - - $func = $this->route; - if (is_array($this->route) && $this->route[1] === 'work') - { - foreach ($matches as $key => $value) - $this->route[0]->setArgument($key, $value); - $output = $func(); - } - else - { - $output = $func(...array_values($routeArguments)); - } - - return true; - } - - private function getRegex() - { - $quotedQuery = preg_quote(trim($this->query, '/'), '/'); - return '/^' . preg_replace('/\\\?\:([a-zA-Z_-]*)/', '(?P<\1>[^\/]+)', $quotedQuery) . '$/i'; - } - - private function getRouteArguments($matches) - { - $reflectionFunction = is_array($this->route) - ? new \ReflectionMethod($this->route[0], $this->route[1]) - : new \ReflectionFunction($this->route); - $arguments = []; - foreach ($reflectionFunction->getParameters() as $reflectionParameter) - { - $key = $reflectionParameter->name; - if (isset($matches[$key])) - $arguments[$key] = $matches[$key]; - elseif ($reflectionParameter->isDefaultValueAvailable()) - $arguments[$key] = $reflectionParameter->getDefaultValue(); - else - $arguments[$key] = null; - } - return $arguments; - } -} diff --git a/src/Router.php b/src/Router.php index 8c6f94a6..7fb5a620 100644 --- a/src/Router.php +++ b/src/Router.php @@ -5,29 +5,24 @@ class Router { private $routes; - public function get($query, callable $route) + public function get($url, callable $function) { - $this->route('GET', $query, $route); + $this->inject('GET', $url, $function); } - public function put($query, callable $route) + public function post($url, callable $function) { - $this->route('PUT', $query, $route); + $this->inject('POST', $url, $function); } - public function delete($query, callable $route) + public function put($url, callable $function) { - $this->route('DELETE', $query, $route); + $this->inject('PUT', $url, $function); } - public function post($query, callable $route) + public function delete($url, callable $function) { - $this->route('POST', $query, $route); - } - - private function route($method, $query, callable $route) - { - $this->routes[$method][] = new Route($query, $route); + $this->inject('DELETE', $url, $function); } public function handle($method, $request) @@ -35,14 +30,28 @@ class Router if (!isset($this->routes[$method])) throw new \DomainException('Unhandled request method: ' . $method); - foreach ($this->routes[$method] as $route) + $request = trim($request, '/'); + foreach ($this->routes[$method] as $url => $callback) { - if ($route->handle($request, $output)) - { - return $output; - } + if (!preg_match(self::getRegex($url), $request, $matches)) + continue; + + return $callback($matches); } throw new \DomainException('Unhandled request address: ' . $request); } + + private function inject($method, $url, callable $function) + { + if (!isset($this->routes[$method])) + $this->routes[$method] = []; + $this->routes[$method][$url] = $function; + } + + private static function getRegex($url) + { + $quotedQuery = preg_quote(trim($url, '/'), '/'); + return '/^' . preg_replace('/\\\?\:([a-zA-Z_-]*)/', '(?P<\1>[^\/]+)', $quotedQuery) . '$/i'; + } } diff --git a/src/Routes/AbstractRoute.php b/src/Routes/AbstractRoute.php index 03db8ac8..33a87edc 100644 --- a/src/Routes/AbstractRoute.php +++ b/src/Routes/AbstractRoute.php @@ -3,21 +3,9 @@ namespace Szurubooru\Routes; abstract class AbstractRoute { - protected $arguments = []; - public abstract function getMethods(); public abstract function getUrl(); - public abstract function work(); - - public function setArgument($argName, $argValue) - { - $this->arguments[$argName] = $argValue; - } - - protected function getArgument($argName) - { - return $this->arguments[$argName]; - } + public abstract function work($args); } diff --git a/src/Routes/Comments/AddComment.php b/src/Routes/Comments/AddComment.php index 460e9a5b..99c38922 100644 --- a/src/Routes/Comments/AddComment.php +++ b/src/Routes/Comments/AddComment.php @@ -43,11 +43,11 @@ class AddComment extends AbstractCommentRoute return '/api/comments/:postNameOrId'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::ADD_COMMENTS); - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $comment = $this->commentService->createComment($post, $this->inputReader->text); return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); } diff --git a/src/Routes/Comments/DeleteComment.php b/src/Routes/Comments/DeleteComment.php index 815ee3ef..70aa2d48 100644 --- a/src/Routes/Comments/DeleteComment.php +++ b/src/Routes/Comments/DeleteComment.php @@ -43,9 +43,9 @@ class DeleteComment extends AbstractCommentRoute return '/api/comments/:commentId'; } - public function work() + public function work($args) { - $comment = $this->commentService->getById($this->getArgument('commentId')); + $comment = $this->commentService->getById($args['commentId']); $this->privilegeService->assertPrivilege( $this->privilegeService->isLoggedIn($comment->getUser()) diff --git a/src/Routes/Comments/EditComment.php b/src/Routes/Comments/EditComment.php index 2f1d2897..f2a0b91d 100644 --- a/src/Routes/Comments/EditComment.php +++ b/src/Routes/Comments/EditComment.php @@ -43,9 +43,9 @@ class EditComment extends AbstractCommentRoute return '/api/comments/:commentId'; } - public function work() + public function work($args) { - $comment = $this->commentService->getById($this->getArgument('commentId')); + $comment = $this->commentService->getById($args['commentId']); $this->privilegeService->assertPrivilege( ($comment->getUser() && $this->privilegeService->isLoggedIn($comment->getUser())) diff --git a/src/Routes/Comments/GetComments.php b/src/Routes/Comments/GetComments.php index e0390d32..441f0355 100644 --- a/src/Routes/Comments/GetComments.php +++ b/src/Routes/Comments/GetComments.php @@ -46,7 +46,7 @@ class GetComments extends AbstractCommentRoute return '/api/comments'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); diff --git a/src/Routes/Comments/GetPostComments.php b/src/Routes/Comments/GetPostComments.php index 7a20d478..1fd15f44 100644 --- a/src/Routes/Comments/GetPostComments.php +++ b/src/Routes/Comments/GetPostComments.php @@ -46,10 +46,10 @@ class GetPostComments extends AbstractCommentRoute return '/api/comments/:postNameOrId'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $filter = new CommentFilter(); $filter->setOrder([ diff --git a/src/Routes/Favorites/AddToFavorites.php b/src/Routes/Favorites/AddToFavorites.php index e1ecc1cd..8272be2f 100644 --- a/src/Routes/Favorites/AddToFavorites.php +++ b/src/Routes/Favorites/AddToFavorites.php @@ -39,11 +39,11 @@ class AddToFavorites extends AbstractRoute return '/api/posts/:postNameOrId/favorites'; } - public function work() + public function work($args) { $this->privilegeService->assertLoggedIn(); $user = $this->authService->getLoggedInUser(); - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $this->favoritesService->addFavorite($user, $post); $users = $this->favoritesService->getFavoriteUsers($post); diff --git a/src/Routes/Favorites/GetFavoriteUsers.php b/src/Routes/Favorites/GetFavoriteUsers.php index 0c99a66e..bec5eac4 100644 --- a/src/Routes/Favorites/GetFavoriteUsers.php +++ b/src/Routes/Favorites/GetFavoriteUsers.php @@ -39,9 +39,9 @@ class GetFavoriteUsers extends AbstractRoute return '/api/posts/:postNameOrId/favorites'; } - public function work() + public function work($args) { - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $users = $this->favoritesService->getFavoriteUsers($post); return ['data' => $this->userViewProxy->fromArray($users)]; } diff --git a/src/Routes/Favorites/RemoveFromFavorites.php b/src/Routes/Favorites/RemoveFromFavorites.php index b96037ea..8196ccfe 100644 --- a/src/Routes/Favorites/RemoveFromFavorites.php +++ b/src/Routes/Favorites/RemoveFromFavorites.php @@ -39,11 +39,11 @@ class RemoveFromFavorites extends AbstractRoute return '/api/posts/:postNameOrId/favorites'; } - public function work() + public function work($args) { $this->privilegeService->assertLoggedIn(); $user = $this->authService->getLoggedInUser(); - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $this->favoritesService->deleteFavorite($user, $post); $users = $this->favoritesService->getFavoriteUsers($post); diff --git a/src/Routes/GetGlobals.php b/src/Routes/GetGlobals.php index a721833c..6afe84c4 100644 --- a/src/Routes/GetGlobals.php +++ b/src/Routes/GetGlobals.php @@ -21,7 +21,7 @@ class GetGlobals extends AbstractRoute return '/api/globals'; } - public function work() + public function work($args) { $globals = $this->globalParamDao->findAll(); $result = []; diff --git a/src/Routes/GetHistory.php b/src/Routes/GetHistory.php index 9f205339..0356b293 100644 --- a/src/Routes/GetHistory.php +++ b/src/Routes/GetHistory.php @@ -40,7 +40,7 @@ class GetHistory extends AbstractRoute return '/api/history'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY); diff --git a/src/Routes/Login.php b/src/Routes/Login.php index 4b24b9c2..28b0b606 100644 --- a/src/Routes/Login.php +++ b/src/Routes/Login.php @@ -48,7 +48,7 @@ class Login extends AbstractRoute return '/api/login'; } - public function work() + public function work($args) { if (isset($this->inputReader->userNameOrEmail) && isset($this->inputReader->password)) { diff --git a/src/Routes/Posts/CreatePost.php b/src/Routes/Posts/CreatePost.php index 88036ad6..bdea7351 100644 --- a/src/Routes/Posts/CreatePost.php +++ b/src/Routes/Posts/CreatePost.php @@ -36,7 +36,7 @@ class CreatePost extends AbstractPostRoute return '/api/posts'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS); $formData = new UploadFormData($this->inputReader); diff --git a/src/Routes/Posts/DeletePost.php b/src/Routes/Posts/DeletePost.php index 5d8f940f..24084f74 100644 --- a/src/Routes/Posts/DeletePost.php +++ b/src/Routes/Posts/DeletePost.php @@ -27,11 +27,11 @@ class DeletePost extends AbstractPostRoute return '/api/posts/:postNameOrId'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::DELETE_POSTS); - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $this->postService->deletePost($post); } } diff --git a/src/Routes/Posts/FeaturePost.php b/src/Routes/Posts/FeaturePost.php index 5eb8c902..bb84e1c2 100644 --- a/src/Routes/Posts/FeaturePost.php +++ b/src/Routes/Posts/FeaturePost.php @@ -31,11 +31,11 @@ class FeaturePost extends AbstractPostRoute return '/api/posts/:postNameOrId/feature'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::FEATURE_POSTS); - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $this->postFeatureService->featurePost($post); } } diff --git a/src/Routes/Posts/GetFeaturedPost.php b/src/Routes/Posts/GetFeaturedPost.php index 8bb8371d..35c2575e 100644 --- a/src/Routes/Posts/GetFeaturedPost.php +++ b/src/Routes/Posts/GetFeaturedPost.php @@ -30,7 +30,7 @@ class GetFeaturedPost extends AbstractPostRoute return '/api/posts/featured'; } - public function work() + public function work($args) { $post = $this->postFeatureService->getFeaturedPost(); $user = $this->postFeatureService->getFeaturedPostUser(); diff --git a/src/Routes/Posts/GetPost.php b/src/Routes/Posts/GetPost.php index 722817a4..1928ec7c 100644 --- a/src/Routes/Posts/GetPost.php +++ b/src/Routes/Posts/GetPost.php @@ -31,11 +31,11 @@ class GetPost extends AbstractPostRoute return '/api/posts/:postNameOrId'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS); - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); } } diff --git a/src/Routes/Posts/GetPostContent.php b/src/Routes/Posts/GetPostContent.php index 6e655871..8b06a5c2 100644 --- a/src/Routes/Posts/GetPostContent.php +++ b/src/Routes/Posts/GetPostContent.php @@ -36,9 +36,9 @@ class GetPostContent extends AbstractPostRoute return '/api/posts/:postName/content'; } - public function work() + public function work($args) { - $post = $this->postService->getByName($this->getArgument('postName')); + $post = $this->postService->getByName($args['postName']); $customFileName = sprintf('%s_%s.%s', $this->config->basic->serviceName, diff --git a/src/Routes/Posts/GetPostThumbnail.php b/src/Routes/Posts/GetPostThumbnail.php index e6069b28..bf3e8149 100644 --- a/src/Routes/Posts/GetPostThumbnail.php +++ b/src/Routes/Posts/GetPostThumbnail.php @@ -39,10 +39,10 @@ class GetPostThumbnail extends AbstractPostRoute return '/api/posts/:postName/thumbnail/:size'; } - public function work() + public function work($args) { - $size = $this->getArgument('size'); - $post = $this->postService->getByName($this->getArgument('postName')); + $size = $args['size']; + $post = $this->postService->getByName($args['postName']); $thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size); $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); } diff --git a/src/Routes/Posts/GetPosts.php b/src/Routes/Posts/GetPosts.php index d6b91bc9..1e886c00 100644 --- a/src/Routes/Posts/GetPosts.php +++ b/src/Routes/Posts/GetPosts.php @@ -43,7 +43,7 @@ class GetPosts extends AbstractPostRoute return '/api/posts'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::LIST_POSTS); diff --git a/src/Routes/Posts/Notes/AddPostNote.php b/src/Routes/Posts/Notes/AddPostNote.php index 07d4e42c..4ffdad2c 100644 --- a/src/Routes/Posts/Notes/AddPostNote.php +++ b/src/Routes/Posts/Notes/AddPostNote.php @@ -41,9 +41,9 @@ class AddPostNote extends AbstractPostRoute return '/api/notes/:postNameOrId'; } - public function work() + public function work($args) { - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $this->privilegeService->assertPrivilege(Privilege::ADD_POST_NOTES); diff --git a/src/Routes/Posts/Notes/DeletePostNote.php b/src/Routes/Posts/Notes/DeletePostNote.php index 04408b41..8667fc89 100644 --- a/src/Routes/Posts/Notes/DeletePostNote.php +++ b/src/Routes/Posts/Notes/DeletePostNote.php @@ -28,9 +28,9 @@ class DeletePostNote extends AbstractPostRoute return '/api/notes/:postNoteId'; } - public function work() + public function work($args) { - $postNote = $this->postNotesService->getById($this->getArgument('postNoteId')); + $postNote = $this->postNotesService->getById($args['postNoteId']); $this->privilegeService->assertPrivilege(Privilege::DELETE_POST_NOTES); return $this->postNotesService->deletePostNote($postNote); } diff --git a/src/Routes/Posts/Notes/GetPostNotes.php b/src/Routes/Posts/Notes/GetPostNotes.php index 929dd75a..e2849651 100644 --- a/src/Routes/Posts/Notes/GetPostNotes.php +++ b/src/Routes/Posts/Notes/GetPostNotes.php @@ -40,9 +40,9 @@ class GetPostNotes extends AbstractPostRoute return '/api/notes/:postNameOrId'; } - public function work() + public function work($args) { - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); $postNotes = $this->postNotesService->getByPost($post); return $this->postNoteViewProxy->fromArray($postNotes); } diff --git a/src/Routes/Posts/Notes/UpdatePostNote.php b/src/Routes/Posts/Notes/UpdatePostNote.php index c4468ae0..09d68b6c 100644 --- a/src/Routes/Posts/Notes/UpdatePostNote.php +++ b/src/Routes/Posts/Notes/UpdatePostNote.php @@ -37,9 +37,9 @@ class UpdatePostNote extends AbstractPostRoute return '/api/notes/:postNoteId'; } - public function work() + public function work($args) { - $postNote = $this->postNotesService->getById($this->getArgument('postNoteId')); + $postNote = $this->postNotesService->getById($args['postNoteId']); $this->privilegeService->assertPrivilege(Privilege::EDIT_POST_NOTES); diff --git a/src/Routes/Posts/UpdatePost.php b/src/Routes/Posts/UpdatePost.php index 9f2f21da..6514f387 100644 --- a/src/Routes/Posts/UpdatePost.php +++ b/src/Routes/Posts/UpdatePost.php @@ -36,9 +36,9 @@ class UpdatePost extends AbstractPostRoute return '/api/posts/:postNameOrId'; } - public function work() + public function work($args) { - $postNameOrId = $this->getArgument('postNameOrId'); + $postNameOrId = $args['postNameOrId']; $post = $this->postService->getByNameOrId($postNameOrId); $formData = new PostEditFormData($this->inputReader); diff --git a/src/Routes/Scores/GetCommentScore.php b/src/Routes/Scores/GetCommentScore.php index 50d87ea3..58b2325f 100644 --- a/src/Routes/Scores/GetCommentScore.php +++ b/src/Routes/Scores/GetCommentScore.php @@ -37,9 +37,9 @@ class GetCommentScore extends AbstractScoreRoute return '/api/comments/:commentId/score'; } - public function work() + public function work($args) { - $comment = $this->commentService->getById($this->getArgument('commentId')); + $comment = $this->commentService->getById($args['commentId']); return $this->getScore($comment); } } diff --git a/src/Routes/Scores/GetPostScore.php b/src/Routes/Scores/GetPostScore.php index f56ea993..fbb15130 100644 --- a/src/Routes/Scores/GetPostScore.php +++ b/src/Routes/Scores/GetPostScore.php @@ -37,9 +37,9 @@ class GetPostScore extends AbstractScoreRoute return '/api/posts/:postNameOrId/score'; } - public function work() + public function work($args) { - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); return $this->getScore($post); } } diff --git a/src/Routes/Scores/SetCommentScore.php b/src/Routes/Scores/SetCommentScore.php index 91597e6c..70355748 100644 --- a/src/Routes/Scores/SetCommentScore.php +++ b/src/Routes/Scores/SetCommentScore.php @@ -37,9 +37,9 @@ class SetCommentScore extends AbstractScoreRoute return '/api/comments/:commentId/score'; } - public function work() + public function work($args) { - $comment = $this->commentService->getById($this->getArgument('commentId')); + $comment = $this->commentService->getById($args['commentId']); return $this->setScore($comment); } } diff --git a/src/Routes/Scores/SetPostScore.php b/src/Routes/Scores/SetPostScore.php index 1256369e..1b5a8a65 100644 --- a/src/Routes/Scores/SetPostScore.php +++ b/src/Routes/Scores/SetPostScore.php @@ -37,9 +37,9 @@ class SetPostScore extends AbstractScoreRoute return '/api/posts/:postNameOrId/score'; } - public function work() + public function work($args) { - $post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); + $post = $this->postService->getByNameOrId($args['postNameOrId']); return $this->setScore($post); } } diff --git a/src/Routes/Tags/DeleteTag.php b/src/Routes/Tags/DeleteTag.php index 14fea75d..786189c7 100644 --- a/src/Routes/Tags/DeleteTag.php +++ b/src/Routes/Tags/DeleteTag.php @@ -27,9 +27,9 @@ class DeleteTag extends AbstractTagRoute return '/api/tags/:tagName'; } - public function work() + public function work($args) { - $tag = $this->tagService->getByName($this->getArgument('tagName')); + $tag = $this->tagService->getByName($args['tagName']); $this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS); return $this->tagService->deleteTag($tag); } diff --git a/src/Routes/Tags/GetTag.php b/src/Routes/Tags/GetTag.php index 661f4385..3d0a3a84 100644 --- a/src/Routes/Tags/GetTag.php +++ b/src/Routes/Tags/GetTag.php @@ -31,11 +31,11 @@ class GetTag extends AbstractTagRoute return '/api/tags/:tagName'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); - $tag = $this->tagService->getByName($this->getArgument('tagName')); + $tag = $this->tagService->getByName($args['tagName']); return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig()); } } diff --git a/src/Routes/Tags/GetTagSiblings.php b/src/Routes/Tags/GetTagSiblings.php index 36043de5..3f4ac8ff 100644 --- a/src/Routes/Tags/GetTagSiblings.php +++ b/src/Routes/Tags/GetTagSiblings.php @@ -31,9 +31,9 @@ class GetTagSiblings extends AbstractTagRoute return '/api/tags/:tagName/siblings'; } - public function work() + public function work($args) { - $tagName = $this->getArgument('tagName'); + $tagName = $args['tagName']; $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); $tag = $this->tagService->getByName($tagName); $result = $this->tagService->getSiblings($tagName); diff --git a/src/Routes/Tags/GetTags.php b/src/Routes/Tags/GetTags.php index ce162c0d..5d000b52 100644 --- a/src/Routes/Tags/GetTags.php +++ b/src/Routes/Tags/GetTags.php @@ -39,7 +39,7 @@ class GetTags extends AbstractTagRoute return '/api/tags'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); diff --git a/src/Routes/Tags/MergeTags.php b/src/Routes/Tags/MergeTags.php index 13411a53..342ade9b 100644 --- a/src/Routes/Tags/MergeTags.php +++ b/src/Routes/Tags/MergeTags.php @@ -31,9 +31,9 @@ class MergeTags extends AbstractTagRoute return '/api/tags/:tagName/merge'; } - public function work() + public function work($args) { - $tagName = $this->getArgument('tagName'); + $tagName = $args['tagName']; $targetTagName = $this->inputReader->targetTag; $sourceTag = $this->tagService->getByName($tagName); $targetTag = $this->tagService->getByName($targetTagName); diff --git a/src/Routes/Tags/UpdateTag.php b/src/Routes/Tags/UpdateTag.php index 15c80c3c..f66f6c38 100644 --- a/src/Routes/Tags/UpdateTag.php +++ b/src/Routes/Tags/UpdateTag.php @@ -36,9 +36,9 @@ class UpdateTag extends AbstractTagRoute return '/api/tags/:tagName'; } - public function work() + public function work($args) { - $tag = $this->tagService->getByName($this->getArgument('tagName')); + $tag = $this->tagService->getByName($args['tagName']); $formData = new TagEditFormData($this->inputReader); if ($formData->name !== null) diff --git a/src/Routes/Users/ActivateAccount.php b/src/Routes/Users/ActivateAccount.php index bae1aa7e..5209728c 100644 --- a/src/Routes/Users/ActivateAccount.php +++ b/src/Routes/Users/ActivateAccount.php @@ -21,9 +21,9 @@ class ActivateAccount extends AbstractUserRoute return '/api/activation/:userNameOrEmail'; } - public function work() + public function work($args) { - $user = $this->userService->getByNameOrEmail($this->getArgument('userNameOrEmail'), true); + $user = $this->userService->getByNameOrEmail($args['userNameOrEmail'], true); return $this->userService->sendActivationEmail($user); } } diff --git a/src/Routes/Users/CreateUser.php b/src/Routes/Users/CreateUser.php index 62b2f5b9..64a54ab5 100644 --- a/src/Routes/Users/CreateUser.php +++ b/src/Routes/Users/CreateUser.php @@ -36,7 +36,7 @@ class CreateUser extends AbstractUserRoute return '/api/users'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::REGISTER); $formData = new RegistrationFormData($this->inputReader); diff --git a/src/Routes/Users/DeleteUser.php b/src/Routes/Users/DeleteUser.php index 5b5438f1..8b2985e2 100644 --- a/src/Routes/Users/DeleteUser.php +++ b/src/Routes/Users/DeleteUser.php @@ -28,9 +28,9 @@ class DeleteUser extends AbstractUserRoute return '/api/users/:userNameOrEmail'; } - public function work() + public function work($args) { - $userNameOrEmail = $this->getArgument('userNameOrEmail'); + $userNameOrEmail = $args['userNameOrEmail']; $this->privilegeService->assertPrivilege( $this->privilegeService->isLoggedIn($userNameOrEmail) diff --git a/src/Routes/Users/FinishActivation.php b/src/Routes/Users/FinishActivation.php index 8946cf10..79a6a3ec 100644 --- a/src/Routes/Users/FinishActivation.php +++ b/src/Routes/Users/FinishActivation.php @@ -26,9 +26,9 @@ class FinishActivation extends AbstractUserRoute return '/api/finish-activation/:tokenName'; } - public function work() + public function work($args) { - $token = $this->tokenService->getByName($this->getArgument('tokenName')); + $token = $this->tokenService->getByName($args['tokenName']); $this->userService->finishActivation($token); } } diff --git a/src/Routes/Users/FinishPasswordReset.php b/src/Routes/Users/FinishPasswordReset.php index c4ee8cb5..9280b7b5 100644 --- a/src/Routes/Users/FinishPasswordReset.php +++ b/src/Routes/Users/FinishPasswordReset.php @@ -26,9 +26,9 @@ class FinishPasswordReset extends AbstractUserRoute return '/api/finish-password-reset/:tokenName'; } - public function work() + public function work($args) { - $token = $this->tokenService->getByName($this->getArgument('tokenName')); + $token = $this->tokenService->getByName($args['tokenName']); return ['newPassword' => $this->userService->finishPasswordReset($token)]; } } diff --git a/src/Routes/Users/GetUser.php b/src/Routes/Users/GetUser.php index b31f9977..ed715aaa 100644 --- a/src/Routes/Users/GetUser.php +++ b/src/Routes/Users/GetUser.php @@ -35,9 +35,9 @@ class GetUser extends AbstractUserRoute return '/api/users/:userNameOrEmail'; } - public function work() + public function work($args) { - $userNameOrEmail = $this->getArgument('userNameOrEmail'); + $userNameOrEmail = $args['userNameOrEmail']; if (!$this->privilegeService->isLoggedIn($userNameOrEmail)) $this->privilegeService->assertPrivilege(Privilege::VIEW_USERS); $user = $this->userService->getByNameOrEmail($userNameOrEmail); diff --git a/src/Routes/Users/GetUserAvatar.php b/src/Routes/Users/GetUserAvatar.php index 42c363fb..41d18d76 100644 --- a/src/Routes/Users/GetUserAvatar.php +++ b/src/Routes/Users/GetUserAvatar.php @@ -36,10 +36,10 @@ class GetUserAvatar extends AbstractUserRoute return '/api/users/:userName/avatar/:size'; } - public function work() + public function work($args) { - $userName = $this->getArgument('userName'); - $size = $this->getArgument('size'); + $userName = $args['userName']; + $size = $args['size']; try { diff --git a/src/Routes/Users/GetUsers.php b/src/Routes/Users/GetUsers.php index 00f3dad8..85fc6805 100644 --- a/src/Routes/Users/GetUsers.php +++ b/src/Routes/Users/GetUsers.php @@ -43,7 +43,7 @@ class GetUsers extends AbstractUserRoute return '/api/users'; } - public function work() + public function work($args) { $this->privilegeService->assertPrivilege(Privilege::LIST_USERS); diff --git a/src/Routes/Users/PasswordReset.php b/src/Routes/Users/PasswordReset.php index 70c4df27..866ed808 100644 --- a/src/Routes/Users/PasswordReset.php +++ b/src/Routes/Users/PasswordReset.php @@ -19,9 +19,9 @@ class PasswordReset extends AbstractUserRoute return '/api/password-reset/:userNameOrEmail'; } - public function work() + public function work($args) { - $user = $this->userService->getByNameOrEmail($this->getArgument('userNameOrEmail')); + $user = $this->userService->getByNameOrEmail($args['userNameOrEmail']); return $this->userService->sendPasswordResetEmail($user); } } diff --git a/src/Routes/Users/UpdateUser.php b/src/Routes/Users/UpdateUser.php index ec340e9f..dfa8828e 100644 --- a/src/Routes/Users/UpdateUser.php +++ b/src/Routes/Users/UpdateUser.php @@ -36,9 +36,9 @@ class UpdateUser extends AbstractUserRoute return '/api/users/:userNameOrEmail'; } - public function work() + public function work($args) { - $userNameOrEmail = $this->getArgument('userNameOrEmail'); + $userNameOrEmail = $args['userNameOrEmail']; $user = $this->userService->getByNameOrEmail($userNameOrEmail); $formData = new UserEditFormData($this->inputReader); diff --git a/tests/RouterTest.php b/tests/RouterTest.php index fbef5edc..5e0b3a3e 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -54,7 +54,8 @@ final class PostDaoTest extends AbstractTestCase { $router = new Router; $testOk = false; - $router->get('/tests/:id', function($id) use (&$testOk) { + $router->get('/tests/:id', function($args) use (&$testOk) { + extract($args); $this->assertEquals($id, 'test_id'); $testOk = true; }); @@ -66,7 +67,8 @@ final class PostDaoTest extends AbstractTestCase { $router = new Router; $testOk = false; - $router->get('/tests/:id/:page', function($id, $page) use (&$testOk) { + $router->get('/tests/:id/:page', function($args) use (&$testOk) { + extract($args); $this->assertEquals($id, 'test_id'); $this->assertEquals($page, 'test_page'); $testOk = true; }); @@ -79,22 +81,10 @@ final class PostDaoTest extends AbstractTestCase { $router = new Router; $testOk = false; - $router->get('/tests/:id', function($id, $page) use (&$testOk) { + $router->get('/tests/:id', function($args) use (&$testOk) { + extract($args); $this->assertEquals($id, 'test_id'); - $this->assertNull($page); - $testOk = true; }); - - $router->handle('GET', '/tests/test_id'); - $this->assertTrue($testOk); - } - - public function testMissingDefaultParameterHandling() - { - $router = new Router; - $testOk = false; - $router->get('/tests/:id', function($id, $page = 1) use (&$testOk) { - $this->assertEquals($id, 'test_id'); - $this->assertEquals(1, $page); + $this->assertFalse(isset($page)); $testOk = true; }); $router->handle('GET', '/tests/test_id');