Config.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @package Grav\Common\Config
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Config;
  9. use Grav\Common\Debugger;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Data\Data;
  12. use Grav\Common\Service\ConfigServiceProvider;
  13. use Grav\Common\Utils;
  14. use function is_array;
  15. /**
  16. * Class Config
  17. * @package Grav\Common\Config
  18. */
  19. class Config extends Data
  20. {
  21. /** @var string */
  22. public $environment;
  23. /** @var string */
  24. protected $key;
  25. /** @var string */
  26. protected $checksum;
  27. /** @var int */
  28. protected $timestamp = 0;
  29. /** @var bool */
  30. protected $modified = false;
  31. /**
  32. * @return string
  33. */
  34. public function key()
  35. {
  36. if (null === $this->key) {
  37. $this->key = md5($this->checksum . $this->timestamp);
  38. }
  39. return $this->key;
  40. }
  41. /**
  42. * @param string|null $checksum
  43. * @return string|null
  44. */
  45. public function checksum($checksum = null)
  46. {
  47. if ($checksum !== null) {
  48. $this->checksum = $checksum;
  49. }
  50. return $this->checksum;
  51. }
  52. /**
  53. * @param bool|null $modified
  54. * @return bool
  55. */
  56. public function modified($modified = null)
  57. {
  58. if ($modified !== null) {
  59. $this->modified = $modified;
  60. }
  61. return $this->modified;
  62. }
  63. /**
  64. * @param int|null $timestamp
  65. * @return int
  66. */
  67. public function timestamp($timestamp = null)
  68. {
  69. if ($timestamp !== null) {
  70. $this->timestamp = $timestamp;
  71. }
  72. return $this->timestamp;
  73. }
  74. /**
  75. * @return $this
  76. */
  77. public function reload()
  78. {
  79. $grav = Grav::instance();
  80. // Load new configuration.
  81. $config = ConfigServiceProvider::load($grav);
  82. /** @var Debugger $debugger */
  83. $debugger = $grav['debugger'];
  84. if ($config->modified()) {
  85. // Update current configuration.
  86. $this->items = $config->toArray();
  87. $this->checksum($config->checksum());
  88. $this->modified(true);
  89. $debugger->addMessage('Configuration was changed and saved.');
  90. }
  91. return $this;
  92. }
  93. /**
  94. * @return void
  95. */
  96. public function debug()
  97. {
  98. /** @var Debugger $debugger */
  99. $debugger = Grav::instance()['debugger'];
  100. $debugger->addMessage('Environment Name: ' . $this->environment);
  101. if ($this->modified()) {
  102. $debugger->addMessage('Configuration reloaded and cached.');
  103. }
  104. }
  105. /**
  106. * @return void
  107. */
  108. public function init()
  109. {
  110. $setup = Grav::instance()['setup']->toArray();
  111. foreach ($setup as $key => $value) {
  112. if ($key === 'streams' || !is_array($value)) {
  113. // Optimized as streams and simple values are fully defined in setup.
  114. $this->items[$key] = $value;
  115. } else {
  116. $this->joinDefaults($key, $value);
  117. }
  118. }
  119. // Legacy value - Override the media.upload_limit based on PHP values
  120. $this->items['system']['media']['upload_limit'] = Utils::getUploadLimit();
  121. }
  122. /**
  123. * @return mixed
  124. * @deprecated 1.5 Use Grav::instance()['languages'] instead.
  125. */
  126. public function getLanguages()
  127. {
  128. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use Grav::instance()[\'languages\'] instead', E_USER_DEPRECATED);
  129. return Grav::instance()['languages'];
  130. }
  131. }