Skip to content

Commit

Permalink
feat: added an Application class to avoid duplicated code
Browse files Browse the repository at this point in the history
- we moved some classes to the Administration folder
  • Loading branch information
thorsten committed Oct 24, 2023
1 parent 9ab0102 commit f468ffe
Show file tree
Hide file tree
Showing 25 changed files with 444 additions and 393 deletions.
71 changes: 5 additions & 66 deletions phpmyfaq/admin/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,18 @@
* @since 2023-07-02
*/

use phpMyFAQ\Application;
use phpMyFAQ\Configuration;
use phpMyFAQ\Language;
use phpMyFAQ\Translation;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;

require '../../src/Bootstrap.php';

$faqConfig = Configuration::getConfigurationInstance();

//
// Create Request & Response
//
$response = new JsonResponse();
$request = Request::createFromGlobals();
$routes = include PMF_SRC_DIR . '/admin-routes.php';

//
// Get language (default: english)
//
$language = new Language($faqConfig);
$currentLanguage = $language->setLanguageByAcceptLanguage();

//
// Set translation class
//
$app = new Application($faqConfig);
try {
Translation::create()
->setLanguagesDir(PMF_TRANSLATION_DIR)
->setDefaultLanguage('en')
->setCurrentLanguage($currentLanguage)
->setMultiByteLanguage();
} catch (Exception $e) {
echo '<strong>Error:</strong> ' . $e->getMessage();
}

$routes = new RouteCollection();

require '../../src/admin-routes.php';

$context = new RequestContext();
$context->fromRequest($request);
$matcher = new UrlMatcher($routes, $context);

$controllerResolver = new ControllerResolver();
$argumentResolver = new ArgumentResolver();

try {
$request->attributes->add($matcher->match($request->getPathInfo()));

$controller = $controllerResolver->getController($request);
$arguments = $argumentResolver->getArguments($request, $controller);

$response->setStatusCode(Response::HTTP_OK);
$response = call_user_func_array($controller, $arguments);
} catch (ResourceNotFoundException $exception) {
$response = new Response('Not Found', 404);
$app->run($routes);
} catch (Exception $exception) {
$response = new Response(
sprintf(
'An error occurred: %s at line %d at %s',
$exception->getMessage(),
$exception->getLine(),
$exception->getFile()
),
500
);
echo $exception->getMessage();
}

$response->send();
2 changes: 1 addition & 1 deletion phpmyfaq/admin/api/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
* @since 2009-04-04
*/

use phpMyFAQ\Administration\Report;
use phpMyFAQ\Auth;
use phpMyFAQ\Category;
use phpMyFAQ\Component\Alert;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Filter;
use phpMyFAQ\Helper\MailHelper;
use phpMyFAQ\Permission;
use phpMyFAQ\Report;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Strings;
use phpMyFAQ\Translation;
Expand Down
Loading

0 comments on commit f468ffe

Please sign in to comment.