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 @@ -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/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/Controllers/PostController.php b/src/Controllers/PostController.php deleted file mode 100644 index ae0a27eb..00000000 --- a/src/Controllers/PostController.php +++ /dev/null @@ -1,182 +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) - { - $this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS); - $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) - { - $this->privilegeService->assertPrivilege(Privilege::DELETE_POSTS); - $post = $this->postService->getByNameOrId($postNameOrId); - $this->postService->deletePost($post); - } - - public function featurePost($postNameOrId) - { - $this->privilegeService->assertPrivilege(Privilege::FEATURE_POSTS); - $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/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/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/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/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/Dispatcher.php b/src/Dispatcher.php index b785142b..c6431215 100644 --- a/src/Dispatcher.php +++ b/src/Dispatcher.php @@ -2,7 +2,6 @@ namespace Szurubooru; use Szurubooru\Bootstrap; use Szurubooru\Config; -use Szurubooru\ControllerRepository; use Szurubooru\DatabaseConnection; use Szurubooru\Helpers\HttpHelper; use Szurubooru\Router; @@ -24,7 +23,7 @@ final class Dispatcher HttpHelper $httpHelper, AuthService $authService, TokenService $tokenService, - ControllerRepository $controllerRepository) + RouteRepository $routeRepository) { $this->router = $router; $this->config = $config; @@ -36,8 +35,7 @@ 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); } public function run($requestMethod, $requestUri) diff --git a/src/Route.php b/src/Route.php deleted file mode 100644 index c71f7f4b..00000000 --- a/src/Route.php +++ /dev/null @@ -1,51 +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; - $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/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/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 new file mode 100644 index 00000000..33a87edc --- /dev/null +++ b/src/Routes/AbstractRoute.php @@ -0,0 +1,11 @@ + true, + ]; + } +} diff --git a/src/Routes/Comments/AddComment.php b/src/Routes/Comments/AddComment.php new file mode 100644 index 00000000..99c38922 --- /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($args) + { + $this->privilegeService->assertPrivilege(Privilege::ADD_COMMENTS); + + $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 new file mode 100644 index 00000000..70aa2d48 --- /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($args) + { + $comment = $this->commentService->getById($args['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..f2a0b91d --- /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($args) + { + $comment = $this->commentService->getById($args['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..441f0355 --- /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($args) + { + $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..1fd15f44 --- /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($args) + { + $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); + $post = $this->postService->getByNameOrId($args['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/Routes/Favorites/AddToFavorites.php b/src/Routes/Favorites/AddToFavorites.php new file mode 100644 index 00000000..8272be2f --- /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($args) + { + $this->privilegeService->assertLoggedIn(); + $user = $this->authService->getLoggedInUser(); + $post = $this->postService->getByNameOrId($args['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..bec5eac4 --- /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($args) + { + $post = $this->postService->getByNameOrId($args['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..8196ccfe 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($args) + { + $this->privilegeService->assertLoggedIn(); + $user = $this->authService->getLoggedInUser(); + $post = $this->postService->getByNameOrId($args['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/Routes/GetGlobals.php b/src/Routes/GetGlobals.php new file mode 100644 index 00000000..6afe84c4 --- /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($args) + { + $globals = $this->globalParamDao->findAll(); + $result = []; + foreach ($globals as $global) + { + $result[$global->getKey()] = $global->getValue(); + } + return $result; + } +} 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..0356b293 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($args) { $this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY); diff --git a/src/Controllers/AuthController.php b/src/Routes/Login.php similarity index 89% rename from src/Controllers/AuthController.php rename to src/Routes/Login.php index d8d90cff..28b0b606 100644 --- a/src/Controllers/AuthController.php +++ b/src/Routes/Login.php @@ -1,5 +1,5 @@ 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($args) { if (isset($this->inputReader->userNameOrEmail) && isset($this->inputReader->password)) { diff --git a/src/Routes/Posts/AbstractPostRoute.php b/src/Routes/Posts/AbstractPostRoute.php new file mode 100644 index 00000000..5fd627ba --- /dev/null +++ b/src/Routes/Posts/AbstractPostRoute.php @@ -0,0 +1,29 @@ + 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, + ]; + } + + protected 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..bdea7351 --- /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($args) + { + $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..24084f74 --- /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($args) + { + $this->privilegeService->assertPrivilege(Privilege::DELETE_POSTS); + + $post = $this->postService->getByNameOrId($args['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..bb84e1c2 --- /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($args) + { + $this->privilegeService->assertPrivilege(Privilege::FEATURE_POSTS); + + $post = $this->postService->getByNameOrId($args['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..35c2575e --- /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($args) + { + $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..1928ec7c --- /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($args) + { + $this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS); + + $post = $this->postService->getByNameOrId($args['postNameOrId']); + return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); + } +} 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..8b06a5c2 100644 --- a/src/Controllers/PostContentController.php +++ b/src/Routes/Posts/GetPostContent.php @@ -1,45 +1,44 @@ 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($args) + { + $post = $this->postService->getByName($args['postName']); $customFileName = sprintf('%s_%s.%s', $this->config->basic->serviceName, @@ -54,11 +53,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..bf3e8149 --- /dev/null +++ b/src/Routes/Posts/GetPostThumbnail.php @@ -0,0 +1,49 @@ +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($args) + { + $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 new file mode 100644 index 00000000..1e886c00 --- /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($args) + { + $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/Notes/AddPostNote.php b/src/Routes/Posts/Notes/AddPostNote.php new file mode 100644 index 00000000..4ffdad2c --- /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($args) + { + $post = $this->postService->getByNameOrId($args['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..8667fc89 --- /dev/null +++ b/src/Routes/Posts/Notes/DeletePostNote.php @@ -0,0 +1,37 @@ +postNotesService = $postNotesService; + $this->privilegeService = $privilegeService; + } + + public function getMethods() + { + return ['DELETE']; + } + + public function getUrl() + { + return '/api/notes/:postNoteId'; + } + + public function work($args) + { + $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 new file mode 100644 index 00000000..e2849651 --- /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($args) + { + $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 new file mode 100644 index 00000000..09d68b6c --- /dev/null +++ b/src/Routes/Posts/Notes/UpdatePostNote.php @@ -0,0 +1,50 @@ +inputReader = $inputReader; + $this->postNotesService = $postNotesService; + $this->privilegeService = $privilegeService; + $this->postNoteViewProxy = $postNoteViewProxy; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/notes/:postNoteId'; + } + + public function work($args) + { + $postNote = $this->postNotesService->getById($args['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/Routes/Posts/UpdatePost.php b/src/Routes/Posts/UpdatePost.php new file mode 100644 index 00000000..6514f387 --- /dev/null +++ b/src/Routes/Posts/UpdatePost.php @@ -0,0 +1,64 @@ +privilegeService = $privilegeService; + $this->postService = $postService; + $this->inputReader = $inputReader; + $this->postViewProxy = $postViewProxy; + } + + public function getMethods() + { + return ['PUT']; + } + + public function getUrl() + { + return '/api/posts/:postNameOrId'; + } + + public function work($args) + { + $postNameOrId = $args['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/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..58b2325f --- /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($args) + { + $comment = $this->commentService->getById($args['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..fbb15130 --- /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($args) + { + $post = $this->postService->getByNameOrId($args['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..70355748 --- /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($args) + { + $comment = $this->commentService->getById($args['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..1b5a8a65 --- /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($args) + { + $post = $this->postService->getByNameOrId($args['postNameOrId']); + return $this->setScore($post); + } +} 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..786189c7 --- /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($args) + { + $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 new file mode 100644 index 00000000..3d0a3a84 --- /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($args) + { + $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); + + $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 new file mode 100644 index 00000000..3f4ac8ff --- /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($args) + { + $tagName = $args['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..5d000b52 --- /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($args) + { + $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..342ade9b --- /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($args) + { + $tagName = $args['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..f66f6c38 --- /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($args) + { + $tag = $this->tagService->getByName($args['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/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($args) + { + $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 new file mode 100644 index 00000000..64a54ab5 --- /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($args) + { + $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..8b2985e2 --- /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($args) + { + $userNameOrEmail = $args['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..79a6a3ec --- /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($args) + { + $token = $this->tokenService->getByName($args['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..9280b7b5 --- /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($args) + { + $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 new file mode 100644 index 00000000..ed715aaa --- /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($args) + { + $userNameOrEmail = $args['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/Controllers/UserAvatarController.php b/src/Routes/Users/GetUserAvatar.php similarity index 86% rename from src/Controllers/UserAvatarController.php rename to src/Routes/Users/GetUserAvatar.php index 6cd9c4ae..41d18d76 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($args) + { + $userName = $args['userName']; + $size = $args['size']; + try { $user = $this->userService->getByName($userName); diff --git a/src/Routes/Users/GetUsers.php b/src/Routes/Users/GetUsers.php new file mode 100644 index 00000000..85fc6805 --- /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($args) + { + $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..866ed808 --- /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($args) + { + $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 new file mode 100644 index 00000000..dfa8828e --- /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($args) + { + $userNameOrEmail = $args['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 b43799a0..73ef2304 100644 --- a/src/di.php +++ b/src/di.php @@ -11,7 +11,7 @@ $publicDataDirectory = __DIR__ return [ \Szurubooru\Config::class => DI\object()->constructor($dataDirectory, $publicDataDirectory), - \Szurubooru\ControllerRepository::class => DI\object()->constructor(DI\link('controllers')), + \Szurubooru\RouteRepository::class => DI\object()->constructor(DI\link('routes')), \Szurubooru\Upgrades\UpgradeRepository::class => DI\object()->constructor(DI\link('upgrades')), 'upgrades' => DI\factory(function (DI\container $container) { @@ -54,20 +54,52 @@ return [ ]; }), - 'controllers' => DI\factory(function (DI\container $container) { + 'routes' => 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), - $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\FavoritesController::class), - $container->get(\Szurubooru\Controllers\ScoreController::class), - $container->get(\Szurubooru\Controllers\CommentController::class), - $container->get(\Szurubooru\Controllers\TagController::class), + $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), + $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), + $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), + $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), + $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), + $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\GetUserAvatar::class), + $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), ]; }), ]; 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 ea603433..63f0498c 100644 --- a/tests/DispatcherTest.php +++ b/tests/DispatcherTest.php @@ -1,8 +1,8 @@ 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); } @@ -38,7 +38,7 @@ 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(); $actual = $dispatcher->run('GET', '/'); @@ -54,7 +54,7 @@ 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(); $actual = $dispatcher->run('GET', '/'); @@ -67,7 +67,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 +82,6 @@ final class DispatcherTest extends AbstractDatabaseTestCase $this->httpHelperMock, $this->authServiceMock, $this->tokenServiceMock, - $this->controllerRepositoryMock); + $this->routeRepositoryMock); } } diff --git a/tests/RouteRepositoryTest.php b/tests/RouteRepositoryTest.php new file mode 100644 index 00000000..8d07208a --- /dev/null +++ b/tests/RouteRepositoryTest.php @@ -0,0 +1,61 @@ +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); + $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'); + } +} 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');