Skip to content

Commit

Permalink
refactor: class Application and class Languae now using the container…
Browse files Browse the repository at this point in the history
… interface (#3202)
  • Loading branch information
thorsten committed Nov 3, 2024
1 parent 0cd88f5 commit 453ea03
Show file tree
Hide file tree
Showing 30 changed files with 204 additions and 64 deletions.
17 changes: 14 additions & 3 deletions phpmyfaq/admin/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@
*/

use phpMyFAQ\Application;
use phpMyFAQ\Configuration;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

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

$faqConfig = Configuration::getConfigurationInstance();
//
// Service Containers
//
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator(__DIR__));
try {
$loader->load('../../src/services.php');
} catch (\Exception $e) {
echo $e->getMessage();
}

$routes = include PMF_SRC_DIR . '/admin-routes.php';

$app = new Application($faqConfig);
$app = new Application($container);
try {
$app->run($routes);
} catch (Exception $exception) {
Expand Down
17 changes: 14 additions & 3 deletions phpmyfaq/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,26 @@
*/

use phpMyFAQ\Application;
use phpMyFAQ\Configuration;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

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

$faqConfig = Configuration::getConfigurationInstance();
//
// Service Containers
//
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator(__DIR__));
try {
$loader->load('../src/services.php');
} catch (\Exception $e) {
echo $e->getMessage();
}

$routes = include PMF_SRC_DIR . '/api-routes.php';

$app = new Application($faqConfig);
$app = new Application($container);
try {
$app->run($routes);
} catch (Exception $exception) {
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/content/plugins/HelloWorld/HelloWorldPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getName(): string

public function getVersion(): string
{
return '0.1.0';
return '0.2.0';
}

public function getDependencies(): array
Expand Down
16 changes: 14 additions & 2 deletions phpmyfaq/services/webauthn/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
use phpMyFAQ\Application;
use phpMyFAQ\Configuration;
use phpMyFAQ\Controller\WebAuthnController;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

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

$faqConfig = Configuration::getConfigurationInstance();
//
// Service Containers
//
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator(__DIR__));
try {
$loader->load('../../src/services.php');
} catch (\Exception $e) {
echo $e->getMessage();
}

$routes = new RouteCollection();
$routes->add(
'public.webauthn.index',
new Route('/', ['_controller' => [WebAuthnController::class, 'index']])
);

$app = new Application($faqConfig);
$app = new Application($container);
try {
$app->run($routes);
} catch (Exception $exception) {
Expand Down
4 changes: 1 addition & 3 deletions phpmyfaq/sitemap.xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,10 @@
echo $e->getMessage();
}

$faqConfig = $container->get('phpmyfaq.configuration');

$routes = new RouteCollection();
$routes->add('public.sitemap.xml', new Route('/sitemap.xml', ['_controller' => [SitemapController::class, 'index']]));

$app = new Application($faqConfig);
$app = new Application($container);
try {
$app->run($routes);
} catch (Exception $exception) {
Expand Down
10 changes: 6 additions & 4 deletions phpmyfaq/src/phpMyFAQ/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use ErrorException;
use phpMyFAQ\Core\Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -32,7 +33,7 @@

readonly class Application
{
public function __construct(private ?Configuration $configuration = null)
public function __construct(private ?ContainerInterface $container = null)
{
}

Expand All @@ -49,16 +50,17 @@ public function run(RouteCollection $routeCollection): void

private function setLanguage(): string
{
if ($this->configuration) {
$language = new Language($this->configuration);
if (!is_null($this->container)) {
$configuration = $this->container->get('phpmyfaq.configuration');
$language = $this->container->get('phpmyfaq.language');
$currentLanguage = $language->setLanguageByAcceptLanguage();

require sprintf('%s/language_en.php', PMF_TRANSLATION_DIR);
if (Language::isASupportedLanguage($currentLanguage)) {
require sprintf('%s/language_%s.php', PMF_TRANSLATION_DIR, $currentLanguage);
}

$this->configuration->setLanguage($language);
$configuration->setLanguage($language);

return $currentLanguage;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function update(Request $request): Response
public function render(string $pathToTwigFile, array $templateVars = [], ?Response $response = null): Response
{
$response ??= new Response();
$twigWrapper = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates');
$twigWrapper = new TwigWrapper(PMF_ROOT_DIR . '/assets/templates', true);
$twigWrapper->setSetup(true);
$template = $twigWrapper->loadTemplate($pathToTwigFile);

Expand Down
11 changes: 7 additions & 4 deletions phpmyfaq/src/phpMyFAQ/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use phpMyFAQ\Language\LanguageCodes;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

/**
* Class Language
Expand All @@ -42,8 +43,10 @@ class Language
/**
* Constructor.
*/
public function __construct(private readonly Configuration $configuration)
{
public function __construct(
private readonly Configuration $configuration,
private readonly SessionInterface $session
) {
}

/**
Expand Down Expand Up @@ -95,7 +98,7 @@ public function setLanguage(bool $configDetection, string $configLanguage): stri
{
$detectedLang = $this->detectLanguage($configDetection, $configLanguage);
self::$language = $this->selectLanguage($detectedLang);
$_SESSION['lang'] = self::$language;
$this->session->set('lang', self::$language);
return self::$language;
}

Expand Down Expand Up @@ -166,7 +169,7 @@ private function getArtGetLanguage(): ?string

private function getSessionLanguage(): ?string
{
$lang = $_SESSION['lang'] ?? null;
$lang = $this->session->get('lang');
return $this->isASupportedLanguage($lang) ? trim((string) $lang) : null;
}

Expand Down
5 changes: 3 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Template/TwigWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ class TwigWrapper
{
private Environment $twigEnvironment;

private bool $isSetup = false;
private bool $isSetup;

/** @var string Name of an active template set. */
private static string $templateSetName = 'default';

public function __construct(string $templatePath)
public function __construct(string $templatePath, bool $isSetup = false)
{
$this->isSetup = $isSetup;
$filesystemLoader = new FilesystemLoader($templatePath);
$this->twigEnvironment = new Environment(
$filesystemLoader,
Expand Down
13 changes: 12 additions & 1 deletion phpmyfaq/src/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
use phpMyFAQ\Instance;
use phpMyFAQ\Language;
use phpMyFAQ\Services\Gravatar;
use phpMyFAQ\Session\Token;
use phpMyFAQ\Sitemap;
use phpMyFAQ\Tags;
use phpMyFAQ\User\CurrentUser;
use phpMyFAQ\User\UserSession;
use phpMyFAQ\Visits;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpFoundation\Session\Session;

return static function (ContainerConfigurator $container): void {
// Parameters
Expand All @@ -43,6 +45,8 @@
// Services
$services = $container->services();

$services->set('session', Session::class);

$services->set('phpmyfaq.admin.category', Category::class)
->args([
new Reference('phpmyfaq.configuration'),
Expand Down Expand Up @@ -101,14 +105,21 @@

$services->set('phpmyfaq.language', Language::class)
->args([
new Reference('phpmyfaq.configuration')
new Reference('phpmyfaq.configuration'),
new Reference('session')
]);

$services->set('phpmyfaq.session', UserSession::class)
->args([
new Reference('phpmyfaq.configuration')
]);

$services->set('phpmyfaq.session.token', Token::class)
->factory([Token::class, 'getInstance'])
->args([
new Reference('session')
]);

$services->set('phpmyfaq.sitemap', Sitemap::class)
->args([
new Reference('phpmyfaq.configuration')
Expand Down
17 changes: 14 additions & 3 deletions phpmyfaq/update/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,30 @@
*/

use phpMyFAQ\Application;
use phpMyFAQ\Configuration;
use phpMyFAQ\Controller\Frontend\SetupController;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

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

$faqConfig = Configuration::getConfigurationInstance();
//
// Service Containers
//
$container = new ContainerBuilder();
$loader = new PhpFileLoader($container, new FileLocator(__DIR__));
try {
$loader->load('../src/services.php');
} catch (\Exception $e) {
echo $e->getMessage();
}

$routes = new RouteCollection();
$routes->add('public.update.index', new Route('/', ['_controller' => [SetupController::class, 'update']]));

$app = new Application($faqConfig);
$app = new Application($container);
try {
$app->run($routes);
} catch (Exception $exception) {
Expand Down
20 changes: 11 additions & 9 deletions tests/phpMyFAQ/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@

namespace phpMyFAQ;

use phpMyFAQ\Application;
use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Language;
use phpMyFAQ\Translation;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;

class ApplicationTest extends TestCase
{
/**
* @throws Exception
*/
public function testConstructor(): void
{
$config = $this->createMock(Configuration::class);
$application = new Application($config);
$container = $this->createMock(ContainerInterface::class);
$application = new Application($container);
$this->assertInstanceOf(Application::class, $application);
}


/**
* @throws Exception
* @throws \ReflectionException
*/
public function testHandleRequest(): void
{
$routeCollection = $this->createMock(RouteCollection::class);
Expand Down
8 changes: 7 additions & 1 deletion tests/phpMyFAQ/BookmarkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\User\CurrentUser;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;

class BookmarkTest extends TestCase
{
private Bookmark $bookmark;

/**
* @throws Exception
* @throws Core\Exception
*/
protected function setUp(): void
{
parent::setUp();
Expand All @@ -31,7 +37,7 @@ protected function setUp(): void
$configuration->set('main.referenceURL', 'http://example.com');

$user = CurrentUser::getCurrentUser($configuration);
$language = new Language($configuration);
$language = new Language($configuration, $this->createMock(Session::class));
$language->setLanguage(false, 'en');
$configuration->setLanguage($language);

Expand Down
Loading

0 comments on commit 453ea03

Please sign in to comment.