UrlGenerator.php 1.66 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
Crell's avatar
Crell committed
5
 * Contains Drupal\Core\Routing\UrlGenerator.
6 7 8 9
 */

namespace Drupal\Core\Routing;

10 11
use Symfony\Component\HttpKernel\Log\LoggerInterface;

12 13 14 15 16 17
use Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
use Symfony\Cmf\Component\Routing\RouteProviderInterface;

use Drupal\Core\Path\AliasManagerInterface;

/**
18
 * A Generator creates URL strings based on a specified route.
19 20 21 22 23 24 25 26 27 28
 */
class UrlGenerator extends ProviderBasedGenerator {

  /**
   * The alias manager that will be used to alias generated URLs.
   *
   * @var AliasManagerInterface
   */
  protected $aliasManager;

29 30 31 32 33 34 35 36 37 38
  /**
   *  Constructs a new generator object.
   *
   * @param \Symfony\Cmf\Component\Routing\RouteProviderInterface $provider
   *   The route provider to be searched for routes.
   * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
   *   The alias manager responsible for path aliasing.
   * @param \Symfony\Component\HttpKernel\Log\LoggerInterface $logger
   *   An optional logger for recording errors.
   */
39 40 41 42 43 44
  public function __construct(RouteProviderInterface $provider, AliasManagerInterface $alias_manager, LoggerInterface $logger = NULL) {
    parent::__construct($provider, $logger);

    $this->aliasManager = $alias_manager;
  }

45 46 47
  /**
   * Implements Symfony\Component\Routing\Generator\UrlGeneratorInterface::generate();
   */
48 49 50 51 52 53 54 55 56 57 58
  public function generate($name, $parameters = array(), $absolute = FALSE) {
    $path = parent::generate($name, $parameters, $absolute);

    // This method is expected to return a path with a leading /, whereas
    // the alias manager has no leading /.
    $path = '/' . $this->aliasManager->getPathAlias(trim($path, '/'));

    return $path;
  }

}