Test Failed
Push — master ( 8c47c2...3acf9f )
by Steve
12:37
created

engine/classes/Elgg/EventsService.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elgg;
3
4
use Elgg\HooksRegistrationService\Hook;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Elgg\Hook.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Elgg\HooksRegistrationService\Event;
6
7
/**
8
 * Service for Events
9
 *
10
 * @access private
11
 *
12
 * @package    Elgg.Core
13
 * @subpackage Hooks
14
 * @since      1.9.0
15
 */
16
class EventsService extends HooksRegistrationService {
17
	use Profilable;
18
19
	const OPTION_STOPPABLE = 'stoppable';
20
	const OPTION_DEPRECATION_MESSAGE = 'deprecation_message';
21
	const OPTION_DEPRECATION_VERSION = 'deprecation_version';
22
23
	/**
24
	 * {@inheritdoc}
25
	 */
26 113
	public function registerHandler($name, $type, $callback, $priority = 500) {
27 113
		if (in_array($type, ['member', 'friend', 'attached'])
28 113
				&& in_array($name, ['create', 'update', 'delete'])) {
29
			_elgg_services()->logger->error("'$name, $type' event is no longer triggered. "
30
				. "Update your event registration to use '$name, relationship'");
31
		}
32
33 113
		return parent::registerHandler($name, $type, $callback, $priority);
34
	}
35
36
	/**
37
	 * Triggers an Elgg event.
38
	 *
39
	 * @see elgg_trigger_event
40
	 * @see elgg_trigger_after_event
41
	 * @access private
42
	 */
43 202
	public function trigger($name, $type, $object = null, array $options = []) {
44 202
		$options = array_merge([
45 202
			self::OPTION_STOPPABLE => true,
46 202
			self::OPTION_DEPRECATION_MESSAGE => '',
47 202
			self::OPTION_DEPRECATION_VERSION => '',
48 202
		], $options);
49
50 202
		$handlers = $this->hasHandler($name, $type);
51 202
		if ($handlers && $options[self::OPTION_DEPRECATION_MESSAGE]) {
52
			_elgg_services()->deprecation->sendNotice(
53
				$options[self::OPTION_DEPRECATION_MESSAGE],
54
				$options[self::OPTION_DEPRECATION_VERSION],
55
				2
56
			);
57
		}
58
59 202
		$handlers = $this->getOrderedHandlers($name, $type);
60 202
		$handler_svc = _elgg_services()->handlers;
61
62
		// This starts as a string, but if a handler type-hints an object we convert it on-demand inside
63
		// \Elgg\HandlersService::call and keep it alive during all handler calls. We do this because
64
		// creating objects for every triggering is expensive.
65 202
		$event = 'event';
66
		/* @var Event|string */
67
68 202
		foreach ($handlers as $handler) {
69 8
			$handler_description = false;
70 8
			if ($this->timer && $type === 'system' && $name !== 'shutdown') {
71
				$handler_description = $handler_svc->describeCallable($handler) . "()";
72
				$this->timer->begin(["[$name,$type]", $handler_description]);
73
			}
74
75 8
			list($success, $return, $event) = $handler_svc->call($handler, $event, [$name, $type, $object]);
76
77 8
			if ($handler_description) {
78
				$this->timer->end(["[$name,$type]", $handler_description]);
79
			}
80
81 8
			if (!$success) {
82 1
				continue;
83
			}
84
85 7
			if (!empty($options[self::OPTION_STOPPABLE]) && ($return === false)) {
86 7
				return false;
87
			}
88
		}
89
90 199
		return true;
91
	}
92
93
	/**
94
	 * Trigger a "Before event" indicating a process is about to begin.
95
	 *
96
	 * Like regular events, a handler returning false will cancel the process and false
97
	 * will be returned.
98
	 *
99
	 * To register for a before event, append ":before" to the event name when registering.
100
	 *
101
	 * @param string $event       The event type. The fired event type will be appended with ":before".
102
	 * @param string $object_type The object type
103
	 * @param string $object      The object involved in the event
104
	 *
105
	 * @return bool False if any handler returned false, otherwise true
106
	 *
107
	 * @see trigger
108
	 * @see triggerAfter
109
	 * @since 2.0.0
110
	 */
111 85
	function triggerBefore($event, $object_type, $object = null) {
112 85
		return $this->trigger("$event:before", $object_type, $object);
113
	}
114
115
	/**
116
	 * Trigger an "After event" indicating a process has finished.
117
	 *
118
	 * Unlike regular events, all the handlers will be called, their return values ignored.
119
	 *
120
	 * To register for an after event, append ":after" to the event name when registering.
121
	 *
122
	 * @param string $event       The event type. The fired event type will be appended with ":after".
123
	 * @param string $object_type The object type
124
	 * @param string $object      The object involved in the event
125
	 *
126
	 * @return true
127
	 *
128
	 * @see triggerBefore
129
	 * @since 2.0.0
130
	 */
131 88
	public function triggerAfter($event, $object_type, $object = null) {
132
		$options = [
133 88
			self::OPTION_STOPPABLE => false,
134
		];
135 88
		return $this->trigger("$event:after", $object_type, $object, $options);
136
	}
137
138
	/**
139
	 * Trigger an event normally, but send a notice about deprecated use if any handlers are registered.
140
	 *
141
	 * @param string $event       The event type
142
	 * @param string $object_type The object type
143
	 * @param string $object      The object involved in the event
144
	 * @param string $message     The deprecation message
145
	 * @param string $version     Human-readable *release* version: 1.9, 1.10, ...
146
	 *
147
	 * @return bool
148
	 *
149
	 * @see trigger
150
	 */
151
	function triggerDeprecated($event, $object_type, $object = null, $message = null, $version = null) {
152
		$options = [
153
			self::OPTION_DEPRECATION_MESSAGE => $message,
154
			self::OPTION_DEPRECATION_VERSION => $version,
155
		];
156
		return $this->trigger($event, $object_type, $object, $options);
157
	}
158
}
159