Config.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @package Grav\Common\Config
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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. class Config extends Data
  15. {
  16. public $environment;
  17. /** @var string */
  18. protected $key;
  19. /** @var string */
  20. protected $checksum;
  21. /** @var int */
  22. protected $timestamp = 0;
  23. /** @var bool */
  24. protected $modified = false;
  25. public function key()
  26. {
  27. if (null === $this->key) {
  28. $this->key = md5($this->checksum . $this->timestamp);
  29. }
  30. return $this->key;
  31. }
  32. public function checksum($checksum = null)
  33. {
  34. if ($checksum !== null) {
  35. $this->checksum = $checksum;
  36. }
  37. return $this->checksum;
  38. }
  39. public function modified($modified = null)
  40. {
  41. if ($modified !== null) {
  42. $this->modified = $modified;
  43. }
  44. return $this->modified;
  45. }
  46. public function timestamp($timestamp = null)
  47. {
  48. if ($timestamp !== null) {
  49. $this->timestamp = $timestamp;
  50. }
  51. return $this->timestamp;
  52. }
  53. public function reload()
  54. {
  55. $grav = Grav::instance();
  56. // Load new configuration.
  57. $config = ConfigServiceProvider::load($grav);
  58. /** @var Debugger $debugger */
  59. $debugger = $grav['debugger'];
  60. if ($config->modified()) {
  61. // Update current configuration.
  62. $this->items = $config->toArray();
  63. $this->checksum($config->checksum());
  64. $this->modified(true);
  65. $debugger->addMessage('Configuration was changed and saved.');
  66. }
  67. return $this;
  68. }
  69. public function debug()
  70. {
  71. /** @var Debugger $debugger */
  72. $debugger = Grav::instance()['debugger'];
  73. $debugger->addMessage('Environment Name: ' . $this->environment);
  74. if ($this->modified()) {
  75. $debugger->addMessage('Configuration reloaded and cached.');
  76. }
  77. }
  78. public function init()
  79. {
  80. $setup = Grav::instance()['setup']->toArray();
  81. foreach ($setup as $key => $value) {
  82. if ($key === 'streams' || !\is_array($value)) {
  83. // Optimized as streams and simple values are fully defined in setup.
  84. $this->items[$key] = $value;
  85. } else {
  86. $this->joinDefaults($key, $value);
  87. }
  88. }
  89. // Legacy value - Override the media.upload_limit based on PHP values
  90. $this->items['system']['media']['upload_limit'] = Utils::getUploadLimit();
  91. }
  92. /**
  93. * @return mixed
  94. * @deprecated 1.5 Use Grav::instance()['languages'] instead.
  95. */
  96. public function getLanguages()
  97. {
  98. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use Grav::instance()[\'languages\'] instead', E_USER_DEPRECATED);
  99. return Grav::instance()['languages'];
  100. }
  101. }