8000 [Feature-WIP] Refactor registrar implementation into units · volldigital/laravel-json-api@908e95a · GitHub
[go: up one dir, main page]

Skip to content

Commit 908e95a

Browse files
committed
[Feature-WIP] Refactor registrar implementation into units
This will allow the next step of features to be added, primarily choosing which routes are registered using the `only` and `except` options.
1 parent e7f7c85 commit 908e95a

File tree

11 files changed

+1148
-432
lines changed

11 files changed

+1148
-432
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
class CommentsController
6+
{
7+
8+
/**
9+
* @return string
10+
*/
11+
public function index()
12+
{
13+
return 'comments:index';
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function create()
20+
{
21+
return 'comments:create';
22+
}
23+
24+
/**
25+
* @param $id
26+
* @return string
27+
*/
28+
public function read($id)
29+
{
30+
return "comments:read:$id";
31+
}
32+
33+
/**
34+
* @param $id
35+
* @return string
36+
*/
37+
public function update($id)
38+
{
39+
return "comments:update:$id";
40+
}
41+
42+
/**
43+
* @param $id
44+
* @return string
45+
*/
46+
public function delete($id)
47+
{
48+
return "comments:delete:$id";
49+
}
50+
51+
/**
52+
* @param $id
53+
* @return string
54+
*/
55+
public function readRelatedResource($id)
56+
{
57+
return "comments:read-related:$id";
58+
}
59+
60+
/**
61+
* @param $id
62+
* @return string
63+
*/
64+
public function readRelationship($id)
65+
{
66+
return "comments:read-relationship:$id";
67+
}
68+
69+
/**
70+
* @param $id
71+
* @return string
72+
*/
73+
public function replaceRelationship($id)
74+
{
75+
return "comments:replace-relationship:$id";
76+
}
77+
78+
/**
79+
* @param $id
80+
* @return string
81+
*/
82+
public function addToRelationship($id)
83+
{
84+
return "comments:add-relationship:$id";
85+
}
86+
87+
/**
88+
* @param $id
89+
* @return string
90+
*/
91+
public function removeFromRelationship($id)
92+
{
93+
return "comments:remove-relationship:$id";
94+
}
95+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
class PostsController
6+
{
7+
8+
/**
9+
* @return string
10+
*/
11+
public function index()
12+
{
13+
return 'posts:index';
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function create()
20+
{
21+
return 'posts:create';
22+
}
23+
24+
/**
25+
* @param $id
26+
* @return string
27+
*/
28+
public function read($id)
29+
{
30+
return "posts:read:$id";
31+
}
32+
33+
/**
34+
* @param $id
35+
* @return string
36+
*/
37+
public function update($id)
38+
{
39+
return "posts:update:$id";
40+
}
41+
42+
/**
43+
* @param $id
44+
* @return string
45+
*/
46+
public function delete($id)
47+
{
48+
return "posts:delete:$id";
49+
}
50+
51+
/**
52+
* @param $id
53+
* @return string
54+
*/
55+
public function readRelatedResource($id)
56+
{
57+
return "posts:read-related:$id";
58+
}
59+
60+
/**
61+
* @param $id
62+
* @return string
63+
*/
64+
public function readRelationship($id)
65+
{
66+
return "posts:read-relationship:$id";
67+
}
68+
69+
/**
70+
* @param $id
71+
* @return string
72+
*/
73+
public function replaceRelationship($id)
74+
{
75+
return "posts:replace-relationship:$id";
76+
}
77+
78+
/**
79+
* @param $id
80+
* @return string
81+
*/
82+
public function addToRelationship($id)
83+
{
84+
return "posts:add-relationship:$id";
85+
}
86+
87+
/**
88+
* @param $id
89+
* @return string
90+
*/
91+
public function removeFromRelationship($id)
92+
{
93+
return "posts:remove-relationship:$id";
94+
}
95+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"autoload-dev": {
4242
"psr-4": {
43+
"App\\": "app/",
4344
"CloudCreativity\\LaravelJsonApi\\": "test/"
4445
}
4546
},

src/Document/LinkFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function relatedResource(
120120
array $queryParams = [],
121121
$meta = null
122122
) {
123-
$name = RouteName::related($resourceType);
123+
$name = RouteName::related($resourceType, $relationshipKey);
124124

125125
return $this->relationshipRoute($name, $id, $relationshipKey, $queryParams, $meta);
126126
}
@@ -135,7 +135,7 @@ public function readRelationship(
135135
array $queryParams = [],
136136
$meta = null
137137
) {
138-
$name = RouteName::readRelationship($resourceType);
138+
$name = RouteName::readRelationship($resourceType, $relationshipKey);
139139

140140
return $this->relationshipRoute($name, $id, $relationshipKey, $queryParams, $meta);
141141
}
@@ -150,7 +150,7 @@ public function replaceRelationship(
150150
array $queryParams = [],
151151
$meta = null
152152
) {
153-
$name = RouteName::replaceRelationship($resourceType);
153+
$name = RouteName::replaceRelationship($resourceType, $relationshipKey);
154154

155155
return $this->relationshipRoute($name, $id, $relationshipKey, $queryParams, $meta);
156156
}
@@ -165,7 +165,7 @@ public function addRelationship(
165165
array $queryParams = [],
166166
$meta = null
167167
) {
168-
$name = RouteName::addRelationship($resourceType);
168+
$name = RouteName::addRelationship($resourceType, $relationshipKey);
169169

170170
return $this->relationshipRoute($name, $id, $relationshipKey, $queryParams, $meta);
171171
}
@@ -180,7 +180,7 @@ public function removeRelationship(
180180
array $queryParams = [],
181181
$meta = null
182182
) {
183-
$name = RouteName::removeRelationship($resourceType);
183+
$name = RouteName::removeRelationship($resourceType, $relationshipKey);
184184

185185
return $this->relationshipRoute($name, $id, $relationshipKey, $queryParams, $meta);
186186
}

src/Routing/RegistersResources.php

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2017 Cloud Creativity Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace CloudCreativity\LaravelJsonApi\Routing;
20+
21+
use CloudCreativity\JsonApi\Utils\Str;
22+
use Illuminate\Contracts\Routing\Registrar;
23+
use Illuminate\Routing\Route;
24+
use Illuminate\Support\Fluent;
25+
26+
/**
27+
* Class RegistersResources
28+
*
29+
* @package CloudCreativity\LaravelJsonApi
30+
*/
31+
trait RegistersResources
32+
{
33+
34+
/**
35+
* @var string
36+
*/
37+
protected $resourceType;
38+
39+
/**
40+
* @var Fluent
41+
*/
42+
protected $options;
43+
44+
45+
/**
46+
* @return string
47+
*/
48+
protected function baseUrl()
49+
{
50+
return '/';
51+
}
52+
53+
/**
54+
* @return string
55+
*/
56+
protected function resourceUrl()
57+
{
58+
return sprintf('%s/{%s}', $this->baseUrl(), ResourceRegistrar::PARAM_RESOURCE_ID);
59+
}
60+
61+
/**
62+
* @param string $relationship
63+
* @return string
64+
*/
65+
protected function relatedUrl($relationship)
66+
{
67+
return sprintf('%s/%s', $this->resourceUrl(), Str::dasherize($relationship));
68+
}
69+
70+
/**
71+
* @param $relationship
72+
* @return string
73+
*/
74+
protected function relationshipUrl($relationship)
75+
{
76+
return sprintf(
77+
'%s/%s/%s',
78+
$this->resourceUrl(),
79+
ResourceRegistrar::KEYWORD_RELATIONSHIPS,
80+
Str::dasherize($relationship)
81+
);
82+
}
83+
84+
/**
85+
* @param string $url
86+
* @return string|null
87+
*/
88+
protected function idConstraint($url)
89+
{
90+
if ($this->baseUrl() === $url) {
91+
return null;
92+
}
93+
94+
return $this->options->get('id');
95+
}
96+
97+
/**
98+
* @return string
99+
*/
100+
protected function controller()
101+
{
102+
if ($controller = $this->options->get('controller')) {
103+
return $controller;
104+
}
105+
106+
return $this->options['controller'] = Str::classify($this->resourceType) . 'Controller';
107+
}
108+
109+
/**
110+
* @return array
111+
*/
112+
protected function hasOne()
113+
{
114+
return (array) $this->options->get('has-one');
115+
}
116+
117+
/**
118+
* @return array
119+
*/
120+
protected function hasMany()
121+
{
122+
return (array) $this->options->get('has-many');
123+
}
124+
125+
/**
126+
* @param Registrar $router
127+
* @param $method
128+
* @param $uri
129+
* @param $action
130+
* @return Route
131+
*/
132+
protected function createRoute(Registrar $router, $method, $uri, $action)
133+
{
134+
/** @var Route $route */
135+
$route = $router->{$method}($uri, $action);
136+
$route->defaults(ResourceRegistrar::PARAM_RESOURCE_TYPE, $this->resourceType);
137+
138+
if ($idConstraint = $this->idConstraint($uri)) {
139+
$route->where(ResourceRegistrar::PARAM_RESOURCE_ID, $idConstraint);
140+
}
141+
142+
return $route;
143+
}
144+
}

0 commit comments

Comments
 (0)
0