1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Symfony\Routing; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
17
|
|
|
use ApiPlatform\Core\Api\OperationTypeDeprecationHelper; |
18
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
19
|
|
|
use Doctrine\Common\Util\Inflector; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Generates the Symfony route name associated with an operation name and a resource short name. |
23
|
|
|
* |
24
|
|
|
* @internal |
25
|
|
|
* |
26
|
|
|
* @author Baptiste Meyer <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class RouteNameGenerator |
29
|
|
|
{ |
30
|
|
|
const ROUTE_NAME_PREFIX = 'api_'; |
31
|
|
|
|
32
|
|
|
private function __construct() |
33
|
|
|
{ |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Generates a Symfony route name. |
38
|
|
|
* |
39
|
|
|
* @param string $operationName |
40
|
|
|
* @param string $resourceShortName |
41
|
|
|
* @param string|bool $operationType |
42
|
|
|
* |
43
|
|
|
* @throws InvalidArgumentException |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function generate(string $operationName, string $resourceShortName, $operationType): string |
48
|
|
|
{ |
49
|
|
|
if (OperationType::SUBRESOURCE === $operationType = OperationTypeDeprecationHelper::getOperationType($operationType)) { |
50
|
|
|
throw new InvalidArgumentException('Subresource operations are not supported by the RouteNameGenerator.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return sprintf( |
54
|
|
|
'%s%s_%s_%s', |
55
|
|
|
static::ROUTE_NAME_PREFIX, |
56
|
|
|
self::inflector($resourceShortName), |
57
|
|
|
$operationName, |
58
|
|
|
$operationType |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Transforms a given string to a tableized, pluralized string. |
64
|
|
|
* |
65
|
|
|
* @param string $name usually a ResourceMetadata shortname |
66
|
|
|
* |
67
|
|
|
* @return string A string that is a part of the route name |
68
|
|
|
*/ |
69
|
|
|
public static function inflector(string $name, bool $pluralize = true): string |
70
|
|
|
{ |
71
|
|
|
$name = Inflector::tableize($name); |
72
|
|
|
|
73
|
|
|
return $pluralize ? Inflector::pluralize($name) : $name; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|