Completed
Pull Request — development (#3246)
by Emanuele
16:38
created

Url_Generator::register()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This class takes care of actually putting together the URL, starting from
5
 * the domain/forum address and appending the query part generated in another
6
 * class.
7
 *
8
 * @name      ElkArte Forum
9
 * @copyright ElkArte Forum contributors
10
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
11
 *
12
 * @version 2.0 dev
13
 *
14
 */
15
16
class Url_Generator
17
{
18
	/**
19
	 * Configuration parameters (for the moment script url and replacements)
20
	 * @var array
21
	 */
22
	protected $_config = array();
23
24
	/**
25
	 * All the objects that create the queries
26
	 * @var array
27
	 */
28
	protected $_generators = array();
29
30
	/**
31
	 * Searching for in replacements
32
	 * @var array
33
	 */
34
	protected $_search = array();
35
36
	/**
37
	 * replacing with in replacements
38
	 * @var array
39
	 */
40
	protected $_replace = array();
41
42
	/**
43
	 * The begin of all
44
	 *
45
	 * @param mixed[] $options
46
	 */
47
	public function __construct($options)
48
	{
49
		Elk_Autoloader::instance()->register(SUBSDIR . '/UrlGenerator', '\\ElkArte\\UrlGenerator');
50
51
		$this->_config = array_merge(array(
52
			'scripturl' => '',
53
			'replacements' => array(),
54
		), $options);
55
56
		$this->register('Standard');
57
		$this->updateReplacements($this->_config['replacements']);
58
	}
59
60
	/**
61
	 * Adds new replacements to the stack.
62
	 *
63
	 * @param string[] $replacements
64
	 */
65
	public function updateReplacements($replacements)
66
	{
67
		$this->_config['replacements'] = array_merge($this->_config['replacements'], $replacements);
68
69
		$this->_search = array_keys($this->_config['replacements']);
70
		$this->_replace = array_values($this->_config['replacements']);
71
	}
72
73
	/**
74
	 * Adds a new UrlGenerator (e.g. standard, semantic, etc.)
75
	 *
76
	 * @param object|string $generator
77
	 */
78
	public function register($generator)
79
	{
80
		$this->_initGen($generator);
81
	}
82
83
	/**
84
	 * Initialized the URL generator (i.e. instantiate the class if needed)
85
	 * and sets the generators according to the types they support.
86
	 *
87
	 * @param object|string $generator
0 ignored issues
show
Bug introduced by
There is no parameter named $generator. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
88
	 */
89
	protected function _initGen($name)
90
	{
91
		if (is_object($name))
92
		{
93
			$generator = $name;
94
		}
95
		else
96
		{
97
			$class = '\\ElkArte\\UrlGenerator\\' . $this->_config['generator'] . '\\' . $name;
98
99
			if (class_exists($class))
100
			{
101
				$generator = new $class();
102
			}
103
		}
104
105
		if (isset($generator))
106
		{
107
			foreach ($generator->getTypes() as $type)
108
			{
109
				$this->_generators[$type] = $generator;
110
			}
111
		}
112
	}
113
114
	/**
115
	 * Takes care of building the URL
116
	 *
117
	 * @param string $type The type of URL we want to build
118
	 * @param mixed[] $params The URL parameters
119
	 *
120
	 * @return string The whole URL
121
	 */
122 5
	public function get($type, $params)
123
	{
124 5
		$url = $this->getQuery($type, $params);
125
126 5
		return $this->_append_base($url);
127
	}
128
129
	/**
130
	 * Almost the same as "get", though it returns only the query.
131
	 * This doesn't append the script URL at the beginning.
132
	 *
133
	 * @param string $type The type of URL we want to build
134
	 * @param mixed[] $params The URL parameters
135
	 *
136
	 * @return string The query part of the URL
137
	 */
138 5
	public function getQuery($type, $params)
139
	{
140 5
		if (isset($this->_generators[$type]) === false)
141
		{
142 5
			$type = 'standard';
143
		}
144
145 5
		return str_replace($this->_search, $this->_replace, $this->_generators[$type]->generate($params));
146
	}
147
148
	/**
149
	 * Append the script URL before the parameters.
150
	 *
151
	 * @param string $args The query part
152
	 *
153
	 * @return string The whole URL
154
	 */
155 5
	protected function _append_base($args)
156
	{
157 5
		if (!empty($args))
158
		{
159 5
			$args = '?' . $args;
160
		}
161
162 5
		return $this->_config['scripturl'] . $args;
163
	}
164
}
165