forked from juampi92/api-resources
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAPIResourceManager.php
More file actions
156 lines (133 loc) · 3.86 KB
/
Copy pathAPIResourceManager.php
File metadata and controls
156 lines (133 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace Juampi92\APIResources;
use Exception;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Juampi92\APIResources\Resolvers\ClassnameResolverFactory;
use Juampi92\APIResources\Support\Version;
class APIResourceManager
{
private Repository $config;
private ClassnameResolverFactory $classnameResolverFactory;
private ?APIResourceVersion $version = null;
public function __construct(Repository $config, ClassnameResolverFactory $classnameResolverFactory)
{
$this->config = $config;
$this->classnameResolverFactory = $classnameResolverFactory;
}
private function getResourceVersion(): APIResourceVersion
{
if (! $this->version) {
throw new Exception('APIResource not initialised. Please use APIResource::setVersion($version) to specify the current version');
}
return $this->version;
}
/**
* Returns the name of the versioned route.
*
* @param string $route
* @return string
*/
public function getRouteName(string $route)
{
return $this->getResourceVersion()->getRouteName($route);
}
/**
* Returns the versioned url.
*
* @param string $name
* @param mixed $parameters
*/
public function getRoute($name, $parameters = [], bool $absolute = true): string
{
return route($this->getRouteName((string) $name), $parameters, $absolute);
}
/**
* Sets the current API version.
*
* @param string $current
* @param string $apiName = null
*
* @return $this
*/
public function setVersion(string $current, ?string $apiName = null)
{
$config = new Config($this->config, $apiName);
$resolver = $this->classnameResolverFactory->make($config);
$this->version = new APIResourceVersion($config, $current, $resolver);
return $this;
}
/**
* Gets the current API version.
*
* @return string
*/
public function getVersion()
{
return $this->getResourceVersion()->getVersion();
}
public function getLatestVersion(): string
{
return $this->getResourceVersion()->getLatestVersion();
}
/**
* Checks if the given version is the latest.
*
* @param string $current
*
* @return bool
*/
public function isLatest(string $current = null): bool
{
if (is_null($current)) {
$current = $this->getResourceVersion()->getVersion();
}
return $this->getResourceVersion()->getLatestVersion() === $current;
}
/**
* Smart builds the classname using the correct version.
* If it fails with the current version, it falls back to
* the latest version. If it still fails, throw exception.
*
* @param string $classname
*
* @return APIResource
* @throws Exceptions\ResourceNotFoundException
*/
public function resolve(string $classname): APIResource
{
return new APIResource(
$this->resolveClassname($classname)
);
}
/**
* @returns class-string<JsonResource>
*/
public function resolveClassname(string $classname): string
{
return $this->getResourceVersion()->resolveClassname($classname);
}
/**
* @param string $classname
* @param array $args
*
* @return JsonResource
*/
public function make($classname, ...$args)
{
$resource = $this->resolve($classname);
return $resource->make(...$args);
}
/**
* @param string $classname
* @param array ...$args
*
* @return ResourceCollection
*/
public function collection($classname, ...$args)
{
$resource = $this->resolve($classname);
return $resource->collection(...$args);
}
}