Simplified routing
This commit is contained in:
parent
da6b37b14c
commit
48230a64ad
48 changed files with 115 additions and 190 deletions
|
@ -1,62 +0,0 @@
|
|||
<?php
|
||||
namespace Szurubooru;
|
||||
|
||||
final class Route
|
||||
{
|
||||
public $query;
|
||||
public $route;
|
||||
|
||||
public function __construct($query, callable $route)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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()))
|
||||
|
|
|
@ -46,7 +46,7 @@ class GetComments extends AbstractCommentRoute
|
|||
return '/api/comments';
|
||||
}
|
||||
|
||||
public function work()
|
||||
public function work($args)
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS);
|
||||
|
||||
|
|
|
@ -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([
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)];
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -21,7 +21,7 @@ class GetGlobals extends AbstractRoute
|
|||
return '/api/globals';
|
||||
}
|
||||
|
||||
public function work()
|
||||
public function work($args)
|
||||
{
|
||||
$globals = $this->globalParamDao->findAll();
|
||||
$result = [];
|
||||
|
|
|
@ -40,7 +40,7 @@ class GetHistory extends AbstractRoute
|
|||
return '/api/history';
|
||||
}
|
||||
|
||||
public function work()
|
||||
public function work($args)
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY);
|
||||
|
||||
|
|
|
@ -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))
|
||||
{
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ class GetPosts extends AbstractPostRoute
|
|||
return '/api/posts';
|
||||
}
|
||||
|
||||
public function work()
|
||||
public function work($args)
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(Privilege::LIST_POSTS);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -39,7 +39,7 @@ class GetTags extends AbstractTagRoute
|
|||
return '/api/tags';
|
||||
}
|
||||
|
||||
public function work()
|
||||
public function work($args)
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(Privilege::LIST_TAGS);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
|
@ -43,7 +43,7 @@ class GetUsers extends AbstractUserRoute
|
|||
return '/api/users';
|
||||
}
|
||||
|
||||
public function work()
|
||||
public function work($args)
|
||||
{
|
||||
$this->privilegeService->assertPrivilege(Privilege::LIST_USERS);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue