Skip to content

Commit e685017

Browse files
committed
Add basic respond statement
1 parent 888c493 commit e685017

File tree

7 files changed

+159
-15
lines changed

7 files changed

+159
-15
lines changed

src/Generators/ControllerGenerator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Blueprint\Models\Statements\QueryStatement;
1212
use Blueprint\Models\Statements\RedirectStatement;
1313
use Blueprint\Models\Statements\RenderStatement;
14+
use Blueprint\Models\Statements\RespondStatement;
1415
use Blueprint\Models\Statements\SendStatement;
1516
use Blueprint\Models\Statements\SessionStatement;
1617
use Blueprint\Models\Statements\ValidateStatement;
@@ -114,6 +115,8 @@ private function buildMethods(Controller $controller)
114115
$body .= self::INDENT . $statement->output() . PHP_EOL;
115116
} elseif ($statement instanceof RedirectStatement) {
116117
$body .= self::INDENT . $statement->output() . PHP_EOL;
118+
} elseif ($statement instanceof RespondStatement) {
119+
$body .= self::INDENT . $statement->output() . PHP_EOL;
117120
} elseif ($statement instanceof SessionStatement) {
118121
$body .= self::INDENT . $statement->output() . PHP_EOL;
119122
} elseif ($statement instanceof EloquentStatement) {

src/Lexers/StatementLexer.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Blueprint\Models\Statements\QueryStatement;
1111
use Blueprint\Models\Statements\RedirectStatement;
1212
use Blueprint\Models\Statements\RenderStatement;
13+
use Blueprint\Models\Statements\RespondStatement;
1314
use Blueprint\Models\Statements\SendStatement;
1415
use Blueprint\Models\Statements\SessionStatement;
1516
use Blueprint\Models\Statements\ValidateStatement;
@@ -44,6 +45,9 @@ public function analyze(array $tokens): array
4445
case 'redirect':
4546
$statements[] = $this->analyzeRedirect($statement);
4647
break;
48+
case 'respond':
49+
$statements[] = $this->analyzeRespond($statement);
50+
break;
4751
case 'save':
4852
case 'update':
4953
case 'delete':
@@ -88,17 +92,9 @@ private function analyzeRedirect(string $statement)
8892
return new RedirectStatement($route, $data);
8993
}
9094

91-
private function parseWithStatement(string $statement)
95+
private function analyzeRespond(string $statement)
9296
{
93-
[$object, $with] = $this->extractTokens($statement, 2);
94-
95-
$data = [];
96-
97-
if (!empty($with)) {
98-
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
99-
}
100-
101-
return [$object, $data];
97+
return new RespondStatement($statement);
10298
}
10399

104100
private function analyzeMail($statement)
@@ -126,6 +122,19 @@ private function analyzeValidate($statement)
126122
return new ValidateStatement(preg_split('/,([ \t]+)?/', $statement));
127123
}
128124

125+
private function parseWithStatement(string $statement)
126+
{
127+
[$object, $with] = $this->extractTokens($statement, 2);
128+
129+
$data = [];
130+
131+
if (!empty($with)) {
132+
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
133+
}
134+
135+
return [$object, $data];
136+
}
137+
129138
private function extractTokens(string $statement, int $limit = -1)
130139
{
131140
return array_pad(preg_split('/[ \t]+/', $statement, $limit), $limit, null);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
4+
namespace Blueprint\Models\Statements;
5+
6+
use Illuminate\Support\Str;
7+
8+
class RespondStatement
9+
{
10+
/**
11+
* @var int
12+
*/
13+
private $status = 200;
14+
15+
/**
16+
* @var string
17+
*/
18+
private $content;
19+
20+
public function __construct(string $data)
21+
{
22+
if (ctype_digit($data)) {
23+
$this->status = (int)$data;
24+
} else {
25+
$this->content = $data;
26+
}
27+
}
28+
29+
public function status(): int
30+
{
31+
return $this->status;
32+
}
33+
34+
public function content(): ?string
35+
{
36+
return $this->content;
37+
}
38+
39+
public function output()
40+
{
41+
if ($this->content()) {
42+
return 'return $' . $this->content . ';';
43+
}
44+
45+
return sprintf('return response()->noContent(%s);', $this->status() === 204 ? '' : $this->status());
46+
}
47+
}

tests/Feature/Generator/ControllerGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public function controllerTreeDataProvider()
106106
['definitions/readme-example.bp', 'app/Http/Controllers/PostController.php', 'controllers/readme-example.php'],
107107
['definitions/crazy-eloquent.bp', 'app/Http/Controllers/PostController.php', 'controllers/crazy-eloquent.php'],
108108
['definitions/nested-components.bp', 'app/Http/Controllers/Admin/UserController.php', 'controllers/nested-components.php'],
109+
['definitions/respond-statements.bp', 'app/Http/Controllers/Api/PostController.php', 'controllers/respond-statements.php'],
109110
];
110111
}
111112
}

tests/Feature/Lexers/StatementLexerTest.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Blueprint\Models\Statements\QueryStatement;
1010
use Blueprint\Models\Statements\RedirectStatement;
1111
use Blueprint\Models\Statements\RenderStatement;
12+
use Blueprint\Models\Statements\RespondStatement;
1213
use Blueprint\Models\Statements\SendStatement;
1314
use Blueprint\Models\Statements\SessionStatement;
1415
use Blueprint\Models\Statements\ValidateStatement;
@@ -314,13 +315,40 @@ public function it_returns_a_redirect_statement_with_data()
314315
$this->assertEquals(['foo', 'bar', 'baz'], $actual[0]->data());
315316
}
316317

317-
public function eloquentTokensProvider()
318+
/**
319+
* @test
320+
*/
321+
public function it_returns_a_response_statement_with_status_code()
318322
{
319-
return [
320-
['save', 'post'],
321-
['update', 'post'],
322-
['delete', 'post.id'],
323+
$tokens = [
324+
'respond' => '204'
325+
];
326+
327+
$actual = $this->subject->analyze($tokens);
328+
329+
$this->assertCount(1, $actual);
330+
$this->assertInstanceOf(RespondStatement::class, $actual[0]);
331+
332+
$this->assertEquals(204, $actual[0]->status());
333+
$this->assertNull($actual[0]->content());
334+
}
335+
336+
/**
337+
* @test
338+
*/
339+
public function it_returns_a_response_statement_with_content()
340+
{
341+
$tokens = [
342+
'respond' => 'post'
323343
];
344+
345+
$actual = $this->subject->analyze($tokens);
346+
347+
$this->assertCount(1, $actual);
348+
$this->assertInstanceOf(RespondStatement::class, $actual[0]);
349+
350+
$this->assertEquals(200, $actual[0]->status());
351+
$this->assertEquals('post', $actual[0]->content());
324352
}
325353

326354
/**
@@ -444,4 +472,13 @@ public function sessionTokensProvider()
444472
['store', 'post.id'],
445473
];
446474
}
475+
476+
public function eloquentTokensProvider()
477+
{
478+
return [
479+
['save', 'post'],
480+
['update', 'post'],
481+
['delete', 'post.id'],
482+
];
483+
}
447484
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Api\Post;
6+
use Illuminate\Http\Request;
7+
8+
class PostController extends Controller
9+
{
10+
/**
11+
* @param \Illuminate\Http\Request $request
12+
* @return \Illuminate\Http\Response
13+
*/
14+
public function index(Request $request)
15+
{
16+
$posts = Post::all();
17+
18+
return $posts;
19+
}
20+
21+
/**
22+
* @param \Illuminate\Http\Request $request
23+
* @return \Illuminate\Http\Response
24+
*/
25+
public function store(Request $request)
26+
{
27+
return response()->noContent();
28+
}
29+
30+
/**
31+
* @param \Illuminate\Http\Request $request
32+
* @return \Illuminate\Http\Response
33+
*/
34+
public function error(Request $request)
35+
{
36+
return response()->noContent(400);
37+
}
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
controllers:
2+
Api/Post:
3+
index:
4+
query: all:posts
5+
respond: posts
6+
store:
7+
respond: 204
8+
error:
9+
respond: 400

0 commit comments

Comments
 (0)