|
| 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 | +} |
0 commit comments