Merge branch 'api'

This commit is contained in:
Marcin Kurczewski 2014-11-22 13:10:24 +01:00
commit 0b032e7f94
68 changed files with 2280 additions and 1049 deletions

View file

@ -1,17 +0,0 @@
<?php
namespace Szurubooru;
class ControllerRepository
{
private $controllers = [];
public function __construct(array $controllers)
{
$this->controllers = $controllers;
}
public function getControllers()
{
return $this->controllers;
}
}

View file

@ -1,8 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Router;
abstract class AbstractController
{
abstract function registerRoutes(Router $router);
}

View file

@ -1,155 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\SearchServices\Filters\CommentFilter;
use Szurubooru\SearchServices\Filters\PostFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementRangedValue;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
final class CommentController extends AbstractController
{
private $privilegeService;
private $authService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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,
];
}
}

View file

@ -1,30 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Router;
final class GlobalParamController extends AbstractController
{
private $globalParamDao;
public function __construct(GlobalParamDao $globalParamDao)
{
$this->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;
}
}

View file

@ -1,182 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Config;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Controllers\ViewProxies\SnapshotViewProxy;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Entities\Post;
use Szurubooru\FormData\PostEditFormData;
use Szurubooru\FormData\UploadFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\SearchServices\Parsers\PostSearchParser;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\PostFeatureService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
final class PostController extends AbstractController
{
private $config;
private $authService;
private $privilegeService;
private $postService;
private $postFeatureService;
private $postSearchParser;
private $inputReader;
private $postViewProxy;
private $snapshotViewProxy;
public function __construct(
Config $config,
AuthService $authService,
PrivilegeService $privilegeService,
PostService $postService,
PostFeatureService $postFeatureService,
PostSearchParser $postSearchParser,
InputReader $inputReader,
UserViewProxy $userViewProxy,
PostViewProxy $postViewProxy,
SnapshotViewProxy $snapshotViewProxy)
{
$this->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,
];
}
}

View file

@ -1,77 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Controllers\ViewProxies\PostNoteViewProxy;
use Szurubooru\FormData\PostNoteFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\Services\PostNotesService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
final class PostNotesController extends AbstractController
{
private $inputReader;
private $postService;
private $postNotesService;
private $privilegeService;
private $postNoteViewProxy;
public function __construct(
InputReader $inputReader,
PostService $postService,
PostNotesService $postNotesService,
PrivilegeService $privilegeService,
PostNoteViewProxy $postNoteViewProxy)
{
$this->inputReader = $inputReader;
$this->postService = $postService;
$this->postNotesService = $postNotesService;
$this->privilegeService = $privilegeService;
$this->postNoteViewProxy = $postNoteViewProxy;
}
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);
}
}

View file

@ -1,89 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Entities\Entity;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Router;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\ScoreService;
final class ScoreController extends AbstractController
{
private $privilegeService;
private $authService;
private $postService;
private $commentService;
private $scoreService;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
PostService $postService,
CommentService $commentService,
ScoreService $scoreService,
InputReader $inputReader)
{
$this->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),
];
}
}

View file

@ -1,127 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\FormData\TagEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\SearchServices\Parsers\TagSearchParser;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
final class TagController extends AbstractController
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
private $tagSearchParser;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy,
TagSearchParser $tagSearchParser,
InputReader $inputReader)
{
$this->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,
];
}
}

View file

@ -1,176 +0,0 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Config;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\FormData\RegistrationFormData;
use Szurubooru\FormData\UserEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\SearchServices\Parsers\UserSearchParser;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TokenService;
use Szurubooru\Services\UserService;
final class UserController extends AbstractController
{
private $config;
private $privilegeService;
private $userService;
private $tokenService;
private $userSearchParser;
private $inputReader;
private $userViewProxy;
public function __construct(
Config $config,
PrivilegeService $privilegeService,
UserService $userService,
TokenService $tokenService,
UserSearchParser $userSearchParser,
InputReader $inputReader,
UserViewProxy $userViewProxy)
{
$this->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);
}
}

View file

@ -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)

View file

@ -1,51 +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;
$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;
}
}

29
src/RouteRepository.php Normal file
View file

@ -0,0 +1,29 @@
<?php
namespace Szurubooru;
class RouteRepository
{
private $routes = [];
public function __construct(array $routes)
{
$this->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']);
}
}
}
}

View file

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

View file

@ -0,0 +1,11 @@
<?php
namespace Szurubooru\Routes;
abstract class AbstractRoute
{
public abstract function getMethods();
public abstract function getUrl();
public abstract function work($args);
}

View file

@ -0,0 +1,15 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
abstract class AbstractCommentRoute extends AbstractRoute
{
protected function getCommentsFetchConfig()
{
return
[
CommentViewProxy::FETCH_OWN_SCORE => true,
];
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class AddComment extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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());
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class DeleteComment extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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);
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class EditComment extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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());
}
}

View file

@ -0,0 +1,87 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Filters\PostFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementRangedValue;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetComments extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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()];
}
}

View file

@ -0,0 +1,68 @@
<?php
namespace Szurubooru\Routes\Comments;
use Szurubooru\Controllers\ViewProxies\CommentViewProxy;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Filters\CommentFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetPostComments extends AbstractCommentRoute
{
private $privilegeService;
private $postService;
private $commentService;
private $commentViewProxy;
private $postViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
CommentService $commentService,
CommentViewProxy $commentViewProxy,
PostViewProxy $postViewProxy,
InputReader $inputReader)
{
$this->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];
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Szurubooru\Routes\Favorites;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\FavoritesService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class AddToFavorites extends AbstractRoute
{
private $privilegeService;
private $authService;
private $postService;
private $favoritesService;
private $userViewProxy;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
PostService $postService,
FavoritesService $favoritesService,
UserViewProxy $userViewProxy)
{
$this->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)];
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Szurubooru\Routes\Favorites;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\FavoritesService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetFavoriteUsers extends AbstractRoute
{
private $privilegeService;
private $authService;
private $postService;
private $favoritesService;
private $userViewProxy;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
PostService $postService,
FavoritesService $favoritesService,
UserViewProxy $userViewProxy)
{
$this->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)];
}
}

View file

@ -1,13 +1,13 @@
<?php
namespace Szurubooru\Controllers;
namespace Szurubooru\Routes\Favorites;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Router;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\FavoritesService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
final class FavoritesController extends AbstractController
class RemoveFromFavorites extends AbstractRoute
{
private $privilegeService;
private $authService;
@ -29,35 +29,24 @@ final class FavoritesController extends AbstractController
$this->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);
}
}

34
src/Routes/GetGlobals.php Normal file
View file

@ -0,0 +1,34 @@
<?php
namespace Szurubooru\Routes;
use Szurubooru\Dao\GlobalParamDao;
class GetGlobals extends AbstractRoute
{
private $globalParamDao;
public function __construct(GlobalParamDao $globalParamDao)
{
$this->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;
}
}

View file

@ -1,14 +1,14 @@
<?php
namespace Szurubooru\Controllers;
namespace Szurubooru\Routes;
use Szurubooru\Controllers\ViewProxies\SnapshotViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Router;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\SearchServices\Parsers\SnapshotSearchParser;
use Szurubooru\Services\HistoryService;
use Szurubooru\Services\PrivilegeService;
final class HistoryController extends AbstractController
class GetHistory extends AbstractRoute
{
private $historyService;
private $privilegeService;
@ -30,12 +30,17 @@ final class HistoryController extends AbstractController
$this->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);

View file

@ -1,5 +1,5 @@
<?php
namespace Szurubooru\Controllers;
namespace Szurubooru\Routes;
use Szurubooru\Controllers\ViewProxies\TokenViewProxy;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\FormData\LoginFormData;
@ -10,7 +10,7 @@ use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TokenService;
use Szurubooru\Services\UserService;
final class AuthController extends AbstractController
class Login extends AbstractRoute
{
private $authService;
private $userService;
@ -38,13 +38,17 @@ final class AuthController extends AbstractController
$this->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))
{

View file

@ -0,0 +1,29 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
abstract class AbstractPostRoute extends AbstractRoute
{
protected 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,
];
}
protected function getLightFetchConfig()
{
return
[
PostViewProxy::FETCH_TAGS => true,
];
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\FormData\UploadFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class CreatePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $inputReader;
private $postViewProxy;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
InputReader $inputReader,
PostViewProxy $postViewProxy)
{
$this->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());
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class DeletePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService)
{
$this->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);
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Privilege;
use Szurubooru\Services\PostFeatureService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class FeaturePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $postFeatureService;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
PostFeatureService $postFeatureService)
{
$this->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);
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Entities\Post;
use Szurubooru\Services\PostFeatureService;
class GetFeaturedPost extends AbstractPostRoute
{
private $postFeatureService;
private $postViewProxy;
public function __construct(
PostFeatureService $postFeatureService,
UserViewProxy $userViewProxy,
PostViewProxy $postViewProxy)
{
$this->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()),
];
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetPost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $postViewProxy;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
PostViewProxy $postViewProxy)
{
$this->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());
}
}

View file

@ -1,45 +1,44 @@
<?php
namespace Szurubooru\Controllers;
namespace Szurubooru\Routes\Posts;
use Szurubooru\Config;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Post;
use Szurubooru\Helpers\MimeHelper;
use Szurubooru\Router;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PostThumbnailService;
final class PostContentController extends AbstractController
class GetPostContent extends AbstractPostRoute
{
private $config;
private $fileDao;
private $postService;
private $networkingService;
private $postThumbnailService;
public function __construct(
Config $config,
PublicFileDao $fileDao,
PostService $postService,
NetworkingService $networkingService,
PostThumbnailService $postThumbnailService)
NetworkingService $networkingService)
{
$this->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));
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Config;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Post;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PostThumbnailService;
class GetPostThumbnail extends AbstractPostRoute
{
private $config;
private $fileDao;
private $postService;
private $networkingService;
private $postThumbnailService;
public function __construct(
Config $config,
PublicFileDao $fileDao,
PostService $postService,
NetworkingService $networkingService,
PostThumbnailService $postThumbnailService)
{
$this->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));
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Config;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Parsers\PostSearchParser;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetPosts extends AbstractPostRoute
{
private $config;
private $privilegeService;
private $postService;
private $postSearchParser;
private $inputReader;
private $postViewProxy;
public function __construct(
Config $config,
PrivilegeService $privilegeService,
PostService $postService,
PostSearchParser $postSearchParser,
InputReader $inputReader,
PostViewProxy $postViewProxy)
{
$this->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()];
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Szurubooru\Routes\Posts\Notes;
use Szurubooru\Controllers\ViewProxies\PostNoteViewProxy;
use Szurubooru\FormData\PostNoteFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Routes\Posts\AbstractPostRoute;
use Szurubooru\Services\PostNotesService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class AddPostNote extends AbstractPostRoute
{
private $inputReader;
private $postService;
private $postNotesService;
private $privilegeService;
private $postNoteViewProxy;
public function __construct(
InputReader $inputReader,
PostService $postService,
PostNotesService $postNotesService,
PrivilegeService $privilegeService,
PostNoteViewProxy $postNoteViewProxy)
{
$this->inputReader = $inputReader;
$this->postService = $postService;
$this->postNotesService = $postNotesService;
$this->privilegeService = $privilegeService;
$this->postNoteViewProxy = $postNoteViewProxy;
}
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);
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace Szurubooru\Routes\Posts\Notes;
use Szurubooru\Privilege;
use Szurubooru\Routes\Posts\AbstractPostRoute;
use Szurubooru\Services\PostNotesService;
use Szurubooru\Services\PrivilegeService;
class DeletePostNote extends AbstractPostRoute
{
private $postNotesService;
private $privilegeService;
public function __construct(
PostNotesService $postNotesService,
PrivilegeService $privilegeService)
{
$this->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);
}
}

View file

@ -0,0 +1,49 @@
<?php
namespace Szurubooru\Routes\Posts\Notes;
use Szurubooru\Controllers\ViewProxies\PostNoteViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Routes\Posts\AbstractPostRoute;
use Szurubooru\Services\PostNotesService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class GetPostNotes extends AbstractPostRoute
{
private $inputReader;
private $postService;
private $postNotesService;
private $privilegeService;
private $postNoteViewProxy;
public function __construct(
InputReader $inputReader,
PostService $postService,
PostNotesService $postNotesService,
PrivilegeService $privilegeService,
PostNoteViewProxy $postNoteViewProxy)
{
$this->inputReader = $inputReader;
$this->postService = $postService;
$this->postNotesService = $postNotesService;
$this->privilegeService = $privilegeService;
$this->postNoteViewProxy = $postNoteViewProxy;
}
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);
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace Szurubooru\Routes\Posts\Notes;
use Szurubooru\Controllers\ViewProxies\PostNoteViewProxy;
use Szurubooru\FormData\PostNoteFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Routes\Posts\AbstractPostRoute;
use Szurubooru\Services\PostNotesService;
use Szurubooru\Services\PrivilegeService;
class UpdatePostNote extends AbstractPostRoute
{
private $inputReader;
private $postNotesService;
private $privilegeService;
private $postNoteViewProxy;
public function __construct(
InputReader $inputReader,
PostNotesService $postNotesService,
PrivilegeService $privilegeService,
PostNoteViewProxy $postNoteViewProxy)
{
$this->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);
}
}

View file

@ -0,0 +1,64 @@
<?php
namespace Szurubooru\Routes\Posts;
use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\FormData\PostEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
class UpdatePost extends AbstractPostRoute
{
private $privilegeService;
private $postService;
private $inputReader;
private $postViewProxy;
public function __construct(
PrivilegeService $privilegeService,
PostService $postService,
InputReader $inputReader,
PostViewProxy $postViewProxy)
{
$this->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());
}
}

View file

@ -0,0 +1,48 @@
<?php
namespace Szurubooru\Routes\Scores;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Entities\Entity;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\ScoreService;
use Szurubooru\Routes\AbstractRoute;
abstract class AbstractScoreRoute extends AbstractRoute
{
private $privilegeService;
private $scoreService;
private $authService;
public function __construct(
AuthService $authService,
InputReader $inputReader,
PrivilegeService $privilegeService,
ScoreService $scoreService)
{
$this->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(),
];
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Szurubooru\Routes\Scores;
use Szurubooru\Entities\Entity;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\ScoreService;
class GetCommentScore extends AbstractScoreRoute
{
private $commentService;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
CommentService $commentService,
ScoreService $scoreService,
InputReader $inputReader)
{
parent::__construct(
$authService,
$inputReader,
$privilegeService,
$scoreService);
$this->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);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Szurubooru\Routes\Scores;
use Szurubooru\Entities\Entity;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\ScoreService;
class GetPostScore extends AbstractScoreRoute
{
private $postService;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
PostService $postService,
ScoreService $scoreService,
InputReader $inputReader)
{
parent::__construct(
$authService,
$inputReader,
$privilegeService,
$scoreService);
$this->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);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Szurubooru\Routes\Scores;
use Szurubooru\Entities\Entity;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\CommentService;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\ScoreService;
class SetCommentScore extends AbstractScoreRoute
{
private $commentService;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
CommentService $commentService,
ScoreService $scoreService,
InputReader $inputReader)
{
parent::__construct(
$authService,
$inputReader,
$privilegeService,
$scoreService);
$this->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);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Szurubooru\Routes\Scores;
use Szurubooru\Entities\Entity;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\ScoreService;
class SetPostScore extends AbstractScoreRoute
{
private $postService;
public function __construct(
PrivilegeService $privilegeService,
AuthService $authService,
PostService $postService,
ScoreService $scoreService,
InputReader $inputReader)
{
parent::__construct(
$authService,
$inputReader,
$privilegeService,
$scoreService);
$this->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);
}
}

View file

@ -0,0 +1,17 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
abstract class AbstractTagRoute extends AbstractRoute
{
protected function getFullFetchConfig()
{
return
[
TagViewProxy::FETCH_IMPLICATIONS => true,
TagViewProxy::FETCH_SUGGESTIONS => true,
TagViewProxy::FETCH_HISTORY => true,
];
}
}

View file

@ -0,0 +1,36 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class DeleteTag extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService)
{
$this->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);
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class GetTag extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy)
{
$this->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());
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class GetTagSiblings extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy)
{
$this->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,
];
}
}

View file

@ -0,0 +1,56 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Parsers\TagSearchParser;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class GetTags extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
private $tagSearchParser;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy,
TagSearchParser $tagSearchParser,
InputReader $inputReader)
{
$this->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()];
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class MergeTags extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
InputReader $inputReader)
{
$this->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);
}
}

View file

@ -0,0 +1,62 @@
<?php
namespace Szurubooru\Routes\Tags;
use Szurubooru\Controllers\ViewProxies\TagViewProxy;
use Szurubooru\FormData\TagEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\TagService;
class UpdateTag extends AbstractTagRoute
{
private $privilegeService;
private $tagService;
private $tagViewProxy;
private $inputReader;
public function __construct(
PrivilegeService $privilegeService,
TagService $tagService,
TagViewProxy $tagViewProxy,
InputReader $inputReader)
{
$this->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());
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Routes\AbstractRoute;
abstract class AbstractUserRoute extends AbstractRoute
{
}

View file

@ -0,0 +1,29 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Services\UserService;
class ActivateAccount extends AbstractUserRoute
{
private $userService;
public function __construct(UserService $userService)
{
$this->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);
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\FormData\RegistrationFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\UserService;
class CreateUser extends AbstractUserRoute
{
private $privilegeService;
private $userService;
private $inputReader;
private $userViewProxy;
public function __construct(
PrivilegeService $privilegeService,
UserService $userService,
InputReader $inputReader,
UserViewProxy $userViewProxy)
{
$this->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);
}
}

View file

@ -0,0 +1,43 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\UserService;
class DeleteUser extends AbstractUserRoute
{
private $privilegeService;
private $userService;
public function __construct(
PrivilegeService $privilegeService,
UserService $userService)
{
$this->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);
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Services\TokenService;
use Szurubooru\Services\UserService;
class FinishActivation extends AbstractUserRoute
{
private $userService;
private $tokenService;
public function __construct(
UserService $userService,
TokenService $tokenService)
{
$this->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);
}
}

View file

@ -0,0 +1,34 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Services\TokenService;
use Szurubooru\Services\UserService;
class FinishPasswordReset extends AbstractUserRoute
{
private $userService;
private $tokenService;
public function __construct(
UserService $userService,
TokenService $tokenService)
{
$this->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)];
}
}

View file

@ -0,0 +1,46 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Parsers\UserSearchParser;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\UserService;
class GetUser extends AbstractUserRoute
{
private $privilegeService;
private $userService;
private $userSearchParser;
private $userViewProxy;
public function __construct(
PrivilegeService $privilegeService,
UserService $userService,
UserSearchParser $userSearchParser,
UserViewProxy $userViewProxy)
{
$this->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);
}
}

View file

@ -1,5 +1,5 @@
<?php
namespace Szurubooru\Controllers;
namespace Szurubooru\Routes\Users;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\User;
use Szurubooru\Router;
@ -7,7 +7,7 @@ use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\ThumbnailService;
use Szurubooru\Services\UserService;
final class UserAvatarController extends AbstractController
class GetUserAvatar extends AbstractUserRoute
{
private $fileDao;
private $userService;
@ -26,13 +26,21 @@ final class UserAvatarController extends AbstractController
$this->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);

View file

@ -0,0 +1,59 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Config;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\SearchServices\Parsers\UserSearchParser;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\UserService;
class GetUsers extends AbstractUserRoute
{
private $config;
private $privilegeService;
private $userService;
private $userSearchParser;
private $inputReader;
private $userViewProxy;
public function __construct(
Config $config,
PrivilegeService $privilegeService,
UserService $userService,
UserSearchParser $userSearchParser,
InputReader $inputReader,
UserViewProxy $userViewProxy)
{
$this->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()];
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Services\UserService;
class PasswordReset extends AbstractUserRoute
{
public function __construct(UserService $userService)
{
$this->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);
}
}

View file

@ -0,0 +1,96 @@
<?php
namespace Szurubooru\Routes\Users;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\FormData\UserEditFormData;
use Szurubooru\Helpers\InputReader;
use Szurubooru\Privilege;
use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\UserService;
class UpdateUser extends AbstractUserRoute
{
private $privilegeService;
private $userService;
private $inputReader;
private $userViewProxy;
public function __construct(
PrivilegeService $privilegeService,
UserService $userService,
InputReader $inputReader,
UserViewProxy $userViewProxy)
{
$this->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);
}
}

View file

@ -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),
];
}),
];

View file

@ -1,13 +0,0 @@
<?php
namespace Szurubooru\Tests;
use Szurubooru\Injector;
use Szurubooru\Tests\AbstractTestCase;
final class ControllerRepositoryTest extends AbstractDatabaseTestCase
{
public function testInjection()
{
$controllerRepository = Injector::get(\Szurubooru\ControllerRepository::class);
$this->assertNotEmpty($controllerRepository->getControllers());
}
}

View file

@ -1,8 +1,8 @@
<?php
namespace Szurubooru\Tests;
use Szurubooru\ControllerRepository;
use Szurubooru\Dispatcher;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\RouteRepository;
use Szurubooru\Router;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\TokenService;
@ -15,7 +15,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase
private $httpHelperMock;
private $authServiceMock;
private $tokenServiceMock;
private $controllerRepositoryMock;
private $routeRepositoryMock;
public function setUp()
{
@ -25,7 +25,7 @@ final class DispatcherTest extends AbstractDatabaseTestCase
$this->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);
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Szurubooru\Tests;
use Szurubooru\Injector;
use Szurubooru\RouteRepository;
use Szurubooru\Router;
use Szurubooru\Routes\AbstractRoute;
use Szurubooru\Tests\AbstractDatabaseTestCase;
final class RouteRepositoryTest extends AbstractDatabaseTestCase
{
public function testFactory()
{
$routeRepository = Injector::get(RouteRepository::class);
$this->assertNotEmpty($routeRepository->getRoutes());
}
public function testRouteMethods()
{
$routeRepository = Injector::get(RouteRepository::class);
foreach ($routeRepository->getRoutes() as $route)
{
foreach ($route->getMethods() as $method)
{
$this->assertContains($method, ['GET', 'POST', 'PUT', 'DELETE']);
}
}
}
public function testRouteUrls()
{
$routeRepository = Injector::get(RouteRepository::class);
foreach ($routeRepository->getRoutes() as $route)
{
$this->assertEquals(0, strpos($route->getUrl(), '/api/'));
}
}
public function testRouteInjection()
{
$routerMock = $this->mock(Router::class);
$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');
}
}

View file

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