Simplified routing

This commit is contained in:
Marcin Kurczewski 2014-11-22 12:44:45 +01:00
parent da6b37b14c
commit 48230a64ad
48 changed files with 115 additions and 190 deletions

View file

@ -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;
}
}

View file

@ -5,29 +5,24 @@ class Router
{ {
private $routes; 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); $this->inject('DELETE', $url, $function);
}
private function route($method, $query, callable $route)
{
$this->routes[$method][] = new Route($query, $route);
} }
public function handle($method, $request) public function handle($method, $request)
@ -35,14 +30,28 @@ class Router
if (!isset($this->routes[$method])) if (!isset($this->routes[$method]))
throw new \DomainException('Unhandled request method: ' . $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)) if (!preg_match(self::getRegex($url), $request, $matches))
{ continue;
return $output;
} return $callback($matches);
} }
throw new \DomainException('Unhandled request address: ' . $request); 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';
}
} }

View file

@ -3,21 +3,9 @@ namespace Szurubooru\Routes;
abstract class AbstractRoute abstract class AbstractRoute
{ {
protected $arguments = [];
public abstract function getMethods(); public abstract function getMethods();
public abstract function getUrl(); public abstract function getUrl();
public abstract function work(); public abstract function work($args);
public function setArgument($argName, $argValue)
{
$this->arguments[$argName] = $argValue;
}
protected function getArgument($argName)
{
return $this->arguments[$argName];
}
} }

View file

@ -43,11 +43,11 @@ class AddComment extends AbstractCommentRoute
return '/api/comments/:postNameOrId'; return '/api/comments/:postNameOrId';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::ADD_COMMENTS); $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); $comment = $this->commentService->createComment($post, $this->inputReader->text);
return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig()); return $this->commentViewProxy->fromEntity($comment, $this->getCommentsFetchConfig());
} }

View file

@ -43,9 +43,9 @@ class DeleteComment extends AbstractCommentRoute
return '/api/comments/:commentId'; 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->assertPrivilege(
$this->privilegeService->isLoggedIn($comment->getUser()) $this->privilegeService->isLoggedIn($comment->getUser())

View file

@ -43,9 +43,9 @@ class EditComment extends AbstractCommentRoute
return '/api/comments/:commentId'; 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->assertPrivilege(
($comment->getUser() && $this->privilegeService->isLoggedIn($comment->getUser())) ($comment->getUser() && $this->privilegeService->isLoggedIn($comment->getUser()))

View file

@ -46,7 +46,7 @@ class GetComments extends AbstractCommentRoute
return '/api/comments'; return '/api/comments';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS);

View file

@ -46,10 +46,10 @@ class GetPostComments extends AbstractCommentRoute
return '/api/comments/:postNameOrId'; return '/api/comments/:postNameOrId';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS); $this->privilegeService->assertPrivilege(Privilege::LIST_COMMENTS);
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); $post = $this->postService->getByNameOrId($args['postNameOrId']);
$filter = new CommentFilter(); $filter = new CommentFilter();
$filter->setOrder([ $filter->setOrder([

View file

@ -39,11 +39,11 @@ class AddToFavorites extends AbstractRoute
return '/api/posts/:postNameOrId/favorites'; return '/api/posts/:postNameOrId/favorites';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertLoggedIn(); $this->privilegeService->assertLoggedIn();
$user = $this->authService->getLoggedInUser(); $user = $this->authService->getLoggedInUser();
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); $post = $this->postService->getByNameOrId($args['postNameOrId']);
$this->favoritesService->addFavorite($user, $post); $this->favoritesService->addFavorite($user, $post);
$users = $this->favoritesService->getFavoriteUsers($post); $users = $this->favoritesService->getFavoriteUsers($post);

View file

@ -39,9 +39,9 @@ class GetFavoriteUsers extends AbstractRoute
return '/api/posts/:postNameOrId/favorites'; 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); $users = $this->favoritesService->getFavoriteUsers($post);
return ['data' => $this->userViewProxy->fromArray($users)]; return ['data' => $this->userViewProxy->fromArray($users)];
} }

View file

@ -39,11 +39,11 @@ class RemoveFromFavorites extends AbstractRoute
return '/api/posts/:postNameOrId/favorites'; return '/api/posts/:postNameOrId/favorites';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertLoggedIn(); $this->privilegeService->assertLoggedIn();
$user = $this->authService->getLoggedInUser(); $user = $this->authService->getLoggedInUser();
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); $post = $this->postService->getByNameOrId($args['postNameOrId']);
$this->favoritesService->deleteFavorite($user, $post); $this->favoritesService->deleteFavorite($user, $post);
$users = $this->favoritesService->getFavoriteUsers($post); $users = $this->favoritesService->getFavoriteUsers($post);

View file

@ -21,7 +21,7 @@ class GetGlobals extends AbstractRoute
return '/api/globals'; return '/api/globals';
} }
public function work() public function work($args)
{ {
$globals = $this->globalParamDao->findAll(); $globals = $this->globalParamDao->findAll();
$result = []; $result = [];

View file

@ -40,7 +40,7 @@ class GetHistory extends AbstractRoute
return '/api/history'; return '/api/history';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY); $this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY);

View file

@ -48,7 +48,7 @@ class Login extends AbstractRoute
return '/api/login'; return '/api/login';
} }
public function work() public function work($args)
{ {
if (isset($this->inputReader->userNameOrEmail) && isset($this->inputReader->password)) if (isset($this->inputReader->userNameOrEmail) && isset($this->inputReader->password))
{ {

View file

@ -36,7 +36,7 @@ class CreatePost extends AbstractPostRoute
return '/api/posts'; return '/api/posts';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS); $this->privilegeService->assertPrivilege(Privilege::UPLOAD_POSTS);
$formData = new UploadFormData($this->inputReader); $formData = new UploadFormData($this->inputReader);

View file

@ -27,11 +27,11 @@ class DeletePost extends AbstractPostRoute
return '/api/posts/:postNameOrId'; return '/api/posts/:postNameOrId';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::DELETE_POSTS); $this->privilegeService->assertPrivilege(Privilege::DELETE_POSTS);
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); $post = $this->postService->getByNameOrId($args['postNameOrId']);
$this->postService->deletePost($post); $this->postService->deletePost($post);
} }
} }

View file

@ -31,11 +31,11 @@ class FeaturePost extends AbstractPostRoute
return '/api/posts/:postNameOrId/feature'; return '/api/posts/:postNameOrId/feature';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::FEATURE_POSTS); $this->privilegeService->assertPrivilege(Privilege::FEATURE_POSTS);
$post = $this->postService->getByNameOrId($this->getArgument('postNameOrId')); $post = $this->postService->getByNameOrId($args['postNameOrId']);
$this->postFeatureService->featurePost($post); $this->postFeatureService->featurePost($post);
} }
} }

View file

@ -30,7 +30,7 @@ class GetFeaturedPost extends AbstractPostRoute
return '/api/posts/featured'; return '/api/posts/featured';
} }
public function work() public function work($args)
{ {
$post = $this->postFeatureService->getFeaturedPost(); $post = $this->postFeatureService->getFeaturedPost();
$user = $this->postFeatureService->getFeaturedPostUser(); $user = $this->postFeatureService->getFeaturedPostUser();

View file

@ -31,11 +31,11 @@ class GetPost extends AbstractPostRoute
return '/api/posts/:postNameOrId'; return '/api/posts/:postNameOrId';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS); $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()); return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
} }
} }

View file

@ -36,9 +36,9 @@ class GetPostContent extends AbstractPostRoute
return '/api/posts/:postName/content'; 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', $customFileName = sprintf('%s_%s.%s',
$this->config->basic->serviceName, $this->config->basic->serviceName,

View file

@ -39,10 +39,10 @@ class GetPostThumbnail extends AbstractPostRoute
return '/api/posts/:postName/thumbnail/:size'; return '/api/posts/:postName/thumbnail/:size';
} }
public function work() public function work($args)
{ {
$size = $this->getArgument('size'); $size = $args['size'];
$post = $this->postService->getByName($this->getArgument('postName')); $post = $this->postService->getByName($args['postName']);
$thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size); $thumbnailName = $this->postThumbnailService->generateIfNeeded($post, $size, $size);
$this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName)); $this->networkingService->serveFile($this->fileDao->getFullPath($thumbnailName));
} }

View file

@ -43,7 +43,7 @@ class GetPosts extends AbstractPostRoute
return '/api/posts'; return '/api/posts';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::LIST_POSTS); $this->privilegeService->assertPrivilege(Privilege::LIST_POSTS);

View file

@ -41,9 +41,9 @@ class AddPostNote extends AbstractPostRoute
return '/api/notes/:postNameOrId'; 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); $this->privilegeService->assertPrivilege(Privilege::ADD_POST_NOTES);

View file

@ -28,9 +28,9 @@ class DeletePostNote extends AbstractPostRoute
return '/api/notes/:postNoteId'; 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); $this->privilegeService->assertPrivilege(Privilege::DELETE_POST_NOTES);
return $this->postNotesService->deletePostNote($postNote); return $this->postNotesService->deletePostNote($postNote);
} }

View file

@ -40,9 +40,9 @@ class GetPostNotes extends AbstractPostRoute
return '/api/notes/:postNameOrId'; 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); $postNotes = $this->postNotesService->getByPost($post);
return $this->postNoteViewProxy->fromArray($postNotes); return $this->postNoteViewProxy->fromArray($postNotes);
} }

View file

@ -37,9 +37,9 @@ class UpdatePostNote extends AbstractPostRoute
return '/api/notes/:postNoteId'; 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); $this->privilegeService->assertPrivilege(Privilege::EDIT_POST_NOTES);

View file

@ -36,9 +36,9 @@ class UpdatePost extends AbstractPostRoute
return '/api/posts/:postNameOrId'; return '/api/posts/:postNameOrId';
} }
public function work() public function work($args)
{ {
$postNameOrId = $this->getArgument('postNameOrId'); $postNameOrId = $args['postNameOrId'];
$post = $this->postService->getByNameOrId($postNameOrId); $post = $this->postService->getByNameOrId($postNameOrId);
$formData = new PostEditFormData($this->inputReader); $formData = new PostEditFormData($this->inputReader);

View file

@ -37,9 +37,9 @@ class GetCommentScore extends AbstractScoreRoute
return '/api/comments/:commentId/score'; 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); return $this->getScore($comment);
} }
} }

View file

@ -37,9 +37,9 @@ class GetPostScore extends AbstractScoreRoute
return '/api/posts/:postNameOrId/score'; 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); return $this->getScore($post);
} }
} }

View file

@ -37,9 +37,9 @@ class SetCommentScore extends AbstractScoreRoute
return '/api/comments/:commentId/score'; 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); return $this->setScore($comment);
} }
} }

View file

@ -37,9 +37,9 @@ class SetPostScore extends AbstractScoreRoute
return '/api/posts/:postNameOrId/score'; 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); return $this->setScore($post);
} }
} }

View file

@ -27,9 +27,9 @@ class DeleteTag extends AbstractTagRoute
return '/api/tags/:tagName'; 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); $this->privilegeService->assertPrivilege(Privilege::DELETE_TAGS);
return $this->tagService->deleteTag($tag); return $this->tagService->deleteTag($tag);
} }

View file

@ -31,11 +31,11 @@ class GetTag extends AbstractTagRoute
return '/api/tags/:tagName'; return '/api/tags/:tagName';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); $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()); return $this->tagViewProxy->fromEntity($tag, $this->getFullFetchConfig());
} }
} }

View file

@ -31,9 +31,9 @@ class GetTagSiblings extends AbstractTagRoute
return '/api/tags/:tagName/siblings'; 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); $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS);
$tag = $this->tagService->getByName($tagName); $tag = $this->tagService->getByName($tagName);
$result = $this->tagService->getSiblings($tagName); $result = $this->tagService->getSiblings($tagName);

View file

@ -39,7 +39,7 @@ class GetTags extends AbstractTagRoute
return '/api/tags'; return '/api/tags';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::LIST_TAGS); $this->privilegeService->assertPrivilege(Privilege::LIST_TAGS);

View file

@ -31,9 +31,9 @@ class MergeTags extends AbstractTagRoute
return '/api/tags/:tagName/merge'; return '/api/tags/:tagName/merge';
} }
public function work() public function work($args)
{ {
$tagName = $this->getArgument('tagName'); $tagName = $args['tagName'];
$targetTagName = $this->inputReader->targetTag; $targetTagName = $this->inputReader->targetTag;
$sourceTag = $this->tagService->getByName($tagName); $sourceTag = $this->tagService->getByName($tagName);
$targetTag = $this->tagService->getByName($targetTagName); $targetTag = $this->tagService->getByName($targetTagName);

View file

@ -36,9 +36,9 @@ class UpdateTag extends AbstractTagRoute
return '/api/tags/:tagName'; 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); $formData = new TagEditFormData($this->inputReader);
if ($formData->name !== null) if ($formData->name !== null)

View file

@ -21,9 +21,9 @@ class ActivateAccount extends AbstractUserRoute
return '/api/activation/:userNameOrEmail'; 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); return $this->userService->sendActivationEmail($user);
} }
} }

View file

@ -36,7 +36,7 @@ class CreateUser extends AbstractUserRoute
return '/api/users'; return '/api/users';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::REGISTER); $this->privilegeService->assertPrivilege(Privilege::REGISTER);
$formData = new RegistrationFormData($this->inputReader); $formData = new RegistrationFormData($this->inputReader);

View file

@ -28,9 +28,9 @@ class DeleteUser extends AbstractUserRoute
return '/api/users/:userNameOrEmail'; return '/api/users/:userNameOrEmail';
} }
public function work() public function work($args)
{ {
$userNameOrEmail = $this->getArgument('userNameOrEmail'); $userNameOrEmail = $args['userNameOrEmail'];
$this->privilegeService->assertPrivilege( $this->privilegeService->assertPrivilege(
$this->privilegeService->isLoggedIn($userNameOrEmail) $this->privilegeService->isLoggedIn($userNameOrEmail)

View file

@ -26,9 +26,9 @@ class FinishActivation extends AbstractUserRoute
return '/api/finish-activation/:tokenName'; 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); $this->userService->finishActivation($token);
} }
} }

View file

@ -26,9 +26,9 @@ class FinishPasswordReset extends AbstractUserRoute
return '/api/finish-password-reset/:tokenName'; 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)]; return ['newPassword' => $this->userService->finishPasswordReset($token)];
} }
} }

View file

@ -35,9 +35,9 @@ class GetUser extends AbstractUserRoute
return '/api/users/:userNameOrEmail'; return '/api/users/:userNameOrEmail';
} }
public function work() public function work($args)
{ {
$userNameOrEmail = $this->getArgument('userNameOrEmail'); $userNameOrEmail = $args['userNameOrEmail'];
if (!$this->privilegeService->isLoggedIn($userNameOrEmail)) if (!$this->privilegeService->isLoggedIn($userNameOrEmail))
$this->privilegeService->assertPrivilege(Privilege::VIEW_USERS); $this->privilegeService->assertPrivilege(Privilege::VIEW_USERS);
$user = $this->userService->getByNameOrEmail($userNameOrEmail); $user = $this->userService->getByNameOrEmail($userNameOrEmail);

View file

@ -36,10 +36,10 @@ class GetUserAvatar extends AbstractUserRoute
return '/api/users/:userName/avatar/:size'; return '/api/users/:userName/avatar/:size';
} }
public function work() public function work($args)
{ {
$userName = $this->getArgument('userName'); $userName = $args['userName'];
$size = $this->getArgument('size'); $size = $args['size'];
try try
{ {

View file

@ -43,7 +43,7 @@ class GetUsers extends AbstractUserRoute
return '/api/users'; return '/api/users';
} }
public function work() public function work($args)
{ {
$this->privilegeService->assertPrivilege(Privilege::LIST_USERS); $this->privilegeService->assertPrivilege(Privilege::LIST_USERS);

View file

@ -19,9 +19,9 @@ class PasswordReset extends AbstractUserRoute
return '/api/password-reset/:userNameOrEmail'; 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); return $this->userService->sendPasswordResetEmail($user);
} }
} }

View file

@ -36,9 +36,9 @@ class UpdateUser extends AbstractUserRoute
return '/api/users/:userNameOrEmail'; return '/api/users/:userNameOrEmail';
} }
public function work() public function work($args)
{ {
$userNameOrEmail = $this->getArgument('userNameOrEmail'); $userNameOrEmail = $args['userNameOrEmail'];
$user = $this->userService->getByNameOrEmail($userNameOrEmail); $user = $this->userService->getByNameOrEmail($userNameOrEmail);
$formData = new UserEditFormData($this->inputReader); $formData = new UserEditFormData($this->inputReader);

View file

@ -54,7 +54,8 @@ final class PostDaoTest extends AbstractTestCase
{ {
$router = new Router; $router = new Router;
$testOk = false; $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'); $this->assertEquals($id, 'test_id');
$testOk = true; }); $testOk = true; });
@ -66,7 +67,8 @@ final class PostDaoTest extends AbstractTestCase
{ {
$router = new Router; $router = new Router;
$testOk = false; $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($id, 'test_id');
$this->assertEquals($page, 'test_page'); $this->assertEquals($page, 'test_page');
$testOk = true; }); $testOk = true; });
@ -79,22 +81,10 @@ final class PostDaoTest extends AbstractTestCase
{ {
$router = new Router; $router = new Router;
$testOk = false; $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->assertEquals($id, 'test_id');
$this->assertNull($page); $this->assertFalse(isset($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);
$testOk = true; }); $testOk = true; });
$router->handle('GET', '/tests/test_id'); $router->handle('GET', '/tests/test_id');