From e4ec71aae53121b506ab309b0f4a5edeb16d1392 Mon Sep 17 00:00:00 2001 From: Christian Fasching Date: Mon, 2 Sep 2019 16:52:39 +0200 Subject: [PATCH] fixed #38 - Feature request: config should follow symfony/pimcore rules for dev/prod --- src/AdvancedObjectSearchBundle.php | 56 +++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/src/AdvancedObjectSearchBundle.php b/src/AdvancedObjectSearchBundle.php index deec19d..374f87f 100644 --- a/src/AdvancedObjectSearchBundle.php +++ b/src/AdvancedObjectSearchBundle.php @@ -16,7 +16,6 @@ namespace AdvancedObjectSearchBundle; use AdvancedObjectSearchBundle\Tools\Installer; -use Elasticsearch\Client; use Pimcore\Extension\Bundle\AbstractPimcoreBundle; use Pimcore\Extension\Bundle\Traits\PackageVersionTrait; @@ -24,6 +23,9 @@ class AdvancedObjectSearchBundle extends AbstractPimcoreBundle { use PackageVersionTrait; + const CONFIG_PATH = 'advancedobjectsearch'; + const CONFIG_FILENAME = 'config.php'; + /** * @var array */ @@ -32,15 +34,53 @@ class AdvancedObjectSearchBundle extends AbstractPimcoreBundle /** * @return array */ - public static function getConfig() { - if(empty(self::$config)) { - $file = PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY . "/advancedobjectsearch/config.php"; - if(file_exists($file)) { - $config = include($file); - } else { + + /** + * @return array|mixed + * @throws \Exception + */ + public static function getConfig() + { + if (empty(self::$config)) { + $pathsToCheck = [ + PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY, + PIMCORE_CONFIGURATION_DIRECTORY, + PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY . '/' . self::CONFIG_PATH, + PIMCORE_CONFIGURATION_DIRECTORY . '/' . self::CONFIG_PATH, + ]; + + $file = null; + + // check for environment configuration + $env = \Pimcore\Config::getEnvironment(); + if ($env) { + $fileExt = \Pimcore\File::getFileExtension(self::CONFIG_FILENAME); + $pureName = str_replace('.' . $fileExt, '', self::CONFIG_FILENAME); + foreach ($pathsToCheck as $path) { + $tmpFile = $path . '/' .$pureName . '_' . $env . '.' . $fileExt; + if (file_exists($tmpFile)) { + $file = $tmpFile; + break; + } + } + } + + //check for config file without environment configuration + if (!$file) { + foreach ($pathsToCheck as $path) { + $tmpFile = $path . '/' . self::CONFIG_FILENAME; + if (file_exists($tmpFile)) { + $file = $tmpFile; + break; + } + } + } + + if (!$file) { throw new \Exception($file . " doesn't exist"); } - self::$config = $config; + + self::$config = include $file; } return self::$config;