FlexCollectionTrait.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Common\Flex
  5. *
  6. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Common\Flex\Traits;
  10. use RocketTheme\Toolbox\Event\Event;
  11. /**
  12. * Trait FlexCollectionTrait
  13. * @package Grav\Common\Flex\Traits
  14. */
  15. trait FlexCollectionTrait
  16. {
  17. use FlexCommonTrait;
  18. /**
  19. * @param string $name
  20. * @param object|null $event
  21. * @return $this
  22. */
  23. public function triggerEvent(string $name, $event = null)
  24. {
  25. if (null === $event) {
  26. $event = new Event([
  27. 'type' => 'flex',
  28. 'directory' => $this->getFlexDirectory(),
  29. 'collection' => $this
  30. ]);
  31. }
  32. if (strpos($name, 'onFlexCollection') !== 0 && strpos($name, 'on') === 0) {
  33. $name = 'onFlexCollection' . substr($name, 2);
  34. }
  35. $container = $this->getContainer();
  36. if ($event instanceof Event) {
  37. $container->fireEvent($name, $event);
  38. } else {
  39. $container->dispatchEvent($event);
  40. }
  41. return $this;
  42. }
  43. }