Skip to content

Commit 7cb8c2c

Browse files
committed
refactor: moved APIs to controllers
1 parent f10f371 commit 7cb8c2c

File tree

7 files changed

+142
-40
lines changed

7 files changed

+142
-40
lines changed

phpmyfaq/.htaccess

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ RewriteRule api/v2.0/searches/popular api/index.php
152152
RewriteRule api/v2.0/search api/index.php
153153
RewriteRule api/v2.0/tags api/index.php
154154
RewriteRule api/v2.0/open-questions api/index.php
155-
RewriteRule api/v2.0/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA]
156-
RewriteRule api/v2.0/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA]
157-
RewriteRule api/v2.0/news api.php?action=news [L,QSA]
155+
RewriteRule api/v2.0/comments/([0-9]+) api/index.php
156+
RewriteRule api/v2.0/attachments/([0-9]+) api/index.php
157+
RewriteRule api/v2.0/news api/index.php
158158
RewriteRule api/v2.0/login api.php?action=login [L,QSA]
159159
RewriteRule api/v2.0/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
160160
RewriteRule api/v2.0/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
@@ -173,9 +173,9 @@ RewriteRule api/v2.1/searches/popular api/index.php
173173
RewriteRule api/v2.1/search api/index.php
174174
RewriteRule api/v2.1/tags api/index.php
175175
RewriteRule api/v2.1/open-questions api/index.php
176-
RewriteRule api/v2.1/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA]
177-
RewriteRule api/v2.1/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA]
178-
RewriteRule api/v2.1/news api.php?action=news [L,QSA]
176+
RewriteRule api/v2.1/comments/([0-9]+) api/index.php
177+
RewriteRule api/v2.1/attachments/([0-9]+) api/index.php
178+
RewriteRule api/v2.1/news api/index.php
179179
RewriteRule api/v2.1/login api.php?action=login [L,QSA]
180180
RewriteRule api/v2.1/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
181181
RewriteRule api/v2.1/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
@@ -200,9 +200,9 @@ RewriteRule api/v2.2/searches/popular api/index.php
200200
RewriteRule api/v2.2/search api/index.php
201201
RewriteRule api/v2.2/tags api/index.php
202202
RewriteRule api/v2.2/open-questions api/index.php
203-
RewriteRule api/v2.2/comments/([0-9]+) api.php?action=comments&recordId=$1 [L,QSA]
204-
RewriteRule api/v2.2/attachments/([0-9]+) api.php?action=attachments&recordId=$1 [L,QSA]
205-
RewriteRule api/v2.2/news api.php?action=news [L,QSA]
203+
RewriteRule api/v2.2/comments/([0-9]+) api/index.php
204+
RewriteRule api/v2.2/attachments/([0-9]+) api/index.php
205+
RewriteRule api/v2.2/news api/index.php
206206
RewriteRule api/v2.2/login api.php?action=login [L,QSA]
207207
RewriteRule api/v2.2/faqs/([0-9]+) api.php?action=faqs&categoryId=$1 [L,QSA]
208208
RewriteRule api/v2.2/faqs/popular api.php?action=faqs&filter=popular [L,QSA]
@@ -216,7 +216,9 @@ RewriteRule api/v2.2/register api.php?action=register [L,QS
216216

217217
# REST API v2.3
218218
# * http://[...]/api/v2.3/<ACTION>
219+
RewriteRule api/v2.3/attachments/([0-9]+) api/index.php
219220
RewriteRule api/v2.3/categories api/index.php
221+
RewriteRule api/v2.3/comments/([0-9]+) api/index.php
220222
RewriteRule api/v2.3/groups api/index.php
221223
RewriteRule api/v2.3/language api/index.php
222224
RewriteRule api/v2.3/open-questions api/index.php

phpmyfaq/api.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -221,34 +221,6 @@
221221
$response->setData($result);
222222
break;
223223

224-
case 'comments':
225-
$comment = new Comments($faqConfig);
226-
$result = $comment->getCommentsData($recordId, CommentType::FAQ);
227-
if ((is_countable($result) ? count($result) : 0) === 0) {
228-
$response->setStatusCode(Response::HTTP_NOT_FOUND);
229-
}
230-
$response->setData($result);
231-
break;
232-
233-
case 'attachments':
234-
$attachments = $result = [];
235-
try {
236-
$attachments = AttachmentFactory::fetchByRecordId($faqConfig, $recordId);
237-
} catch (AttachmentException) {
238-
$result = [];
239-
}
240-
foreach ($attachments as $attachment) {
241-
$result[] = [
242-
'filename' => $attachment->getFilename(),
243-
'url' => $faqConfig->getDefaultUrl() . $attachment->buildUrl(),
244-
];
245-
}
246-
if (count($result) === 0) {
247-
$response->setStatusCode(Response::HTTP_NOT_FOUND);
248-
}
249-
$response->setData($result);
250-
break;
251-
252224
case 'news':
253225
$news = new News($faqConfig);
254226
$result = $news->getLatestData(false, true, true);

phpmyfaq/src/api-routes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
* @since 2023-07-29
1616
*/
1717

18+
use phpMyFAQ\Api\Controller\AttachmentController;
1819
use phpMyFAQ\Api\Controller\CategoryController;
20+
use phpMyFAQ\Api\Controller\CommentController;
1921
use phpMyFAQ\Api\Controller\GroupController;
2022
use phpMyFAQ\Api\Controller\LanguageController;
23+
use phpMyFAQ\Api\Controller\NewsController;
2124
use phpMyFAQ\Api\Controller\OpenQuestionController;
2225
use phpMyFAQ\Api\Controller\SearchController;
2326
use phpMyFAQ\Api\Controller\TagController;
@@ -33,10 +36,18 @@
3336
$routes = new RouteCollection();
3437

3538
// Public REST API
39+
$routes->add(
40+
'api.attachments',
41+
new Route("v{$apiVersion}/attachments/{recordId}", ['_controller' => [AttachmentController::class, 'list']])
42+
);
3643
$routes->add(
3744
'api.categories',
3845
new Route("v{$apiVersion}/categories", ['_controller' => [CategoryController::class, 'list']])
3946
);
47+
$routes->add(
48+
'api.comments',
49+
new Route("v{$apiVersion}/comments/{recordId}", ['_controller' => [CommentController::class, 'list']])
50+
);
4051
$routes->add(
4152
'api.groups',
4253
new Route("v{$apiVersion}/groups", ['_controller' => [GroupController::class, 'list']])
@@ -45,6 +56,10 @@
4556
'api.language',
4657
new Route("v{$apiVersion}/language", ['_controller' => [LanguageController::class, 'index']])
4758
);
59+
$routes->add(
60+
'api.news',
61+
new Route("v{$apiVersion}/news", ['_controller' => [NewsController::class, 'list']])
62+
);
4863
$routes->add(
4964
'api.open-questions',
5065
new Route("v{$apiVersion}/open-questions", ['_controller' => [OpenQuestionController::class, 'list']])
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace phpMyFAQ\Api\Controller;
4+
5+
use phpMyFAQ\Attachment\AttachmentException;
6+
use phpMyFAQ\Attachment\AttachmentFactory;
7+
use phpMyFAQ\Configuration;
8+
use phpMyFAQ\Filter;
9+
use Symfony\Component\HttpFoundation\JsonResponse;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
12+
13+
class AttachmentController
14+
{
15+
public function list(Request $request): JsonResponse
16+
{
17+
$response = new JsonResponse();
18+
$faqConfig = Configuration::getConfigurationInstance();
19+
20+
$recordId = Filter::filterVar($request->get('recordId'), FILTER_VALIDATE_INT);
21+
22+
$attachments = $result = [];
23+
try {
24+
$attachments = AttachmentFactory::fetchByRecordId($faqConfig, $recordId);
25+
} catch (AttachmentException) {
26+
$result = [];
27+
}
28+
foreach ($attachments as $attachment) {
29+
$result[] = [
30+
'filename' => $attachment->getFilename(),
31+
'url' => $faqConfig->getDefaultUrl() . $attachment->buildUrl(),
32+
];
33+
}
34+
if (count($result) === 0) {
35+
$response->setStatusCode(Response::HTTP_NOT_FOUND);
36+
}
37+
$response->setData($result);
38+
39+
return $response;
40+
}
41+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* The Comemnt Controller for the REST API
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public License,
7+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
8+
* obtain one at https://mozilla.org/MPL/2.0/.
9+
*
10+
* @package phpMyFAQ
11+
* @author Thorsten Rinne <[email protected]>
12+
* @copyright 2023 phpMyFAQ Team
13+
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14+
* @link https://www.phpmyfaq.de
15+
* @since 2023-07-30
16+
*/
17+
18+
namespace phpMyFAQ\Api\Controller;
19+
20+
use phpMyFAQ\Comments;
21+
use phpMyFAQ\Configuration;
22+
use phpMyFAQ\Entity\CommentType;
23+
use phpMyFAQ\Filter;
24+
use Symfony\Component\HttpFoundation\JsonResponse;
25+
use Symfony\Component\HttpFoundation\Request;
26+
use Symfony\Component\HttpFoundation\Response;
27+
28+
class CommentController
29+
{
30+
public function list(Request $request): JsonResponse
31+
{
32+
$response = new JsonResponse();
33+
$faqConfig = Configuration::getConfigurationInstance();
34+
35+
$recordId = Filter::filterVar($request->get('recordId'), FILTER_VALIDATE_INT);
36+
37+
$comment = new Comments($faqConfig);
38+
$result = $comment->getCommentsData($recordId, CommentType::FAQ);
39+
if ((is_countable($result) ? count($result) : 0) === 0) {
40+
$response->setStatusCode(Response::HTTP_NOT_FOUND);
41+
}
42+
$response->setData($result);
43+
44+
return $response;
45+
}
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace phpMyFAQ\Api\Controller;
4+
5+
use phpMyFAQ\Configuration;
6+
use phpMyFAQ\News;
7+
use Symfony\Component\HttpFoundation\JsonResponse;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class NewsController
11+
{
12+
public function list(): JsonResponse
13+
{
14+
$response = new JsonResponse();
15+
$faqConfig = Configuration::getConfigurationInstance();
16+
17+
$news = new News($faqConfig);
18+
$result = $news->getLatestData(false, true, true);
19+
if ((is_countable($result) ? count($result) : 0) === 0) {
20+
$response->setStatusCode(Response::HTTP_NOT_FOUND);
21+
}
22+
$response->setData($result);
23+
24+
return $response;
25+
}
26+
}

phpmyfaq/src/phpMyFAQ/Attachment/AttachmentFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AttachmentFactory
3838
/**
3939
* Storage type.
4040
*/
41-
private static ?int $storageType = null;
41+
private static ?int $storageType = 0;
4242

4343
/**
4444
* File encryption is enabled.
@@ -100,12 +100,12 @@ public static function fetchByRecordId(Configuration $config, int $recordId): ar
100100
);
101101

102102
$result = $config->getDb()->fetchAll($config->getDb()->query($sql));
103+
103104
if (!empty($result)) {
104105
foreach ($result as $item) {
105-
$files[] = self::create($item->id);
106+
$files[] = self::create((int) $item->id);
106107
}
107108
}
108-
reset($files);
109109

110110
return $files;
111111
}

0 commit comments

Comments
 (0)