Router::getPageHandlers()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Elgg;
3
4
/**
5
 * Delegates requests to controllers based on the registered configuration.
6
 *
7
 * Plugin devs should use these wrapper functions:
8
 *  * elgg_register_page_handler
9
 *  * elgg_unregister_page_handler
10
 *
11
 * @package    Elgg.Core
12
 * @subpackage Router
13
 * @since      1.9.0
14
 * @access private
15
 */
16
class Router {
17
	private $handlers = array();
18
	private $hooks;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param \Elgg\PluginHooksService $hooks For customized routing.
24
	 */
25 7
	public function __construct(\Elgg\PluginHooksService $hooks) {
26 7
		$this->hooks = $hooks;
27 7
	}
28
29
	/**
30
	 * Routes the request to a registered page handler
31
	 *
32
	 * This function triggers a plugin hook `'route', $identifier` so that plugins can
33
	 * modify the routing or handle a request.
34
	 *
35
	 * @param \Elgg\Http\Request $request The request to handle.
36
	 * @return boolean Whether the request was routed successfully.
37
	 * @access private
38
	 */
39 5
	public function route(\Elgg\Http\Request $request) {
40 5
		$segments = $request->getUrlSegments();
41 5
		if ($segments) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segments of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
42 4
			$identifier = array_shift($segments);
43 4
		} else {
44 1
			$identifier = '';
45
46
			// this plugin hook is deprecated. Use elgg_register_page_handler()
47
			// to register for the '' (empty string) handler.
48
			// allow plugins to override the front page (return true to indicate
49
			// that the front page has been served)
50 1
			$result = _elgg_services()->hooks->trigger('index', 'system', null, false);
51 1
			if ($result === true) {
52
				elgg_deprecated_notice("The 'index', 'system' plugin has been deprecated. See elgg_front_page_handler()", 1.9);
53
				exit;
54
			}
55
		}
56
57
		// return false to stop processing the request (because you handled it)
58
		// return a new $result array if you want to route the request differently
59
		$result = array(
60 5
			'identifier' => $identifier,
61 5
			'handler' => $identifier, // backward compatibility
62 5
			'segments' => $segments,
63 5
		);
64 5
		$result = $this->hooks->trigger('route', $identifier, $result, $result);
65 5
		if ($result === false) {
66 1
			return true;
67
		}
68
69 4
		if ($identifier != $result['identifier']) {
70 1
			$identifier = $result['identifier'];
71 4
		} else if ($identifier != $result['handler']) {
72 1
			$identifier = $result['handler'];
73 1
		}
74
75 4
		$segments = $result['segments'];
76
77 4
		$handled = false;
78 4
		if (isset($this->handlers[$identifier]) && is_callable($this->handlers[$identifier])) {
79 3
			$function = $this->handlers[$identifier];
80 3
			$handled = call_user_func($function, $segments, $identifier);
81 3
		}
82
83 4
		return $handled || headers_sent();
84
	}
85
86
	/**
87
	 * Register a function that gets called when the first part of a URL is
88
	 * equal to the identifier.
89
	 *
90
	 * @param string $identifier The page type to handle
91
	 * @param string $function   Your function name
92
	 *
93
	 * @return bool Depending on success
94
	 */
95 6
	public function registerPageHandler($identifier, $function) {
96 6
		if (is_callable($function, true)) {
97 5
			$this->handlers[$identifier] = $function;
98 5
			return true;
99
		}
100
101 1
		return false;
102
	}
103
104
	/**
105
	 * Unregister a page handler for an identifier
106
	 *
107
	 * @param string $identifier The page type identifier
108
	 *
109
	 * @return void
110
	 */
111 1
	public function unregisterPageHandler($identifier) {
112 1
		unset($this->handlers[$identifier]);
113 1
	}
114
115
	/**
116
	 * Get page handlers as array of identifier => callback
117
	 * 
118
	 * @return array
119
	 */
120 1
	public function getPageHandlers() {
121 1
		return $this->handlers;
122
	}
123
}
124
125