MockAliasManager.php 1.86 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
Crell's avatar
Crell committed
5
 * Contains Drupal\system\Tests\Routing\MockAliasManager.
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 */

namespace Drupal\system\Tests\Routing;

use Drupal\Core\Path\AliasManagerInterface;

/**
 * An easily configurable mock alias manager.
 */
class MockAliasManager implements AliasManagerInterface {

  /**
   * Array of mocked aliases. Keys are system paths, followed by language.
   *
20
   * @var array
21 22 23
   */
  protected $aliases = array();

24 25 26 27 28
  /**
   * Array of mocked aliases. Keys are aliases, followed by language.
   *
   * @var array
   */
29 30
  protected $systemPaths = array();

31 32 33 34 35
  /**
   * An index of aliases that have been requested.
   *
   * @var array
   */
36 37
  protected $lookedUp = array();

38 39 40 41 42
  /**
   * The language to assume a path alias is for if not specified.
   *
   * @var string
   */
43 44
  public $defaultLanguage = 'en';

45 46 47 48 49 50 51 52 53 54
  /**
   * Adds an alias to the in-memory alias table for this object.
   *
   * @param type $path
   *   The system path of the alias.
   * @param type $alias
   *   The alias of the system path.
   * @param type $path_language
   *   The language of this alias.
   */
55 56 57 58 59 60 61
  public function addAlias($path, $alias, $path_language = NULL) {
    $language = $path_language ?: $this->defaultLanguage;

    $this->aliases[$path][$language] = $alias;
    $this->systemPaths[$alias][$language] = $path;
  }

62
  /**
63
   * {@inheritdoc}
64
   */
65 66 67
  public function getPathByAlias($alias, $langcode = NULL) {
    $langcode = $langcode ?: $this->defaultLanguage;
    return $this->systemPaths[$alias][$langcode];
68 69
  }

70
  /**
71
   * {@inheritdoc}
72 73 74
   * @param $path
   * @param null $langcode
   * @return
75
   */
76 77
  public function getAliasByPath($path, $langcode = NULL) {
    $langcode = $langcode ?: $this->defaultLanguage;
78
    $this->lookedUp[$path] = 1;
79
    return $this->aliases[$path][$langcode];
80 81
  }

82 83 84 85 86 87
  /**
   * {@inheritdoc}
   */
  public function cacheClear($source = NULL) {
    // Not needed.
  }
88
}