From 65fcb7d30ec36c235c4e6f08bcec4e15fa0aa97a Mon Sep 17 00:00:00 2001 From: Helmut Hummel Date: Thu, 13 Feb 2020 14:54:25 +0100 Subject: [PATCH] [BUGFIX] Fix exception occurring during upgrade Mitigate upgrade by providing the default template in case none has been set, which happens when the old config object is used during updating the plugin package. Also allow to specify the template path relative to Composer root directory. Fixes: #22 --- src/Config.php | 1 + src/IncludeFile.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Config.php b/src/Config.php index 94ed136..5bb7f65 100644 --- a/src/Config.php +++ b/src/Config.php @@ -72,6 +72,7 @@ public function get($key, $flags = 0) } switch ($key) { case 'env-file': + case 'include-template-file': $val = rtrim($this->process($this->config[$key], $flags), '/\\'); return ($flags & self::RELATIVE_PATHS === 1) ? $val : $this->realpath($val); default: diff --git a/src/IncludeFile.php b/src/IncludeFile.php index ddd2aba..8790e13 100644 --- a/src/IncludeFile.php +++ b/src/IncludeFile.php @@ -52,7 +52,7 @@ public function __construct(Config $config, $loader, $includeFile = '', $include $this->filesystem = $filesystem ?: new Filesystem(); } - public function dump() + public function dump(): bool { $this->filesystem->ensureDirectoryExists(dirname($this->includeFile)); $successfullyWritten = false !== @file_put_contents($this->includeFile, $this->getIncludeFileContent()); @@ -73,10 +73,15 @@ public function dump() * @throws \InvalidArgumentException * @return string */ - private function getIncludeFileContent() + private function getIncludeFileContent(): string { if (!file_exists($this->includeFileTemplate)) { - throw new \RuntimeException('Include file template defined for helhum/dotenv-connector does not exist!', 1581515568); + if (!empty($this->includeFileTemplate)) { + throw new \RuntimeException('Include file template defined for helhum/dotenv-connector does not exist!', 1581515568); + } + // We get here when include file template is empty, which could be a misconfiguration, but more likely happens + // during plugin package upgrades. In this case we provide the default value for smoother upgrades. + $this->includeFileTemplate = __DIR__ . '/../res/PHP/dotenv-include.php.tmpl'; } $envFile = $this->config->get('env-file'); $pathToEnvFileCode = $this->filesystem->findShortestPathCode(