ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ILIAS\UI\URLBuilder Class Reference

URLBuilder. More...

+ Collaboration diagram for ILIAS\UI\URLBuilder:

Public Member Functions

 __construct (URI $uri)
 
 withURI (URI $uri)
 Changes the base URI of the Builder. More...
 
 withFragment (?string $fragment)
 Change the fragment/hash part of the URL. More...
 
 acquireParameter (array $namespace, string $name, ?string $initial_value=null)
 Add a new parameter with a namespace and get its token for subsequent changes. More...
 
 acquireParameters (array $namespace, string ... $names)
 
 deleteParameter (URLBuilderToken $token)
 Delete an acquired parameter if the supplied token is valid. More...
 
 withParameter (URLBuilderToken $token, string|array $value)
 Change an acquired parameter's value if the supplied token is valid. More...
 
 renderTokens (array $tokens)
 Renders a Javascript Map of all given tokens. More...
 
 renderObject (array $tokens)
 Renders a Javascript URLBuilder object with changeable parameters for all given tokens. More...
 
 buildURI ()
 Get a URI representation of the full URL including query string and fragment/hash. More...
 

Data Fields

const URL_MAX_LENGTH = 8192
 A maximum length of 8192 characters should be safe to use in most browsers, even though longer URLs will be supported by some. More...
 
const SEPARATOR = '_'
 Separator for parts of a parameter's namespace. More...
 

Private Member Functions

 buildQuery ()
 Create the query part of the URL from all parameters Claimed parameters overwrite base parameters in array_merge(), numeric indizes of array-parameters are being removed to ensure continous numeration (p[1]=A&p[2]=B --> p[]=A&p[]=B). More...
 
 buildFragment ()
 Create the fragment/hash part of the URL. More...
 
 parameterExists (string $name)
 Check if parameter was already acquired. More...
 
 checkToken (URLBuilderToken $token)
 Check if a token is valid. More...
 
 checkLength (URI $uri)
 Check the full length of the URI against URL_MAX_LENGTH. More...
 

Private Attributes

URI $uri
 Base URI for the URLBuilder. More...
 
string $fragment = null
 Stores the URL fragment/hash (#) (always changeable due to its usage) More...
 
array $params = []
 Stores all acquired parameters These always take precedence over existing parameters in the base URI. More...
 
array $tokens = []
 Stores all generated tokens for acquired parameters. More...
 

Detailed Description

URLBuilder.

This provides an abstract representation of an URL and its parameters with the option of changing/removing parameters only by providing a token. These tokens are created when a new parameter is acquired and are therefore controlled by the component that added the parameter. This gives us better control over who gets to change which parameter. Besides that, parameters are always given a namespace so that parameters with the same name can exist beside each other. The in- and output of the URLBuilder are \ILIAS\Data\URI objects.

Along with this class, an equivalent Javascript class is provided in UI/Core that offers a similar functionality. The PHP object can be "transferred" to JS in any renderer by using the provided render...() functions to create JS objects/maps.

Definition at line 40 of file URLBuilder.php.

Constructor & Destructor Documentation

◆ __construct()

ILIAS\UI\URLBuilder::__construct ( URI  $uri)

Definition at line 74 of file URLBuilder.php.

75 {
76 $this->uri = $uri;
77 }
URI $uri
Base URI for the URLBuilder.
Definition: URLBuilder.php:54

References ILIAS\UI\URLBuilder\$uri.

Member Function Documentation

◆ acquireParameter()

ILIAS\UI\URLBuilder::acquireParameter ( array  $namespace,
string  $name,
?string  $initial_value = null 
)

Add a new parameter with a namespace and get its token for subsequent changes.

The namespace can consists of one or more levels which are noted as an array. They will be joined with the separator (see constant) and used as a prefix for the name, e.g. Namespace: ["ilOrgUnit","filter"] Name: "name" Resulting parameter: "ilOrgUnit_filter_name"

The return value is an array containing both the changed URLBuilder as well as the token for any subsequent changes to the acquired parameter.

Returns
array{0: URLBuilder, 1: URLBuilderToken}
Exceptions

ilException

Exceptions

InvalidArgumentException

Definition at line 119 of file URLBuilder.php.

119 : array
120 {
121 if ($name === '' || empty($namespace)) {
122 throw new \InvalidArgumentException("Parameter name or namespace not set");
123 }
124
125 $parameter = implode(self::SEPARATOR, $namespace) . self::SEPARATOR . $name;
126 if ($this->parameterExists($parameter)) {
127 throw new \ilException("Parameter '" . $parameter . "' already reserved in URL");
128 }
129
130 $token = new URLBuilderToken($namespace, $name);
131 $clone = clone $this;
132 $clone->params[$parameter] = ($initial_value) ?? '';
133 $clone->tokens[$parameter] = $token;
134
135 return [$clone, $token];
136 }
parameterExists(string $name)
Check if parameter was already acquired.
Definition: URLBuilder.php:250
if($err=$client->getError()) $namespace
$token
Definition: xapitoken.php:70

References $namespace, $token, and ILIAS\UI\URLBuilder\parameterExists().

Referenced by ILIAS\components\ResourceStorage\Container\View\ActionBuilder\appendNamespaceToURIBuilder().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ acquireParameters()

◆ buildFragment()

ILIAS\UI\URLBuilder::buildFragment ( )
private

Create the fragment/hash part of the URL.

Definition at line 238 of file URLBuilder.php.

238 : string
239 {
240 if ($this->fragment !== null) {
241 return ($this->fragment !== '') ? '#' . $this->fragment : '';
242 }
243 $fragment = ($this->uri->getFragment()) ? '#' . $this->uri->getFragment() : '';
244 return $fragment;
245 }
string $fragment
Stores the URL fragment/hash (#) (always changeable due to its usage)
Definition: URLBuilder.php:59

References ILIAS\UI\URLBuilder\$fragment.

◆ buildQuery()

ILIAS\UI\URLBuilder::buildQuery ( )
private

Create the query part of the URL from all parameters Claimed parameters overwrite base parameters in array_merge(), numeric indizes of array-parameters are being removed to ensure continous numeration (p[1]=A&p[2]=B --> p[]=A&p[]=B).

Definition at line 227 of file URLBuilder.php.

227 : string
228 {
229 $params = array_merge($this->uri->getParameters(), $this->params);
230 $query = (! empty($params)) ? '?' . http_build_query($params) : '';
231 $query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
232 return $query;
233 }
array $params
Stores all acquired parameters These always take precedence over existing parameters in the base URI.
Definition: URLBuilder.php:66

References ILIAS\UI\URLBuilder\$params.

◆ buildURI()

ILIAS\UI\URLBuilder::buildURI ( )

Get a URI representation of the full URL including query string and fragment/hash.

Exceptions

LengthException if length of URI is greater than self::URL_MAX_LENGTH

Definition at line 214 of file URLBuilder.php.

214 : URI
215 {
216 $uri = new URI($this->uri->getBaseURI() . $this->buildQuery() . $this->buildFragment());
217 $this->checkLength($uri);
218 return $uri;
219 }
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
checkLength(URI $uri)
Check the full length of the URI against URL_MAX_LENGTH.
Definition: URLBuilder.php:277

References ILIAS\UI\URLBuilder\$uri, and ILIAS\UI\URLBuilder\checkLength().

Referenced by ILIAS\UI\Implementation\Component\Table\Action\Action\__construct(), ilObjLanguageFolderGUI\getUrl(), ilStudyProgrammeTypeGUI\getUrl(), and ILIAS\UI\URLBuilder\renderObject().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ checkLength()

ILIAS\UI\URLBuilder::checkLength ( URI  $uri)
private

Check the full length of the URI against URL_MAX_LENGTH.

Exceptions

LengthException

Definition at line 277 of file URLBuilder.php.

277 : void
278 {
279 if (! (strlen((string) $uri) <= self::URL_MAX_LENGTH)) {
280 throw new \LengthException("The final URL is longer than " . self::URL_MAX_LENGTH . " and will not be valid.");
281 }
282 }

References ILIAS\UI\URLBuilder\$uri.

Referenced by ILIAS\UI\URLBuilder\buildURI().

+ Here is the caller graph for this function:

◆ checkToken()

ILIAS\UI\URLBuilder::checkToken ( URLBuilderToken  $token)
private

Check if a token is valid.

Exceptions

DomainException

Exceptions

ilException

Definition at line 261 of file URLBuilder.php.

261 : void
262 {
263 if (! in_array($token, $this->tokens)
264 || $this->tokens[$token->getName()]->getToken() !== $token->getToken()) {
265 throw new \DomainException("Token for '" . $token->getName() . "' is not valid");
266 }
267 if (! $this->parameterExists($token->getName())) {
268 throw new \ilException("Parameter '" . $token->getName() . "' does not exist in URL");
269 }
270 }
getName()
Get the full name of the token including its namespace.

References $token, ILIAS\UI\URLBuilderToken\getName(), and ILIAS\UI\URLBuilder\parameterExists().

Referenced by ILIAS\UI\URLBuilder\deleteParameter(), and ILIAS\UI\URLBuilder\withParameter().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deleteParameter()

ILIAS\UI\URLBuilder::deleteParameter ( URLBuilderToken  $token)

Delete an acquired parameter if the supplied token is valid.

Definition at line 153 of file URLBuilder.php.

153 : self
154 {
155 $this->checkToken($token);
156 $clone = clone $this;
157 unset($clone->params[$token->getName()]);
158 unset($clone->tokens[$token->getName()]);
159
160 return $clone;
161 }
checkToken(URLBuilderToken $token)
Check if a token is valid.
Definition: URLBuilder.php:261

References $token, and ILIAS\UI\URLBuilder\checkToken().

+ Here is the call graph for this function:

◆ parameterExists()

ILIAS\UI\URLBuilder::parameterExists ( string  $name)
private

Check if parameter was already acquired.

Definition at line 250 of file URLBuilder.php.

250 : bool
251 {
252 return array_key_exists($name, $this->params);
253 }

Referenced by ILIAS\UI\URLBuilder\acquireParameter(), and ILIAS\UI\URLBuilder\checkToken().

+ Here is the caller graph for this function:

◆ renderObject()

ILIAS\UI\URLBuilder::renderObject ( array  $tokens)

Renders a Javascript URLBuilder object with changeable parameters for all given tokens.

Note: By providing only the tokens that need to be changed on the JS side, all other parameters will be passed as unchangeable.

Parameters
array<URLBuilderToken>$tokens

Definition at line 203 of file URLBuilder.php.

203 : string
204 {
205 $output = 'new il.UI.core.URLBuilder(new URL("' . $this->buildURI() . '"), ' . $this->renderTokens($tokens) . ')';
206 return $output;
207 }
renderTokens(array $tokens)
Renders a Javascript Map of all given tokens.
Definition: URLBuilder.php:183
buildURI()
Get a URI representation of the full URL including query string and fragment/hash.
Definition: URLBuilder.php:214

References ILIAS\UI\URLBuilder\buildURI(), and ILIAS\UI\URLBuilder\renderTokens().

+ Here is the call graph for this function:

◆ renderTokens()

ILIAS\UI\URLBuilder::renderTokens ( array  $tokens)

Renders a Javascript Map of all given tokens.

Note: Only the tokens needed for changing parameters on the JS side should be used here.

Parameters
array<URLBuilderToken>$tokens

Definition at line 183 of file URLBuilder.php.

183 : string
184 {
185 $token_render = [];
186 foreach ($tokens as $token) {
187 $token_render[] = '["' . $token->getName() . '",' . $token->render() . ']';
188 }
189 $output = 'new Map([' . implode(',', $token_render) . '])';
190 return $output;
191 }

References $token, and ILIAS\UI\URLBuilder\$tokens.

Referenced by ILIAS\UI\URLBuilder\renderObject().

+ Here is the caller graph for this function:

◆ withFragment()

ILIAS\UI\URLBuilder::withFragment ( ?string  $fragment)

Change the fragment/hash part of the URL.

Definition at line 92 of file URLBuilder.php.

92 : self
93 {
94 $clone = clone $this;
95 $clone->fragment = $fragment;
96 return $clone;
97 }

References ILIAS\UI\URLBuilder\$fragment.

◆ withParameter()

ILIAS\UI\URLBuilder::withParameter ( URLBuilderToken  $token,
string|array  $value 
)

Change an acquired parameter's value if the supplied token is valid.

Definition at line 166 of file URLBuilder.php.

166 : self
167 {
168 $this->checkToken($token);
169 $clone = clone $this;
170 $clone->params[$token->getName()] = $value;
171
172 return $clone;
173 }

References $token, and ILIAS\UI\URLBuilder\checkToken().

Referenced by ilMediaCastManageTableGUI\get(), LDAPRoleMappingTable\getActions(), ilMailTemplateTable\getActions(), ILIAS\Registration\RegistrationCodesTable\getActions(), ilObjLanguageFolderGUI\getUrl(), ilStudyProgrammeTypeGUI\getUrl(), and ilOrgUnitTypeGUI\setSubTabsEdit().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ withURI()

ILIAS\UI\URLBuilder::withURI ( URI  $uri)

Changes the base URI of the Builder.

Definition at line 82 of file URLBuilder.php.

82 : self
83 {
84 $clone = clone $this;
85 $clone->uri = $uri;
86 return $clone;
87 }

References ILIAS\UI\URLBuilder\$uri.

Field Documentation

◆ $fragment

string ILIAS\UI\URLBuilder::$fragment = null
private

Stores the URL fragment/hash (#) (always changeable due to its usage)

Definition at line 59 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\buildFragment(), and ILIAS\UI\URLBuilder\withFragment().

◆ $params

array ILIAS\UI\URLBuilder::$params = []
private

Stores all acquired parameters These always take precedence over existing parameters in the base URI.

array<string, string>

Definition at line 66 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\buildQuery().

◆ $tokens

array ILIAS\UI\URLBuilder::$tokens = []
private

Stores all generated tokens for acquired parameters.

array<string, URLBuilderToken>

Definition at line 72 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilder\acquireParameters(), and ILIAS\UI\URLBuilder\renderTokens().

◆ $uri

URI ILIAS\UI\URLBuilder::$uri
private

◆ SEPARATOR

◆ URL_MAX_LENGTH

const ILIAS\UI\URLBuilder::URL_MAX_LENGTH = 8192

A maximum length of 8192 characters should be safe to use in most browsers, even though longer URLs will be supported by some.

Definition at line 46 of file URLBuilder.php.

Referenced by ILIAS\UI\URLBuilderTest\testUrlTooLong().


The documentation for this class was generated from the following file: