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

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