Skip to content

Commit

Permalink
refactor: moved APIs to controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jul 29, 2023
1 parent 0a2f4dc commit f10f371
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 67 deletions.
33 changes: 19 additions & 14 deletions phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,11 @@ RewriteRule api/autocomplete api/index.php
# * http://[...]/api/v2.0/<ACTION>
RewriteRule api/v2.0/version api/index.php
RewriteRule api/v2.0/language api/index.php
RewriteRule api/v2.0/categories api.php?action=categories [L,QSA]
RewriteRule api/v2.0/searches/popular api.php?action=searches&filter=popular [L,QSA]
RewriteRule api/v2.0/categories api/index.php
RewriteRule api/v2.0/searches/popular api/index.php
RewriteRule api/v2.0/search api/index.php
RewriteRule api/v2.0/tags api.php?action=tags [L,QSA]
RewriteRule api/v2.0/open-questions api.php?action=open-questions [L,QSA]
RewriteRule api/v2.0/tags api/index.php
RewriteRule api/v2.0/open-questions api/index.php
RewriteRule api/v2.0/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA]
RewriteRule api/v2.0/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA]
RewriteRule api/v2.0/news api.php?action=news [L,QSA]
Expand All @@ -168,11 +168,11 @@ RewriteRule api/v2.0/faq/([0-9]+)/([0-9]+) api.php?action=faq&categoryId
# * http://[...]/api/v2.1/<ACTION>
RewriteRule api/v2.1/version api/index.php
RewriteRule api/v2.1/language api/index.php
RewriteRule api/v2.1/categories api.php?action=categories [L,QSA]
RewriteRule api/v2.1/searches/popular api.php?action=searches&filter=popular [L,QSA]
RewriteRule api/v2.1/categories api/index.php
RewriteRule api/v2.1/searches/popular api/index.php
RewriteRule api/v2.1/search api/index.php
RewriteRule api/v2.1/tags api.php?action=tags [L,QSA]
RewriteRule api/v2.1/open-questions api.php?action=open-questions [L,QSA]
RewriteRule api/v2.1/tags api/index.php
RewriteRule api/v2.1/open-questions api/index.php
RewriteRule api/v2.1/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA]
RewriteRule api/v2.1/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA]
RewriteRule api/v2.1/news api.php?action=news [L,QSA]
Expand All @@ -193,13 +193,13 @@ RewriteRule api/v2.1/question api.php?action=question [L,QS
RewriteRule api/v2.2/version api/index.php
RewriteRule api/v2.2/title api/index.php
RewriteRule api/v2.2/language api/index.php
RewriteRule api/v2.2/categories api.php?action=categories [L,QSA]
RewriteRule api/v2.2/categories api/index.php
RewriteRule api/v2.2/category api.php?action=category [L,QSA]
RewriteRule api/v2.2/groups api.php?action=groups [L,QSA]
RewriteRule api/v2.2/searches/popular api.php?action=searches&filter=popular [L,QSA]
RewriteRule api/v2.2/groups api/index.php
RewriteRule api/v2.2/searches/popular api/index.php
RewriteRule api/v2.2/search api/index.php
RewriteRule api/v2.2/tags api.php?action=tags [L,QSA]
RewriteRule api/v2.2/open-questions api.php?action=open-questions [L,QSA]
RewriteRule api/v2.2/tags api/index.php
RewriteRule api/v2.2/open-questions api/index.php
RewriteRule api/v2.2/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA]
RewriteRule api/v2.2/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA]
RewriteRule api/v2.2/news api.php?action=news [L,QSA]
Expand All @@ -216,7 +216,12 @@ RewriteRule api/v2.2/register api.php?action=register [L,QS

# REST API v2.3
# * http://[...]/api/v2.3/<ACTION>
RewriteRule api/v2.3/categories api/index.php
RewriteRule api/v2.3/groups api/index.php
RewriteRule api/v2.3/language api/index.php
RewriteRule api/v2.3/open-questions api/index.php
RewriteRule api/v2.3/search api/index.php
RewriteRule api/v2.3/searches/popular api/index.php
RewriteRule api/v2.3/tags api/index.php
RewriteRule api/v2.3/title api/index.php
RewriteRule api/v2.3/version api/index.php
RewriteRule api/v2.3/search api/index.php
53 changes: 0 additions & 53 deletions phpmyfaq/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use phpMyFAQ\Entity\FaqEntity;
use phpMyFAQ\Faq;
use phpMyFAQ\Faq\FaqMetaData;
use phpMyFAQ\Faq\FaqPermission;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\QuestionHelper;
use phpMyFAQ\Helper\RegistrationHelper;
Expand All @@ -37,14 +36,12 @@
use phpMyFAQ\Permission\MediumPermission;
use phpMyFAQ\Question;
use phpMyFAQ\Search;
use phpMyFAQ\Search\SearchResultSet;
use phpMyFAQ\Services;
use phpMyFAQ\Strings;
use phpMyFAQ\Tags;
use phpMyFAQ\Translation;
use phpMyFAQ\User\CurrentUser;
use phpMyFAQ\User\UserAuthentication;
use phpMyFAQ\Utils;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -128,20 +125,6 @@
//
// v2.3
//
case 'categories':
$category = new Category($faqConfig, $currentGroups, true);
$category->setUser($currentUser);
$category->setGroups($currentGroups);
$category->setLanguage($currentLanguage);
$result = array_values($category->getAllCategories());
if (count($result) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
} else {
$response->setStatusCode(Response::HTTP_OK);
}
$response->setData($result);
break;

case 'category':
$category = new Category($faqConfig, $currentGroups, true);
$category->setUser($currentUser);
Expand Down Expand Up @@ -238,42 +221,6 @@
$response->setData($result);
break;

case 'groups':
$groupPermission = new MediumPermission($faqConfig);
$result = $groupPermission->getAllGroups($user);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);
break;

case 'tags':
$tags = new Tags($faqConfig);
$result = $tags->getPopularTagsAsArray(16);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);
break;

case 'open-questions':
$questions = new Question($faqConfig);
$result = $questions->getAllOpenQuestions();
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);
break;

case 'searches':
$search = new Search($faqConfig);
$result = $search->getMostPopularSearches(7, true);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);
break;

case 'comments':
$comment = new Comments($faqConfig);
$result = $comment->getCommentsData($recordId, CommentType::FAQ);
Expand Down
26 changes: 26 additions & 0 deletions phpmyfaq/src/api-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
* @since 2023-07-29
*/

use phpMyFAQ\Api\Controller\CategoryController;
use phpMyFAQ\Api\Controller\GroupController;
use phpMyFAQ\Api\Controller\LanguageController;
use phpMyFAQ\Api\Controller\OpenQuestionController;
use phpMyFAQ\Api\Controller\SearchController;
use phpMyFAQ\Api\Controller\TagController;
use phpMyFAQ\Api\Controller\TitleController;
use phpMyFAQ\Api\Controller\VersionController;
use phpMyFAQ\Controller\AutoCompleteController;
Expand All @@ -28,14 +32,35 @@

$routes = new RouteCollection();

// Public REST API
$routes->add(
'api.categories',
new Route("v{$apiVersion}/categories", ['_controller' => [CategoryController::class, 'list']])
);
$routes->add(
'api.groups',
new Route("v{$apiVersion}/groups", ['_controller' => [GroupController::class, 'list']])
);
$routes->add(
'api.language',
new Route("v{$apiVersion}/language", ['_controller' => [LanguageController::class, 'index']])
);
$routes->add(
'api.open-questions',
new Route("v{$apiVersion}/open-questions", ['_controller' => [OpenQuestionController::class, 'list']])
);
$routes->add(
'api.search',
new Route("v{$apiVersion}/search", ['_controller' => [SearchController::class, 'search']])
);
$routes->add(
'api.search.popular',
new Route("v{$apiVersion}/searches/popular", ['_controller' => [SearchController::class, 'popular']])
);
$routes->add(
'api.tags',
new Route("v{$apiVersion}/tags", ['_controller' => [TagController::class, 'list']])
);
$routes->add(
'api.title',
new Route("v{$apiVersion}/title", ['_controller' => [TitleController::class, 'index']])
Expand All @@ -45,6 +70,7 @@
new Route("v$apiVersion/version", ['_controller' => [VersionController::class, 'index']])
);

// Private REST API
$routes->add(
'api.autocomplete',
new Route('autocomplete', ['_controller' => [AutoCompleteController::class, 'search']])
Expand Down
54 changes: 54 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/CategoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* The Category Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-07-29
*/

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Category;
use phpMyFAQ\Configuration;
use phpMyFAQ\Language;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class CategoryController
{
public function list(): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();
$language = new Language($faqConfig);
$currentLanguage = $language->setLanguageByAcceptLanguage();

$user = CurrentUser::getCurrentUser($faqConfig);
[ $currentUser, $currentGroups ] = CurrentUser::getCurrentUserGroupId($user);

$category = new Category($faqConfig, $currentGroups, true);
$category->setUser($currentUser);
$category->setGroups($currentGroups);
$category->setLanguage($currentLanguage);
$result = array_values($category->getAllCategories());

if (count($result) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
} else {
$response->setStatusCode(Response::HTTP_OK);
}
$response->setData($result);

return $response;
}
}
43 changes: 43 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/GroupController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* The Group Controller for the REST API
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at https://mozilla.org/MPL/2.0/.
*
* @package phpMyFAQ
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2023 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2023-07-29
*/

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
use phpMyFAQ\Permission\MediumPermission;
use phpMyFAQ\User\CurrentUser;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class GroupController
{
public function list(): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();
$user = CurrentUser::getCurrentUser($faqConfig);

$groupPermission = new MediumPermission($faqConfig);
$result = $groupPermission->getAllGroups($user);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);

return $response;
}
}
26 changes: 26 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/OpenQuestionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
use phpMyFAQ\Question;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class OpenQuestionController
{
public function list(): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();

$questions = new Question($faqConfig);
$result = $questions->getAllOpenQuestions();
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);

return $response;
}
}
15 changes: 15 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,19 @@ public function search(Request $request): JsonResponse

return $response;
}

public function popular(): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();

$search = new Search($faqConfig);
$result = $search->getMostPopularSearches(7, true);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);

return $response;
}
}
26 changes: 26 additions & 0 deletions phpmyfaq/src/phpMyFAQ/Api/Controller/TagController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace phpMyFAQ\Api\Controller;

use phpMyFAQ\Configuration;
use phpMyFAQ\Tags;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;

class TagController
{
public function list(): JsonResponse
{
$response = new JsonResponse();
$faqConfig = Configuration::getConfigurationInstance();

$tags = new Tags($faqConfig);
$result = $tags->getPopularTagsAsArray(16);
if ((is_countable($result) ? count($result) : 0) === 0) {
$response->setStatusCode(Response::HTTP_NOT_FOUND);
}
$response->setData($result);

return $response;
}
}

0 comments on commit f10f371

Please sign in to comment.