comparison core/modules/system/src/Tests/Routing/MockAliasManager.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\Routing;
4
5 use Drupal\Core\Path\AliasManagerInterface;
6
7 /**
8 * An easily configurable mock alias manager.
9 */
10 class MockAliasManager implements AliasManagerInterface {
11
12 /**
13 * Array of mocked aliases. Keys are system paths, followed by language.
14 *
15 * @var array
16 */
17 protected $aliases = [];
18
19 /**
20 * Array of mocked aliases. Keys are aliases, followed by language.
21 *
22 * @var array
23 */
24 protected $systemPaths = [];
25
26 /**
27 * An index of aliases that have been requested.
28 *
29 * @var array
30 */
31 protected $lookedUp = [];
32
33 /**
34 * The language to assume a path alias is for if not specified.
35 *
36 * @var string
37 */
38 public $defaultLanguage = 'en';
39
40 /**
41 * Adds an alias to the in-memory alias table for this object.
42 *
43 * @param string $path
44 * The system path of the alias.
45 * @param string $alias
46 * The alias of the system path.
47 * @param string $path_language
48 * The language of this alias.
49 */
50 public function addAlias($path, $alias, $path_language = NULL) {
51 $language = $path_language ?: $this->defaultLanguage;
52
53 if ($path[0] !== '/') {
54 throw new \InvalidArgumentException('The path needs to start with a slash.');
55 }
56 if ($alias[0] !== '/') {
57 throw new \InvalidArgumentException('The alias needs to start with a slash.');
58 }
59
60 $this->aliases[$path][$language] = $alias;
61 $this->systemPaths[$alias][$language] = $path;
62 }
63
64 /**
65 * {@inheritdoc}
66 */
67 public function getPathByAlias($alias, $langcode = NULL) {
68 $langcode = $langcode ?: $this->defaultLanguage;
69 return $this->systemPaths[$alias][$langcode];
70 }
71
72 /**
73 * {@inheritdoc}
74 * @param $path
75 * @param null $langcode
76 * @return
77 */
78 public function getAliasByPath($path, $langcode = NULL) {
79 if ($path[0] !== '/') {
80 throw new \InvalidArgumentException(sprintf('Source path %s has to start with a slash.', $path));
81 }
82
83 $langcode = $langcode ?: $this->defaultLanguage;
84 $this->lookedUp[$path] = 1;
85 return $this->aliases[$path][$langcode];
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function cacheClear($source = NULL) {
92 // Not needed.
93 }
94
95 }