Skip to content

Commit 9af9a8e

Browse files
authored
Api shorthand add routes to routes/api.php (#210)
1 parent c82a8c5 commit 9af9a8e

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed

src/Generators/RouteGenerator.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ public function output(array $tree): array
2323
}
2424

2525
$routes = '';
26+
$path = 'routes/web.php';
2627
/** @var \Blueprint\Models\Controller $controller */
2728
foreach ($tree['controllers'] as $controller) {
2829
$routes .= PHP_EOL . PHP_EOL . $this->buildRoutes($controller);
30+
31+
if ($controller->isApiResource()) {
32+
$path = 'routes/api.php';
33+
}
2934
}
3035
$routes .= PHP_EOL;
3136

32-
$path = 'routes/web.php';
3337
$this->files->append($path, $routes);
3438

3539
return ['updated' => [$path]];
@@ -45,9 +49,14 @@ protected function buildRoutes(Controller $controller)
4549

4650
$resource_methods = array_intersect($methods, Controller::$resourceMethods);
4751
if (count($resource_methods)) {
48-
$routes .= sprintf("Route::resource('%s', '%s')", $slug, $className);
52+
$routes .= $controller->isApiResource()
53+
? sprintf("Route::apiResource('%s', '%s')", $slug, $className)
54+
: sprintf("Route::resource('%s', '%s')", $slug, $className);
55+
56+
$missing_methods = $controller->isApiResource()
57+
? array_diff(Controller::$apiResourceMethods, $resource_methods)
58+
: array_diff(Controller::$resourceMethods, $resource_methods);
4959

50-
$missing_methods = array_diff(Controller::$resourceMethods, $resource_methods);
5160
if (count($missing_methods)) {
5261
if (count($missing_methods) < 4) {
5362
$routes .= sprintf("->except('%s')", implode("', '", $missing_methods));
@@ -56,7 +65,6 @@ protected function buildRoutes(Controller $controller)
5665
}
5766
}
5867

59-
6068
$routes .= ';' . PHP_EOL;
6169
}
6270

src/Lexers/ControllerLexer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function analyze(array $tokens): array
3636
unset($original['resource']);
3737
// this gives the ability to both use a shorthand and override some methods
3838
$definition = array_merge($definition, $original);
39+
$controller->setApiResource(true);
3940
}
4041

4142
foreach ($definition as $method => $body) {

src/Models/Controller.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Controller
1010
/** @var array */
1111
public static $resourceMethods = ['index', 'create', 'store', 'edit', 'update', 'show', 'destroy'];
1212

13+
/** @var array */
14+
public static $apiResourceMethods = ['index', 'store', 'update', 'show', 'destroy'];
15+
1316
/**
1417
* @var string
1518
*/
@@ -25,6 +28,11 @@ class Controller
2528
*/
2629
private $methods = [];
2730

31+
/**
32+
* @var bool
33+
*/
34+
private $apiResource = false;
35+
2836
/**
2937
* Controller constructor.
3038
* @param $name
@@ -92,4 +100,20 @@ public function prefix()
92100

93101
return $this->name();
94102
}
103+
104+
/**
105+
* @param bool $apiResource
106+
*/
107+
public function setApiResource(bool $apiResource)
108+
{
109+
$this->apiResource = $apiResource;
110+
}
111+
112+
/**
113+
* @return bool
114+
*/
115+
public function isApiResource(): bool
116+
{
117+
return $this->apiResource;
118+
}
95119
}

tests/Feature/Generator/RouteGeneratorTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ public function output_writes_migration_for_route_tree($definition, $routes)
5757
$this->assertEquals(['updated' => [$path]], $this->subject->output($tree));
5858
}
5959

60+
/**
61+
* @test
62+
*/
63+
public function output_writes_migration_for_route_tree_api_routes()
64+
{
65+
$definition = "definitions/api-routes-example.bp";
66+
$routes = "routes/api-routes.php";
67+
$path = 'routes/api.php';
68+
69+
$this->files->expects('append')
70+
->with($path, $this->fixture($routes));
71+
72+
$tokens = $this->blueprint->parse($this->fixture($definition));
73+
$tree = $this->blueprint->analyze($tokens);
74+
75+
$this->assertEquals(['updated' => [$path]], $this->subject->output($tree));
76+
}
77+
6078
public function controllerTreeDataProvider()
6179
{
6280
return [

tests/Feature/Lexers/ControllerLexerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public function it_returns_an_api_resource_controller()
245245

246246
$controller = $actual['controllers']['Comment'];
247247
$this->assertEquals('CommentController', $controller->className());
248+
$this->assertTrue($controller->isApiResource());
248249

249250
$methods = $controller->methods();
250251
$this->assertCount(5, $methods);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
models:
2+
Certificate:
3+
name: string
4+
certificate_type_id: id
5+
reference: string
6+
document: string
7+
expiry_date: date
8+
remarks: text nullable
9+
10+
controllers:
11+
Certificate:
12+
resource: api
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
3+
Route::apiResource('certificate', 'CertificateController');

0 commit comments

Comments
 (0)