GetAdsCfgEvent.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace common\events\mall;
  3. use Exception;
  4. use common\enums\EventNameEnum;
  5. use common\components\BaseEvent;
  6. use common\services\mall\BaseAdsSettingService;
  7. use Yii;
  8. /**
  9. * 获取插件配置事件
  10. * @package common\events\mall
  11. */
  12. final class GetAdsCfgEvent extends BaseEvent
  13. {
  14. /**
  15. * 触发事件名称(不可修改)
  16. *
  17. * @var string
  18. */
  19. public $name = EventNameEnum::GET_ADDONS_CONFIG;
  20. /**
  21. * 配置文件
  22. *
  23. * @var array
  24. */
  25. protected $config = [];
  26. /**
  27. * @var int
  28. */
  29. public $mall_id;
  30. /**
  31. * @var array
  32. */
  33. public $listeners = [];
  34. public function __construct(int $mall_id)
  35. {
  36. $this->mall_id = $mall_id;
  37. }
  38. /**
  39. * @param BaseAdsSettingService $service
  40. * @return void
  41. */
  42. public function setService(BaseAdsSettingService $service)
  43. {
  44. $name = preg_replace_callback('/[A-Z][a-z]*/', function ($match) {
  45. return '_' . strtolower($match[0]);
  46. }, lcfirst($service->getAddosName()));
  47. $this->setConfig($name, $service->getSetting());
  48. }
  49. /**
  50. * @param string $addons_name
  51. * @param array $config
  52. * @return void
  53. */
  54. public function setConfig(string $addons_name, array $config = [])
  55. {
  56. $this->config = array_merge(
  57. $this->config, [$addons_name => $config]
  58. );
  59. }
  60. /**
  61. * @return array
  62. */
  63. public function getConfig() : array
  64. {
  65. return $this->config;
  66. }
  67. }