Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Lexers/ControllerLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public function analyze(array $tokens): array
$controller = new Controller($name);

if (isset($definition['resource'])) {
$resource_definition = $this->generateResourceTokens($controller, $this->methodsForResource($definition['resource']));
$resource_methods = $this->methodsForResource($definition['resource']);
$resource_definition = $this->generateResourceTokens($controller, $resource_methods);

if ($definition['resource'] === 'api') {
if ($this->hasOnlyApiResourceMethods($resource_methods)) {
$controller->setApiResource(true);
}

Expand Down Expand Up @@ -139,6 +140,13 @@ private function methodsForResource(string $type)
return ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
}

return array_map('trim', explode(',', $type));
return array_map('trim', explode(',', strtolower($type)));
}

private function hasOnlyApiResourceMethods(array $methods)
{
return collect($methods)->every(function ($item, $key) {
return Str::startsWith($item, 'api.');
});
}
}
12 changes: 2 additions & 10 deletions tests/Feature/Lexers/ControllerLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function it_returns_an_api_resource_controller()
$tokens = [
'controllers' => [
'Comment' => [
'resource' => 'api'
'resource' => 'api.index, api.store, api.show, api.update'
]
]
];
Expand Down Expand Up @@ -232,12 +232,6 @@ public function it_returns_an_api_resource_controller()
'resource' => 'comment'
])
->andReturn(['api-update-statements']);
$this->statementLexer->expects('analyze')
->with([
'delete' => 'comment',
'respond' => 200
])
->andReturn(['api-destroy-statements']);

$actual = $this->subject->analyze($tokens);

Expand All @@ -248,7 +242,7 @@ public function it_returns_an_api_resource_controller()
$this->assertTrue($controller->isApiResource());

$methods = $controller->methods();
$this->assertCount(5, $methods);
$this->assertCount(4, $methods);

$this->assertCount(1, $methods['index']);
$this->assertEquals('api-index-statements', $methods['index'][0]);
Expand All @@ -258,8 +252,6 @@ public function it_returns_an_api_resource_controller()
$this->assertEquals('api-show-statements', $methods['show'][0]);
$this->assertCount(1, $methods['update']);
$this->assertEquals('api-update-statements', $methods['update'][0]);
$this->assertCount(1, $methods['destroy']);
$this->assertEquals('api-destroy-statements', $methods['destroy'][0]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/definitions/multiple-resource-controllers.bp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ controllers:
resource

File:
resource: api
resource: api.store, api.show, api.update

Category:
resource
resource: index, api.destroy

Gallery:
resource: api
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@


Route::apiResource('file', 'FileController');
Route::apiResource('file', 'FileController')->except('index', 'destroy');

Route::apiResource('gallery', 'GalleryController');
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

Route::resource('page', 'PageController');

Route::resource('category', 'CategoryController');
Route::resource('category', 'CategoryController')->only('index', 'destroy');