1<?php
2declare(strict_types = 1);
3
4namespace TYPO3\CMS\Core\Routing;
5
6/*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19use Symfony\Component\Routing\Generator\UrlGenerator as SymfonyUrlGenerator;
20use TYPO3\CMS\Core\Routing\Aspect\MappableProcessor;
21
22/**
23 * @internal
24 */
25class UrlGenerator extends SymfonyUrlGenerator
26{
27    /**
28     * @var MappableProcessor
29     */
30    protected $mappableProcessor;
31
32    public function injectMappableProcessor(MappableProcessor $mappableProcessor): void
33    {
34        $this->mappableProcessor = $mappableProcessor;
35    }
36
37    /**
38     * Processes aspect mapping on default values and delegates route generation to parent class.
39     *
40     * {@inheritdoc}
41     */
42    protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = [])
43    {
44        /** @var Route $route */
45        $route = $this->routes->get($name);
46        // _appliedDefaults contains internal(!) values (mapped default values are not generated yet)
47        // (keys used are deflated and need to be inflated later using VariableProcessor)
48        $relevantDefaults = array_intersect_key($defaults, array_flip($route->compile()->getPathVariables()));
49        $route->setOption('_appliedDefaults', array_diff_key($relevantDefaults, $parameters));
50        // map default values for URL generation (e.g. '1' becomes 'one' if defined in aspect)
51        $mappableProcessor = $this->mappableProcessor ?? new MappableProcessor();
52        $mappableProcessor->generate($route, $defaults);
53
54        return parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, $requiredSchemes);
55    }
56}
57