Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Routing;
13
 
14
use Symfony\Component\Routing\Exception\InvalidArgumentException;
15
 
16
class Alias
17
{
18
    private string $id;
19
    private array $deprecation = [];
20
 
21
    public function __construct(string $id)
22
    {
23
        $this->id = $id;
24
    }
25
 
26
    public function withId(string $id): static
27
    {
28
        $new = clone $this;
29
 
30
        $new->id = $id;
31
 
32
        return $new;
33
    }
34
 
35
    /**
36
     * Returns the target name of this alias.
37
     *
38
     * @return string The target name
39
     */
40
    public function getId(): string
41
    {
42
        return $this->id;
43
    }
44
 
45
    /**
46
     * Whether this alias is deprecated, that means it should not be referenced anymore.
47
     *
48
     * @param string $package The name of the composer package that is triggering the deprecation
49
     * @param string $version The version of the package that introduced the deprecation
50
     * @param string $message The deprecation message to use
51
     *
52
     * @return $this
53
     *
54
     * @throws InvalidArgumentException when the message template is invalid
55
     */
56
    public function setDeprecated(string $package, string $version, string $message): static
57
    {
58
        if ('' !== $message) {
59
            if (preg_match('#[\r\n]|\*/#', $message)) {
60
                throw new InvalidArgumentException('Invalid characters found in deprecation template.');
61
            }
62
 
63
            if (!str_contains($message, '%alias_id%')) {
64
                throw new InvalidArgumentException('The deprecation template must contain the "%alias_id%" placeholder.');
65
            }
66
        }
67
 
68
        $this->deprecation = [
69
            'package' => $package,
70
            'version' => $version,
71
            'message' => $message ?: 'The "%alias_id%" route alias is deprecated. You should stop using it, as it will be removed in the future.',
72
        ];
73
 
74
        return $this;
75
    }
76
 
77
    public function isDeprecated(): bool
78
    {
79
        return (bool) $this->deprecation;
80
    }
81
 
82
    /**
83
     * @param string $name Route name relying on this alias
84
     */
85
    public function getDeprecation(string $name): array
86
    {
87
        return [
88
            'package' => $this->deprecation['package'],
89
            'version' => $this->deprecation['version'],
90
            'message' => str_replace('%alias_id%', $name, $this->deprecation['message']),
91
        ];
92
    }
93
}