vendor/pimcore/pimcore/lib/Config.php line 203

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore;
  15. use Pimcore\Cache\Runtime;
  16. use Pimcore\Config\Config as PimcoreConfig;
  17. use Pimcore\Config\ReportConfigWriter;
  18. use Pimcore\Model\Element\ElementInterface;
  19. use Pimcore\Model\Tool\SettingsStore;
  20. use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
  21. use Symfony\Component\Yaml\Yaml;
  22. final class Config implements \ArrayAccess
  23. {
  24.     /**
  25.      * @var array
  26.      */
  27.     protected static $configFileCache = [];
  28.     /**
  29.      * @var string
  30.      */
  31.     protected static $environment = null;
  32.     /**
  33.      * @var array|null
  34.      */
  35.     protected static $systemConfig = null;
  36.     /**
  37.      * @param  mixed $offset
  38.      *
  39.      * @return bool
  40.      */
  41.     #[\ReturnTypeWillChange]
  42.     public function offsetExists($offset)
  43.     {
  44.         return self::getSystemConfiguration($offset) !== null;
  45.     }
  46.     /**
  47.      * @param  mixed $offset
  48.      * @param  mixed $value
  49.      *
  50.      * @throws \Exception
  51.      */
  52.     #[\ReturnTypeWillChange]
  53.     public function offsetSet($offset, $value)
  54.     {
  55.         throw new \Exception("modifying the config isn't allowed");
  56.     }
  57.     /**
  58.      * @param  mixed $offset
  59.      *
  60.      * @throws \Exception
  61.      */
  62.     #[\ReturnTypeWillChange]
  63.     public function offsetUnset($offset)
  64.     {
  65.         throw new \Exception("modifying the config isn't allowed");
  66.     }
  67.     /**
  68.      * @param string $offset
  69.      *
  70.      * @return array|null
  71.      */
  72.     #[\ReturnTypeWillChange]
  73.     public function offsetGet($offset)
  74.     {
  75.         return self::getSystemConfiguration($offset);
  76.     }
  77.     /**
  78.      * @internal
  79.      *
  80.      * @param string $name - name of configuration file. slash is allowed for subdirectories.
  81.      *
  82.      * @return string
  83.      */
  84.     public static function locateConfigFile(string $name): string
  85.     {
  86.         if (!isset(self::$configFileCache[$name])) {
  87.             $pathsToCheck = [
  88.                 PIMCORE_CUSTOM_CONFIGURATION_DIRECTORY,
  89.                 PIMCORE_CONFIGURATION_DIRECTORY,
  90.             ];
  91.             $file = null;
  92.             // check for environment configuration
  93.             $env = self::getEnvironment();
  94.             if ($env) {
  95.                 $fileExt = File::getFileExtension($name);
  96.                 $pureName = str_replace('.' . $fileExt, '', $name);
  97.                 foreach ($pathsToCheck as $path) {
  98.                     $tmpFile = $path . '/' . $pureName . '_' . $env . '.' . $fileExt;
  99.                     if (file_exists($tmpFile)) {
  100.                         $file = $tmpFile;
  101.                         break;
  102.                     }
  103.                 }
  104.             }
  105.             //check for config file without environment configuration
  106.             if (!$file) {
  107.                 foreach ($pathsToCheck as $path) {
  108.                     $tmpFile = $path . '/' . $name;
  109.                     if (file_exists($tmpFile)) {
  110.                         $file = $tmpFile;
  111.                         break;
  112.                     }
  113.                 }
  114.             }
  115.             //get default path in pimcore configuration directory
  116.             if (!$file) {
  117.                 $file = PIMCORE_CONFIGURATION_DIRECTORY . '/' . $name;
  118.             }
  119.             self::$configFileCache[$name] = $file;
  120.         }
  121.         return self::$configFileCache[$name];
  122.     }
  123.     /**
  124.      * @internal ONLY FOR TESTING PURPOSES IF NEEDED FOR SPECIFIC TEST CASES
  125.      *
  126.      * @param null|array $configuration
  127.      * @param null|mixed $offset
  128.      */
  129.     public static function setSystemConfiguration($configuration, $offset = null)
  130.     {
  131.         if (null !== $offset) {
  132.             self::getSystemConfiguration();
  133.             static::$systemConfig[$offset] = $configuration;
  134.         } else {
  135.             static::$systemConfig = $configuration;
  136.         }
  137.     }
  138.     /**
  139.      * @internal
  140.      *
  141.      * @param null|mixed $offset
  142.      *
  143.      * @return null|array
  144.      */
  145.     public static function getSystemConfiguration($offset = null)
  146.     {
  147.         if (null === static::$systemConfig && $container = \Pimcore::getContainer()) {
  148.             $config = $container->getParameter('pimcore.config');
  149.             $adminConfig = $container->getParameter('pimcore_admin.config');
  150.             static::$systemConfig = array_merge_recursive($config, $adminConfig);
  151.         }
  152.         if (null !== $offset) {
  153.             return static::$systemConfig[$offset] ?? null;
  154.         }
  155.         return static::$systemConfig;
  156.     }
  157.     /**
  158.      * @internal
  159.      *
  160.      * @param string|null $languange
  161.      *
  162.      * @return string
  163.      */
  164.     public static function getWebsiteConfigRuntimeCacheKey($languange = null)
  165.     {
  166.         $cacheKey = 'pimcore_config_website';
  167.         if ($languange) {
  168.             $cacheKey .= '_' . $languange;
  169.         }
  170.         return $cacheKey;
  171.     }
  172.     /**
  173.      * @static
  174.      *
  175.      * @return mixed|\Pimcore\Config\Config
  176.      */
  177.     public static function getWebsiteConfig($language = null)
  178.     {
  179.         if (\Pimcore\Cache\Runtime::isRegistered(self::getWebsiteConfigRuntimeCacheKey($language))) {
  180.             $config = \Pimcore\Cache\Runtime::get(self::getWebsiteConfigRuntimeCacheKey($language));
  181.         } else {
  182.             $cacheKey = 'website_config';
  183.             if ($language) {
  184.                 $cacheKey .= '_' . $language;
  185.             }
  186.             $siteId = null;
  187.             if (Model\Site::isSiteRequest()) {
  188.                 $siteId = Model\Site::getCurrentSite()->getId();
  189.             } elseif (Tool::isFrontendRequestByAdmin()) {
  190.                 // this is necessary to set the correct settings in editmode/preview (using the main domain)
  191.                 // we cannot use the document resolver service here, because we need the document on the master request
  192.                 $originDocument = \Pimcore::getContainer()->get('request_stack')->getMainRequest()->get(DynamicRouter::CONTENT_KEY);
  193.                 if ($originDocument) {
  194.                     $site = Tool\Frontend::getSiteForDocument($originDocument);
  195.                     if ($site) {
  196.                         $siteId = $site->getId();
  197.                     }
  198.                 }
  199.             }
  200.             if ($siteId) {
  201.                 $cacheKey = $cacheKey . '_site_' . $siteId;
  202.             }
  203.             /** @var \Pimcore\Config\Config|null $config */
  204.             $config = Cache::load($cacheKey);
  205.             if (!$config) {
  206.                 $settingsArray = [];
  207.                 $cacheTags = ['website_config', 'system', 'config', 'output'];
  208.                 $list = new Model\WebsiteSetting\Listing();
  209.                 $list = $list->load();
  210.                 foreach ($list as $item) {
  211.                     $itemSiteId = $item->getSiteId();
  212.                     if ($itemSiteId && $itemSiteId !== $siteId) {
  213.                         continue;
  214.                     }
  215.                     $itemLanguage = $item->getLanguage();
  216.                     if ($itemLanguage && $language !== $itemLanguage) {
  217.                         continue;
  218.                     }
  219.                     $key = $item->getName();
  220.                     if (!$itemLanguage && isset($settingsArray[$key])) {
  221.                         continue;
  222.                     }
  223.                     switch ($item->getType()) {
  224.                         case 'document':
  225.                         case 'asset':
  226.                         case 'object':
  227.                             $s = $item->getData();
  228.                             break;
  229.                         case 'bool':
  230.                             $s = (bool) $item->getData();
  231.                             break;
  232.                         case 'text':
  233.                             $s = (string) $item->getData();
  234.                             break;
  235.                         default:
  236.                             $s = null;
  237.                             break;
  238.                     }
  239.                     if ($s instanceof Model\Element\ElementInterface) {
  240.                         $cacheTags = $s->getCacheTags($cacheTags);
  241.                     }
  242.                     if (isset($s)) {
  243.                         $settingsArray[$key] = $s;
  244.                     }
  245.                 }
  246.                 //TODO resolve for all langs, current lang first, then no lang
  247.                 $config = new PimcoreConfig($settingsArray, true);
  248.                 Cache::save($config, $cacheKey, $cacheTags, null, 998);
  249.             } elseif ($config instanceof PimcoreConfig) {
  250.                 $data = $config->toArray();
  251.                 foreach ($data as $key => $setting) {
  252.                     if ($setting instanceof ElementInterface) {
  253.                         $elementCacheKey = $setting->getCacheTag();
  254.                         if (!Runtime::isRegistered($elementCacheKey)) {
  255.                             Runtime::set($elementCacheKey, $setting);
  256.                         }
  257.                     }
  258.                 }
  259.             }
  260.             self::setWebsiteConfig($config, $language);
  261.         }
  262.         return $config;
  263.     }
  264.     /**
  265.      * @internal
  266.      *
  267.      * @param Config\Config|null $config
  268.      * @param string|null $language
  269.      */
  270.     public static function setWebsiteConfig(?PimcoreConfig $config, $language = null)
  271.     {
  272.         \Pimcore\Cache\Runtime::set(self::getWebsiteConfigRuntimeCacheKey($language), $config);
  273.     }
  274.     /**
  275.      * Returns whole website config or only a given setting for the current site
  276.      *
  277.      * @param null|mixed $key       Config key to directly load. If null, the whole config will be returned
  278.      * @param null|mixed $default   Default value to use if the key is not set
  279.      * @param null|string $language   Language
  280.      *
  281.      * @return Config\Config|mixed
  282.      */
  283.     public static function getWebsiteConfigValue($key = null, $default = null, $language = null)
  284.     {
  285.         $config = self::getWebsiteConfig($language);
  286.         if (null !== $key) {
  287.             return $config->get($key, $default);
  288.         }
  289.         return $config;
  290.     }
  291.     private static function getArrayValue($keys, $array)
  292.     {
  293.         $len = count($keys);
  294.         $pointer = $array;
  295.         for ($i = 0; $i < $len; $i++) {
  296.             $key = $keys[$i];
  297.             if (array_key_exists($key, $pointer)) {
  298.                 $pointer = $pointer[$key];
  299.             } else {
  300.                 return null;
  301.             }
  302.         }
  303.         return $pointer;
  304.     }
  305.     /**
  306.      * @return PimcoreConfig
  307.      *
  308.      * @throws \Exception
  309.      *
  310.      * @internal
  311.      * @static
  312.      */
  313.     public static function getReportConfig(): PimcoreConfig
  314.     {
  315.         $config = null;
  316.         if (Runtime::isRegistered('pimcore_config_report')) {
  317.             $config = Runtime::get('pimcore_config_report');
  318.         } else {
  319.             try {
  320.                 $configJson = SettingsStore::get(
  321.                     ReportConfigWriter::REPORT_SETTING_ID, ReportConfigWriter::REPORT_SETTING_SCOPE
  322.                 );
  323.                 if ($configJson) {
  324.                     $configArray = json_decode($configJson->getData(), true);
  325.                     $config = new PimcoreConfig($configArray);
  326.                 }
  327.             } catch (\Exception $e) {
  328.                 // nothing to do
  329.             }
  330.         }
  331.         if (!$config) {
  332.             $config = new PimcoreConfig([]);
  333.         }
  334.         self::setReportConfig($config);
  335.         return $config;
  336.     }
  337.     /**
  338.      * @internal
  339.      * @static
  340.      *
  341.      * @param \Pimcore\Config\Config $config
  342.      */
  343.     public static function setReportConfig(\Pimcore\Config\Config $config)
  344.     {
  345.         \Pimcore\Cache\Runtime::set('pimcore_config_report', $config);
  346.     }
  347.     /**
  348.      * @static
  349.      *
  350.      * @return \Pimcore\Config\Config
  351.      *
  352.      * @internal
  353.      */
  354.     public static function getRobotsConfig()
  355.     {
  356.         if (\Pimcore\Cache\Runtime::isRegistered('pimcore_config_robots')) {
  357.             $config = \Pimcore\Cache\Runtime::get('pimcore_config_robots');
  358.         } else {
  359.             try {
  360.                 $settingsStoreScope = 'robots.txt';
  361.                 $configData = [];
  362.                 $robotsSettingsIds = SettingsStore::getIdsByScope($settingsStoreScope);
  363.                 foreach ($robotsSettingsIds as $id) {
  364.                     $robots = SettingsStore::get($id, $settingsStoreScope);
  365.                     $siteId = \preg_replace('/^robots\.txt\-/', '', $robots->getId());
  366.                     $configData[$siteId] = $robots->getData();
  367.                 }
  368.                 $config = new \Pimcore\Config\Config($configData);
  369.             } catch (\Exception $e) {
  370.                 $config = new \Pimcore\Config\Config([]);
  371.             }
  372.             self::setRobotsConfig($config);
  373.         }
  374.         return $config;
  375.     }
  376.     /**
  377.      * @static
  378.      *
  379.      * @param \Pimcore\Config\Config $config
  380.      *
  381.      * @internal
  382.      */
  383.     public static function setRobotsConfig(\Pimcore\Config\Config $config)
  384.     {
  385.         \Pimcore\Cache\Runtime::set('pimcore_config_robots', $config);
  386.     }
  387.     /**
  388.      * @internal
  389.      * @static
  390.      *
  391.      * @return \Pimcore\Config\Config
  392.      */
  393.     public static function getWeb2PrintConfig()
  394.     {
  395.         if (\Pimcore\Cache\Runtime::isRegistered('pimcore_config_web2print')) {
  396.             $config = \Pimcore\Cache\Runtime::get('pimcore_config_web2print');
  397.         } else {
  398.             $config = \Pimcore\Web2Print\Config::get();
  399.             self::setWeb2PrintConfig($config);
  400.         }
  401.         return $config;
  402.     }
  403.     /**
  404.      * @internal
  405.      * @static
  406.      *
  407.      * @param \Pimcore\Config\Config $config
  408.      */
  409.     public static function setWeb2PrintConfig(\Pimcore\Config\Config $config)
  410.     {
  411.         \Pimcore\Cache\Runtime::set('pimcore_config_web2print', $config);
  412.     }
  413.     /**
  414.      * @internal
  415.      * @static
  416.      *
  417.      * @param \Pimcore\Config\Config $config
  418.      */
  419.     public static function setModelClassMappingConfig($config)
  420.     {
  421.         \Pimcore\Cache\Runtime::set('pimcore_config_model_classmapping', $config);
  422.     }
  423.     /**
  424.      * @internal
  425.      *
  426.      * @param array $runtimeConfig
  427.      * @param string $key
  428.      *
  429.      * @return bool
  430.      */
  431.     public static function inPerspective($runtimeConfig, $key)
  432.     {
  433.         if (!isset($runtimeConfig['toolbar']) || !$runtimeConfig['toolbar']) {
  434.             return true;
  435.         }
  436.         $parts = explode('.', $key);
  437.         $menuItems = $runtimeConfig['toolbar'];
  438.         for ($i = 0; $i < count($parts); $i++) {
  439.             $part = $parts[$i];
  440.             if (!isset($menuItems[$part])) {
  441.                 break;
  442.             }
  443.             $menuItem = $menuItems[$part];
  444.             if (is_array($menuItem)) {
  445.                 if (isset($menuItem['hidden']) && $menuItem['hidden']) {
  446.                     return false;
  447.                 }
  448.                 if (!($menuItem['items'] ?? null)) {
  449.                     break;
  450.                 }
  451.                 $menuItems = $menuItem['items'];
  452.             } else {
  453.                 return $menuItem;
  454.             }
  455.         }
  456.         return true;
  457.     }
  458.     /**
  459.      * @return string
  460.      */
  461.     public static function getEnvironment(): string
  462.     {
  463.         return $_SERVER['APP_ENV'];
  464.     }
  465.     /**
  466.      * @internal
  467.      *
  468.      * @param string $file
  469.      * @param bool $asArray
  470.      *
  471.      * @return null|Config\Config|array
  472.      *
  473.      * @throws \Exception
  474.      */
  475.     public static function getConfigInstance($file, bool $asArray = false)
  476.     {
  477.         $fileType = pathinfo($file, PATHINFO_EXTENSION);
  478.         if (file_exists($file)) {
  479.             if ($fileType == 'yml') {
  480.                 $content = Yaml::parseFile($file);
  481.             } else {
  482.                 $content = include($file);
  483.             }
  484.             if (is_array($content)) {
  485.                 if ($asArray) {
  486.                     return $content;
  487.                 }
  488.                 return new \Pimcore\Config\Config($content);
  489.             }
  490.         } else {
  491.             throw new \Exception($file . " doesn't exist");
  492.         }
  493.         throw new \Exception($file . ' is invalid');
  494.     }
  495. }