PluginsConfig.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\common\modules\shop;
  3. class PluginsConfig
  4. {
  5. /**
  6. * @var self
  7. */
  8. static $current;
  9. protected $items;
  10. /**
  11. * constructor.
  12. */
  13. public function __construct()
  14. {
  15. self::$current = $this;
  16. }
  17. static public function current()
  18. {
  19. if (!isset(self::$current)) {
  20. return new static();
  21. }
  22. return self::$current;
  23. }
  24. protected function _getItems()
  25. {
  26. $result = [];
  27. $plugins = app('plugins')->getEnabledPlugins('*');
  28. foreach ($plugins as $plugin) {
  29. foreach ($plugin->app()->getPluginConfigItems() as $key => $item) {
  30. array_set($result, $key, $item);
  31. }
  32. }
  33. return $result;
  34. }
  35. protected function getItems()
  36. {
  37. if (!isset($this->items)) {
  38. $this->items = $this->_getItems();
  39. }
  40. return $this->items;
  41. }
  42. public function getItem($key)
  43. {
  44. return array_get($this->getItems(), $key);
  45. }
  46. public function clear()
  47. {
  48. $this->items = null;
  49. }
  50. public function get($key = null)
  51. {
  52. if (empty($key)) {
  53. return $this->getItems();
  54. }
  55. return $this->getItem($key);
  56. }
  57. public function set($key, $value = null)
  58. {
  59. $items = $this->getItems();
  60. if (is_array($key)) {
  61. foreach ($key as $k => $v) {
  62. array_set($items, $k, $v);
  63. }
  64. } else {
  65. array_set($items, $key, $value);
  66. }
  67. $this->items = $items;
  68. }
  69. public function push($key, $value)
  70. {
  71. $all = $this->getItems();
  72. $array = $this->getItem($key) ?: [];
  73. $array[] = $value;
  74. array_set($all, $key, $array);
  75. $this->items = $all;
  76. }
  77. }