-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: added new class for environment configuration during setup …
…and update
- Loading branch information
Showing
14 changed files
with
198 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace phpMyFAQ\Setup; | ||
|
||
use phpMyFAQ\Core\Exception; | ||
use SplFileObject; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Tivie\HtaccessParser\Exception\SyntaxException; | ||
use Tivie\HtaccessParser\Parser; | ||
|
||
use const Tivie\HtaccessParser\Token\TOKEN_DIRECTIVE; | ||
|
||
class EnvironmentConfigurator | ||
{ | ||
private string $rootFilePath; | ||
|
||
private string $htaccessPath; | ||
|
||
private string $serverPath; | ||
|
||
public function __construct(string $rootPath = '', private readonly ?Request $request = null) | ||
{ | ||
$this->rootFilePath = $rootPath; | ||
$this->htaccessPath = $this->rootFilePath . '/.htaccess'; | ||
} | ||
|
||
public function getRootFilePath(): string | ||
{ | ||
return $this->rootFilePath; | ||
} | ||
|
||
public function getHtaccessPath(): string | ||
{ | ||
return $this->htaccessPath; | ||
} | ||
|
||
public function getServerPath(): string | ||
{ | ||
return $this->serverPath = $this->request->getPathInfo(); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function getRewriteBase(): string | ||
{ | ||
$file = new SplFileObject($this->htaccessPath); | ||
$parser = new Parser(); | ||
try { | ||
$htaccess = $parser->parse($file); | ||
} catch (SyntaxException $e) { | ||
throw new Exception('Syntax error in .htaccess file: ' . $e->getMessage()); | ||
} catch (\Tivie\HtaccessParser\Exception\Exception $e) { | ||
throw new Exception('Error parsing .htaccess file: ' . $e->getMessage()); | ||
} | ||
$rewriteBase = $htaccess->search('RewriteBase', TOKEN_DIRECTIVE); | ||
|
||
return $rewriteBase->getArguments()[0]; | ||
} | ||
|
||
/** | ||
* Adjusts the RewriteBase in the .htaccess file for the user's environment to avoid errors with controllers. | ||
* Returns true, if the file was successfully changed. | ||
* | ||
* @throws Exception | ||
*/ | ||
public function adjustRewriteBaseHtaccess(): bool | ||
{ | ||
if (!file_exists($this->htaccessPath)) { | ||
throw new Exception(sprintf('The %s file does not exist!', $this->htaccessPath)); | ||
} | ||
|
||
$file = new SplFileObject($this->htaccessPath); | ||
$parser = new Parser(); | ||
try { | ||
$htaccess = $parser->parse($file); | ||
} catch (SyntaxException $e) { | ||
throw new Exception('Syntax error in .htaccess file: ' . $e->getMessage()); | ||
} catch (\Tivie\HtaccessParser\Exception\Exception $e) { | ||
throw new Exception('Error parsing .htaccess file: ' . $e->getMessage()); | ||
} | ||
$rewriteBase = $htaccess->search('RewriteBase', TOKEN_DIRECTIVE); | ||
|
||
$rewriteBase->removeArgument($this->getRewriteBase()); | ||
$rewriteBase->setArguments((array)$this->getServerPath()); | ||
|
||
$output = (string) $htaccess; | ||
return file_put_contents($this->htaccessPath, $output); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
RewriteBase / | ||
RewriteBase /phpmyfaq-test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
tests/phpMyFAQ/FilesystemTest.php → tests/phpMyFAQ/Filesystem/FilesystemTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
|
||
namespace phpMyFAQ; | ||
namespace phpMyFAQ\Filesystem; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace phpMyFAQ\Setup; | ||
|
||
use phpMyFAQ\Core\Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class EnvironmentConfiguratorTest extends TestCase | ||
{ | ||
private EnvironmentConfigurator $configurator; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->configurator = new EnvironmentConfigurator(dirname(__DIR__, 2)); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Request::setTrustedProxies([], -1); | ||
Request::setTrustedHosts([]); | ||
|
||
file_put_contents(dirname(__DIR__, 2) . '/.htaccess', 'RewriteBase /phpmyfaq-test/'); | ||
} | ||
|
||
public function testGetRootFilePath(): void | ||
{ | ||
$this->assertEquals(dirname(__DIR__, 2), $this->configurator->getRootFilePath()); | ||
} | ||
|
||
public function testGetHtaccessPath(): void | ||
{ | ||
$this->assertEquals(dirname(__DIR__, 2) . '/.htaccess', $this->configurator->getHtaccessPath()); | ||
} | ||
|
||
public function testGetServerPath(): void | ||
{ | ||
$request = new Request(); | ||
$server = []; | ||
$server['REQUEST_URI'] = '/'; | ||
$request->initialize([], [], [], [], [], $server); | ||
$configurator = new EnvironmentConfigurator('/path/to', $request); | ||
$this->assertEquals('/', $configurator->getServerPath()); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testGetRewriteBase(): void | ||
{ | ||
$request = new Request(); | ||
$server = []; | ||
$server['REQUEST_URI'] = '/phpmyfaq-test/'; | ||
$server['HTTP_HOST'] = 'https://localhost/phpmyfaq-test/'; | ||
$server['SERVER_NAME'] = 'https://localhost/phpmyfaq-test/'; | ||
$request->initialize([], [], [], [], [], $server); | ||
$configurator = new EnvironmentConfigurator(dirname(__DIR__, 2), $request); | ||
$this->assertEquals('/phpmyfaq-test/', $configurator->getRewriteBase()); | ||
} | ||
|
||
public function testGetServerPathWithSubdirectoryPath(): void | ||
{ | ||
$request = new Request(); | ||
$server = []; | ||
$server['REQUEST_URI'] = '/path/info'; | ||
$request->initialize([], [], [], [], [], $server); | ||
$configurator = new EnvironmentConfigurator('/path/to', $request); | ||
$this->assertEquals('/path/info', $configurator->getServerPath()); | ||
} | ||
|
||
public function testAdjustRewriteBaseHtaccessThrowsExceptionForMissingFile(): void | ||
{ | ||
$configurator = new EnvironmentConfigurator('/path/to'); | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage('The /path/to/.htaccess file does not exist!'); | ||
$configurator->adjustRewriteBaseHtaccess(); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
public function testAdjustRewriteBaseHtaccess(): void | ||
{ | ||
$request = new Request(); | ||
$server = []; | ||
$server['REQUEST_URI'] = '/path/info'; | ||
$request->initialize([], [], [], [], [], $server); | ||
$configurator = new EnvironmentConfigurator(dirname(__DIR__, 2), $request); | ||
$this->assertTrue($configurator->adjustRewriteBaseHtaccess()); | ||
$this->assertEquals('/path/info', $configurator->getRewriteBase()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters