EventDispatcherServiceProvider.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 EasyWeChat\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. /**
  22. * Registers services on the given container.
  23. *
  24. * This method should only be used to configure services and parameters.
  25. * It should not get services.
  26. *
  27. * @param Container $pimple A container instance
  28. */
  29. public function register(Container $pimple)
  30. {
  31. $pimple['events'] = function ($app) {
  32. $dispatcher = new EventDispatcher();
  33. foreach ($app->config->get('events.listen', []) as $event => $listeners) {
  34. foreach ($listeners as $listener) {
  35. $dispatcher->addListener($event, $listener);
  36. }
  37. }
  38. return $dispatcher;
  39. };
  40. }
  41. }