EventDispatcherServiceProvider.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * This file is part of the overtrue/wechat.
  9. *
  10. * (c) overtrue <i@overtrue.me>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. namespace ByteDance\Kernel\Providers;
  16. use Pimple\Container;
  17. use Pimple\ServiceProviderInterface;
  18. use Symfony\Component\EventDispatcher\EventDispatcher;
  19. /**
  20. * Class EventDispatcherServiceProvider.
  21. *
  22. * @author mingyoung <mingyoungcheung@gmail.com>
  23. */
  24. class EventDispatcherServiceProvider implements ServiceProviderInterface
  25. {
  26. /**
  27. * Registers services on the given container.
  28. *
  29. * This method should only be used to configure services and parameters.
  30. * It should not get services.
  31. *
  32. * @param Container $pimple A container instance
  33. */
  34. public function register(Container $pimple)
  35. {
  36. !isset($pimple['events']) && $pimple['events'] = function ($app) {
  37. $dispatcher = new EventDispatcher();
  38. foreach ($app->config->get('events.listen', []) as $event => $listeners) {
  39. foreach ($listeners as $listener) {
  40. $dispatcher->addListener($event, $listener);
  41. }
  42. }
  43. return $dispatcher;
  44. };
  45. }
  46. }