Skip to content

Commit e5d580f

Browse files
committed
refactor: added new class for environment configuration during setup and update
1 parent ee4ed19 commit e5d580f

File tree

14 files changed

+198
-68
lines changed

14 files changed

+198
-68
lines changed

phpmyfaq/admin/instances.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use phpMyFAQ\Configuration;
1919
use phpMyFAQ\Entity\InstanceEntity;
2020
use phpMyFAQ\Enums\PermissionType;
21-
use phpMyFAQ\Filesystem;
21+
use phpMyFAQ\Filesystem\Filesystem;
2222
use phpMyFAQ\Filter;
2323
use phpMyFAQ\Instance;
2424
use phpMyFAQ\Instance\Client;

phpmyfaq/src/phpMyFAQ/Controller/Administration/InstanceController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use phpMyFAQ\Database;
2424
use phpMyFAQ\Entity\InstanceEntity;
2525
use phpMyFAQ\Enums\PermissionType;
26-
use phpMyFAQ\Filesystem;
26+
use phpMyFAQ\Filesystem\Filesystem;
2727
use phpMyFAQ\Filter;
2828
use phpMyFAQ\Instance\Client;
2929
use phpMyFAQ\Instance\Setup;

phpmyfaq/src/phpMyFAQ/Controller/Administration/UpdateController.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ public function downloadPackage(Request $request): JsonResponse
183183
return $this->json(['success' => Translation::get('downloadSuccessful')], Response::HTTP_OK);
184184
}
185185

186-
/**
187-
* @throws Exception
188-
*/
189186
#[Route('admin/api/extract-package')]
190187
public function extractPackage(): StreamedResponse
191188
{
@@ -208,9 +205,6 @@ public function extractPackage(): StreamedResponse
208205
});
209206
}
210207

211-
/**
212-
* @throws Exception
213-
*/
214208
#[Route('admin/api/create-temporary-backup')]
215209
public function createTemporaryBackup(): StreamedResponse
216210
{
@@ -233,9 +227,6 @@ public function createTemporaryBackup(): StreamedResponse
233227
});
234228
}
235229

236-
/**
237-
* @throws Exception
238-
*/
239230
#[Route('admin/api/install-package')]
240231
public function installPackage(): StreamedResponse
241232
{
@@ -256,9 +247,6 @@ public function installPackage(): StreamedResponse
256247
});
257248
}
258249

259-
/**
260-
* @throws Exception
261-
*/
262250
#[Route('admin/api/update-database')]
263251
public function updateDatabase(): StreamedResponse
264252
{

phpmyfaq/src/phpMyFAQ/Filesystem.php renamed to phpmyfaq/src/phpMyFAQ/Filesystem/Filesystem.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* @since 2012-04-02
1616
*/
1717

18-
namespace phpMyFAQ;
18+
namespace phpMyFAQ\Filesystem;
1919

2020
use phpMyFAQ\Core\Exception;
2121

@@ -117,7 +117,7 @@ public function moveDirectory(string $sourcePath, string $destinationPath): bool
117117
}
118118

119119
/**
120-
* Deletes given directory.
120+
* Deletes the given directory.
121121
*/
122122
public function deleteDirectory(string $pathname): bool
123123
{

phpmyfaq/src/phpMyFAQ/Instance/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
namespace phpMyFAQ\Instance;
1919

2020
use phpMyFAQ\Configuration;
21-
use phpMyFAQ\Database;
2221
use phpMyFAQ\Core\Exception;
23-
use phpMyFAQ\Filesystem;
22+
use phpMyFAQ\Database;
23+
use phpMyFAQ\Filesystem\Filesystem;
2424
use phpMyFAQ\Instance;
2525
use phpMyFAQ\Instance\Database as InstanceDatabase;
2626

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace phpMyFAQ\Setup;
4+
5+
use phpMyFAQ\Core\Exception;
6+
use SplFileObject;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Tivie\HtaccessParser\Exception\SyntaxException;
9+
use Tivie\HtaccessParser\Parser;
10+
11+
use const Tivie\HtaccessParser\Token\TOKEN_DIRECTIVE;
12+
13+
class EnvironmentConfigurator
14+
{
15+
private string $rootFilePath;
16+
17+
private string $htaccessPath;
18+
19+
private string $serverPath;
20+
21+
public function __construct(string $rootPath = '', private readonly ?Request $request = null)
22+
{
23+
$this->rootFilePath = $rootPath;
24+
$this->htaccessPath = $this->rootFilePath . '/.htaccess';
25+
}
26+
27+
public function getRootFilePath(): string
28+
{
29+
return $this->rootFilePath;
30+
}
31+
32+
public function getHtaccessPath(): string
33+
{
34+
return $this->htaccessPath;
35+
}
36+
37+
public function getServerPath(): string
38+
{
39+
return $this->serverPath = $this->request->getPathInfo();
40+
}
41+
42+
/**
43+
* @throws Exception
44+
*/
45+
public function getRewriteBase(): string
46+
{
47+
$file = new SplFileObject($this->htaccessPath);
48+
$parser = new Parser();
49+
try {
50+
$htaccess = $parser->parse($file);
51+
} catch (SyntaxException $e) {
52+
throw new Exception('Syntax error in .htaccess file: ' . $e->getMessage());
53+
} catch (\Tivie\HtaccessParser\Exception\Exception $e) {
54+
throw new Exception('Error parsing .htaccess file: ' . $e->getMessage());
55+
}
56+
$rewriteBase = $htaccess->search('RewriteBase', TOKEN_DIRECTIVE);
57+
58+
return $rewriteBase->getArguments()[0];
59+
}
60+
61+
/**
62+
* Adjusts the RewriteBase in the .htaccess file for the user's environment to avoid errors with controllers.
63+
* Returns true, if the file was successfully changed.
64+
*
65+
* @throws Exception
66+
*/
67+
public function adjustRewriteBaseHtaccess(): bool
68+
{
69+
if (!file_exists($this->htaccessPath)) {
70+
throw new Exception(sprintf('The %s file does not exist!', $this->htaccessPath));
71+
}
72+
73+
$file = new SplFileObject($this->htaccessPath);
74+
$parser = new Parser();
75+
try {
76+
$htaccess = $parser->parse($file);
77+
} catch (SyntaxException $e) {
78+
throw new Exception('Syntax error in .htaccess file: ' . $e->getMessage());
79+
} catch (\Tivie\HtaccessParser\Exception\Exception $e) {
80+
throw new Exception('Error parsing .htaccess file: ' . $e->getMessage());
81+
}
82+
$rewriteBase = $htaccess->search('RewriteBase', TOKEN_DIRECTIVE);
83+
84+
$rewriteBase->removeArgument($this->getRewriteBase());
85+
$rewriteBase->setArguments((array)$this->getServerPath());
86+
87+
$output = (string) $htaccess;
88+
return file_put_contents($this->htaccessPath, $output);
89+
}
90+
}

phpmyfaq/src/phpMyFAQ/Setup/Installer.php

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use phpMyFAQ\Link;
4242
use phpMyFAQ\System;
4343
use phpMyFAQ\User;
44+
use Symfony\Component\HttpFoundation\Request;
4445

4546
/**
4647
* Class Installer
@@ -1142,7 +1143,8 @@ public function startInstall(array|null $setup = null): void
11421143
}
11431144

11441145
// adjust RewriteBase in .htaccess
1145-
$this->adjustRewriteBaseHtaccess($rootDir);
1146+
$envConfiguration = new EnvironmentConfigurator($rootDir, Request::createFromGlobals());
1147+
$envConfiguration->adjustRewriteBaseHtaccess();
11461148
}
11471149

11481150
/**
@@ -1154,37 +1156,6 @@ public function checkMinimumPhpVersion(): bool
11541156
return version_compare(PHP_VERSION, System::VERSION_MINIMUM_PHP) >= 0;
11551157
}
11561158

1157-
/**
1158-
* Adjusts the RewriteBase in the .htaccess file for the user's environment to avoid errors with controllers.
1159-
* Returns true, if the file was successfully changed.
1160-
*
1161-
* @throws Exception
1162-
*/
1163-
public function adjustRewriteBaseHtaccess(string $path): bool
1164-
{
1165-
$htaccessPath = $path . '/.htaccess';
1166-
1167-
if (!file_exists($htaccessPath)) {
1168-
throw new Exception(sprintf('The %s file does not exist!', $htaccessPath));
1169-
}
1170-
1171-
$lines = file($htaccessPath);
1172-
$newLines = [];
1173-
1174-
foreach ($lines as $line) {
1175-
if (str_starts_with($line, 'RewriteBase')) {
1176-
$requestUri = filter_input(INPUT_SERVER, 'PHP_SELF');
1177-
$rewriteBase = substr((string) $requestUri, 0, strpos((string) $requestUri, 'setup/index.php'));
1178-
$rewriteBase = ($rewriteBase === '') ? '/' : $rewriteBase;
1179-
$newLines[] = 'RewriteBase ' . $rewriteBase . PHP_EOL;
1180-
} else {
1181-
$newLines[] = $line;
1182-
}
1183-
}
1184-
1185-
return file_put_contents($htaccessPath, implode('', $newLines)) !== false;
1186-
}
1187-
11881159
public function hasLdapSupport(): bool
11891160
{
11901161
return extension_loaded('ldap');

phpmyfaq/src/phpMyFAQ/Setup/Update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use phpMyFAQ\Database;
2323
use phpMyFAQ\Database\DatabaseDriver;
2424
use phpMyFAQ\Enums\ReleaseType;
25-
use phpMyFAQ\Filesystem;
25+
use phpMyFAQ\Filesystem\Filesystem;
2626
use phpMyFAQ\Forms;
2727
use phpMyFAQ\Setup;
2828
use phpMyFAQ\System;

tests/.htaccess

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
RewriteBase /
1+
RewriteBase /phpmyfaq-test/

tests/bootstrap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
const IS_VALID_PHPMYFAQ = true;
4040
const DEBUG = true;
4141

42-
$_SERVER['HTTP_HOST'] = 'https://localhost/';
43-
$_SERVER['SERVER_NAME'] = 'https://localhost/';
42+
$_SERVER['HTTP_HOST'] = 'https://localhost/phpmyfaq-test/';
43+
$_SERVER['SERVER_NAME'] = 'https://localhost/phpmyfaq-test/';
4444

4545
require PMF_ROOT_DIR . '/content/core/config/constants.php';
4646

@@ -63,7 +63,7 @@
6363
$loader->register();
6464

6565
//
66-
// Delete possible SQLite file first
66+
// Delete a possible SQLite file first
6767
//
6868
@unlink(PMF_TEST_DIR . '/test.db');
6969

0 commit comments

Comments
 (0)