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
|
|
|
class RouteNameGenerator |
29
|
|
|
{ |
30
|
|
|
const ROUTE_NAME_PREFIX = 'api_'; |
31
|
|
|
const SUBRESOURCE_SUFFIX = '_subresource'; |
32
|
|
|
|
33
|
|
|
private function __construct() |
34
|
|
|
{ |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Generates a Symfony route name. |
39
|
|
|
* |
40
|
|
|
* @param string $operationName |
41
|
|
|
* @param string $resourceShortName |
42
|
|
|
* @param string|bool $operationType |
43
|
|
|
* @param array $subresourceContext |
44
|
|
|
* |
45
|
|
|
* @throws InvalidArgumentException |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public static function generate(string $operationName, string $resourceShortName, $operationType, array $subresourceContext = []): string |
50
|
|
|
{ |
51
|
|
|
if (OperationType::SUBRESOURCE === $operationType = OperationTypeDeprecationHelper::getOperationType($operationType)) { |
52
|
|
|
if (!isset($subresourceContext['property'])) { |
53
|
|
|
throw new InvalidArgumentException('Missing "property" to generate a route name from a subresource'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return sprintf( |
57
|
|
|
'%s%s_%s_%s%s', |
58
|
|
|
static::ROUTE_NAME_PREFIX, |
59
|
|
|
self::routeNameResolver($resourceShortName), |
60
|
|
|
self::routeNameResolver($subresourceContext['property'], $subresourceContext['collection'] ?? false), |
61
|
|
|
$operationName, |
62
|
|
|
self::SUBRESOURCE_SUFFIX |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return sprintf( |
67
|
|
|
'%s%s_%s_%s', |
68
|
|
|
static::ROUTE_NAME_PREFIX, |
69
|
|
|
self::routeNameResolver($resourceShortName), |
70
|
|
|
$operationName, |
71
|
|
|
$operationType |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Transforms a given string to a tableized, pluralized string. |
77
|
|
|
* |
78
|
|
|
* @param string $name usually a ResourceMetadata shortname |
79
|
|
|
* |
80
|
|
|
* @return string A string that is a part of the route name |
81
|
|
|
*/ |
82
|
|
|
public static function routeNameResolver(string $name, bool $pluralize = true): string |
83
|
|
|
{ |
84
|
|
|
$name = Inflector::tableize($name); |
85
|
|
|
|
86
|
|
|
return $pluralize ? Inflector::pluralize($name) : $name; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|