EventDispatcherServiceProvider.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Libs\TikTok\Kernel\Providers;
  11. use Pimple\Container;
  12. use Pimple\ServiceProviderInterface;
  13. use Symfony\Component\EventDispatcher\EventDispatcher;
  14. /**
  15. * Class EventDispatcherServiceProvider.
  16. *
  17. * @author mingyoung <mingyoungcheung@gmail.com>
  18. */
  19. class EventDispatcherServiceProvider implements ServiceProviderInterface {
  20. /**
  21. * Registers services on the given container.
  22. *
  23. * This method should only be used to configure services and parameters.
  24. * It should not get services.
  25. *
  26. * @param Container $pimple A container instance
  27. */
  28. public function register(Container $pimple): void {
  29. !isset($pimple['events']) && $pimple['events'] = static function($app) {
  30. $dispatcher = new EventDispatcher();
  31. foreach ($app->config->get('events.listen', []) as $event => $listeners) {
  32. foreach ($listeners as $listener) {
  33. $dispatcher->addListener($event, $listener);
  34. }
  35. }
  36. return $dispatcher;
  37. };
  38. }
  39. }