ConfigPathResolver.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Config;
  7. use Magento\Store\Model\ScopeInterface;
  8. /**
  9. * Configures full path for configurations, including scope data and configuration type.
  10. */
  11. class ConfigPathResolver
  12. {
  13. /**
  14. * @var ScopeCodeResolver
  15. */
  16. private $scopeCodeResolver;
  17. /**
  18. * @param ScopeCodeResolver $scopeCodeResolver
  19. */
  20. public function __construct(ScopeCodeResolver $scopeCodeResolver)
  21. {
  22. $this->scopeCodeResolver = $scopeCodeResolver;
  23. }
  24. /**
  25. * Creates full config path for given params.
  26. * If $type variable was provided, it will be used as first part of path.
  27. *
  28. * @param string $path The path of configuration
  29. * @param string $scope The scope of configuration
  30. * @param string|int|null $scopeCode The scope code or its identifier. The values for this
  31. * field are taken from 'store' or 'store_website' tables, depends on $scope value
  32. * @param string|null $type The type of configuration.
  33. * The available types are declared in implementations of Magento\Framework\App\Config\ConfigTypeInterface
  34. * E.g.
  35. * ```php
  36. * const CONFIG_TYPE = 'system';
  37. * ```
  38. * @return string Resolved configuration path
  39. */
  40. public function resolve($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeCode = null, $type = null)
  41. {
  42. $path = trim($path, '/');
  43. $scope = rtrim($scope, 's');
  44. /** Scope name is currently stored in plural form. */
  45. if (in_array($scope, [ScopeInterface::SCOPE_STORE, ScopeInterface::SCOPE_WEBSITE])) {
  46. $scope .= 's';
  47. }
  48. $scopePath = $type ? $type . '/' . $scope : $scope;
  49. if ($scope !== ScopeConfigInterface::SCOPE_TYPE_DEFAULT) {
  50. if (is_numeric($scopeCode) || $scopeCode === null) {
  51. $scopeCode = $this->scopeCodeResolver->resolve($scope, $scopeCode);
  52. }
  53. $scopePath .= '/' . $scopeCode;
  54. }
  55. return $scopePath . ($path ? '/' . $path : '');
  56. }
  57. }