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');