/libraries/src/Event/AbstractEvent.php

https://github.com/Hackwar/joomla-cms · PHP · 172 lines · 63 code · 20 blank · 89 comment · 10 complexity · 07a8b7b64a5c1c16ef9953b0c1ec4dc1 MD5 · raw file

  1. <?php
  2. /**
  3. * Joomla! Content Management System
  4. *
  5. * @copyright (C) 2016 Open Source Matters, Inc. <https://www.joomla.org>
  6. * @license GNU General Public License version 2 or later; see LICENSE
  7. */
  8. namespace Joomla\CMS\Event;
  9. \defined('JPATH_PLATFORM') or die;
  10. use BadMethodCallException;
  11. use Joomla\Event\Event as BaseEvent;
  12. use Joomla\String\Normalise;
  13. /**
  14. * This class implements the base Event object used system-wide to offer orthogonality. Core objects such as Models,
  15. * Controllers, etc create such events on-the-fly and dispatch them through the application's Dispatcher (colloquially
  16. * known as the "Joomla! plugin system"). This way a suitable plugin, typically a "system" plugin, can modify the
  17. * behaviour of any internal class, providing system-wide services such as tags, content versioning, comments or even
  18. * low-level services such as the implementation of created/modified/locked behaviours, record hit counter etc.
  19. *
  20. * You can create a new Event with something like this:
  21. *
  22. * $event = AbstractEvent::create('onModelBeforeSomething', $myModel, $arguments);
  23. *
  24. * You can access the subject object from your event Listener using $event['subject']. It is up to your listener to
  25. * determine whether it should apply its functionality against the subject.
  26. *
  27. * This AbstractEvent class implements a mutable event which is allowed to change its arguments at runtime. This is
  28. * generally unadvisable. It's best to use AbstractImmutableEvent instead and constrict all your interaction to the
  29. * subject class.
  30. *
  31. * @since 4.0.0
  32. */
  33. abstract class AbstractEvent extends BaseEvent
  34. {
  35. /**
  36. * Creates a new CMS event object for a given event name and subject. The following arguments must be given:
  37. * subject object The subject of the event. This is the core object you are going to manipulate.
  38. * eventClass string The Event class name. If you do not provide it Joomla\CMS\Events\<eventNameWithoutOnPrefix>
  39. * will be used.
  40. *
  41. * @param string $eventName The name of the event, e.g. onTableBeforeLoad
  42. * @param array $arguments Additional arguments to pass to the event
  43. *
  44. * @return static
  45. *
  46. * @since 4.0.0
  47. * @throws BadMethodCallException If you do not provide a subject argument
  48. */
  49. public static function create(string $eventName, array $arguments = [])
  50. {
  51. // Get the class name from the arguments, if specified
  52. $eventClassName = '';
  53. if (isset($arguments['eventClass']))
  54. {
  55. $eventClassName = $arguments['eventClass'];
  56. unset($arguments['eventClass']);
  57. }
  58. /**
  59. * If the class name isn't set/found determine it from the event name, e.g. TableBeforeLoadEvent from
  60. * the onTableBeforeLoad event name.
  61. */
  62. if (empty($eventClassName) || !class_exists($eventClassName, true))
  63. {
  64. $bareName = strpos($eventName, 'on') === 0 ? substr($eventName, 2) : $eventName;
  65. $parts = Normalise::fromCamelCase($bareName, true);
  66. $eventClassName = __NAMESPACE__ . '\\' . ucfirst(array_shift($parts)) . '\\';
  67. $eventClassName .= implode('', $parts);
  68. $eventClassName .= 'Event';
  69. }
  70. // Make sure a non-empty subject argument exists and that it is an object
  71. if (!isset($arguments['subject']) || empty($arguments['subject']) || !\is_object($arguments['subject']))
  72. {
  73. throw new BadMethodCallException("No subject given for the $eventName event");
  74. }
  75. // Create and return the event object
  76. if (class_exists($eventClassName, true))
  77. {
  78. return new $eventClassName($eventName, $arguments);
  79. }
  80. return new GenericEvent($eventName, $arguments);
  81. }
  82. /**
  83. * Constructor. Overridden to go through the argument setters.
  84. *
  85. * @param string $name The event name.
  86. * @param array $arguments The event arguments.
  87. *
  88. * @since 4.0.0
  89. */
  90. public function __construct(string $name, array $arguments = [])
  91. {
  92. parent::__construct($name, $arguments);
  93. $this->arguments = [];
  94. foreach ($arguments as $argumentName => $value)
  95. {
  96. $this->setArgument($argumentName, $value);
  97. }
  98. }
  99. /**
  100. * Get an event argument value. It will use a getter method if one exists. The getters have the signature:
  101. *
  102. * get<ArgumentName>($value): mixed
  103. *
  104. * where:
  105. *
  106. * $value is the value currently stored in the $arguments array of the event
  107. * It returns the value to return to the caller.
  108. *
  109. * @param string $name The argument name.
  110. * @param mixed $default The default value if not found.
  111. *
  112. * @return mixed The argument value or the default value.
  113. *
  114. * @since 4.0.0
  115. */
  116. public function getArgument($name, $default = null)
  117. {
  118. $methodName = 'get' . ucfirst($name);
  119. $value = parent::getArgument($name, $default);
  120. if (method_exists($this, $methodName))
  121. {
  122. return $this->{$methodName}($value);
  123. }
  124. return $value;
  125. }
  126. /**
  127. * Add argument to event. It will use a setter method if one exists. The setters have the signature:
  128. *
  129. * set<ArgumentName>($value): mixed
  130. *
  131. * where:
  132. *
  133. * $value is the value being set by the user
  134. * It returns the value to return to set in the $arguments array of the event.
  135. *
  136. * @param string $name Argument name.
  137. * @param mixed $value Value.
  138. *
  139. * @return $this
  140. *
  141. * @since 4.0.0
  142. */
  143. public function setArgument($name, $value)
  144. {
  145. $methodName = 'set' . ucfirst($name);
  146. if (method_exists($this, $methodName))
  147. {
  148. $value = $this->{$methodName}($value);
  149. }
  150. return parent::setArgument($name, $value);
  151. }
  152. }