MemberCenterConfig.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace app\common\modules\shop;
  3. class MemberCenterConfig
  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. 'plugin_data' => [
  28. [
  29. 'code' => 'recommend_goods',
  30. 'name' => '推荐商品',
  31. 'sort' => 0,
  32. 'class' => '\app\frontend\modules\goods\services\MemberCenterRecommendGoods'
  33. ],
  34. [
  35. 'code' => 'limitBuy_goods',
  36. 'name' => '限时抢购',
  37. 'sort' => 1,
  38. 'class' => '\app\frontend\modules\goods\services\MemberCenterLimitBuyGoods'
  39. ],
  40. ]
  41. ];
  42. $this->items = $result; //设置了
  43. $plugins = app('plugins')->getEnabledPlugins('*');
  44. foreach ($plugins as $plugin) {
  45. if (method_exists($plugin->app(),'getMemberCenterConfig')) {
  46. $plugin->app()->getMemberCenterConfig(); //执行一下这个方法即可,需要设置的时候执行
  47. }
  48. }
  49. }
  50. protected function getItems()
  51. {
  52. if (!isset($this->items)) {
  53. $this->_getItems();
  54. }
  55. return $this->items;
  56. }
  57. public function getItem($key)
  58. {
  59. return array_get($this->getItems(), $key);
  60. }
  61. public function clear()
  62. {
  63. $this->items = null;
  64. }
  65. public function get($key = null)
  66. {
  67. if (empty($key)) {
  68. return $this->getItems();
  69. }
  70. return $this->getItem($key);
  71. }
  72. public function set($key, $value = null)
  73. {
  74. $items = $this->getItems();
  75. if (is_array($key)) {
  76. foreach ($key as $k => $v) {
  77. array_set($items, $k, $v);
  78. }
  79. } else {
  80. array_set($items, $key, $value);
  81. }
  82. $this->items = $items;
  83. }
  84. public function push($key, $value)
  85. {
  86. $all = $this->getItems();
  87. $array = $this->getItem($key) ?: [];
  88. $array[] = $value;
  89. array_set($all, $key, $array);
  90. $this->items = $all;
  91. }
  92. }