Skip to content

Commit

Permalink
Add basic respond statement
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary committed Mar 26, 2020
1 parent 888c493 commit e685017
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/Generators/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Blueprint\Models\Statements\QueryStatement;
use Blueprint\Models\Statements\RedirectStatement;
use Blueprint\Models\Statements\RenderStatement;
use Blueprint\Models\Statements\RespondStatement;
use Blueprint\Models\Statements\SendStatement;
use Blueprint\Models\Statements\SessionStatement;
use Blueprint\Models\Statements\ValidateStatement;
Expand Down Expand Up @@ -114,6 +115,8 @@ private function buildMethods(Controller $controller)
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof RedirectStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof RespondStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof SessionStatement) {
$body .= self::INDENT . $statement->output() . PHP_EOL;
} elseif ($statement instanceof EloquentStatement) {
Expand Down
29 changes: 19 additions & 10 deletions src/Lexers/StatementLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Blueprint\Models\Statements\QueryStatement;
use Blueprint\Models\Statements\RedirectStatement;
use Blueprint\Models\Statements\RenderStatement;
use Blueprint\Models\Statements\RespondStatement;
use Blueprint\Models\Statements\SendStatement;
use Blueprint\Models\Statements\SessionStatement;
use Blueprint\Models\Statements\ValidateStatement;
Expand Down Expand Up @@ -44,6 +45,9 @@ public function analyze(array $tokens): array
case 'redirect':
$statements[] = $this->analyzeRedirect($statement);
break;
case 'respond':
$statements[] = $this->analyzeRespond($statement);
break;
case 'save':
case 'update':
case 'delete':
Expand Down Expand Up @@ -88,17 +92,9 @@ private function analyzeRedirect(string $statement)
return new RedirectStatement($route, $data);
}

private function parseWithStatement(string $statement)
private function analyzeRespond(string $statement)
{
[$object, $with] = $this->extractTokens($statement, 2);

$data = [];

if (!empty($with)) {
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
}

return [$object, $data];
return new RespondStatement($statement);
}

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

private function parseWithStatement(string $statement)
{
[$object, $with] = $this->extractTokens($statement, 2);

$data = [];

if (!empty($with)) {
$data = preg_split('/,([ \t]+)?/', substr($with, 5));
}

return [$object, $data];
}

private function extractTokens(string $statement, int $limit = -1)
{
return array_pad(preg_split('/[ \t]+/', $statement, $limit), $limit, null);
Expand Down
47 changes: 47 additions & 0 deletions src/Models/Statements/RespondStatement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php


namespace Blueprint\Models\Statements;

use Illuminate\Support\Str;

class RespondStatement
{
/**
* @var int
*/
private $status = 200;

/**
* @var string
*/
private $content;

public function __construct(string $data)
{
if (ctype_digit($data)) {
$this->status = (int)$data;
} else {
$this->content = $data;
}
}

public function status(): int
{
return $this->status;
}

public function content(): ?string
{
return $this->content;
}

public function output()
{
if ($this->content()) {
return 'return $' . $this->content . ';';
}

return sprintf('return response()->noContent(%s);', $this->status() === 204 ? '' : $this->status());
}
}
1 change: 1 addition & 0 deletions tests/Feature/Generator/ControllerGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function controllerTreeDataProvider()
['definitions/readme-example.bp', 'app/Http/Controllers/PostController.php', 'controllers/readme-example.php'],
['definitions/crazy-eloquent.bp', 'app/Http/Controllers/PostController.php', 'controllers/crazy-eloquent.php'],
['definitions/nested-components.bp', 'app/Http/Controllers/Admin/UserController.php', 'controllers/nested-components.php'],
['definitions/respond-statements.bp', 'app/Http/Controllers/Api/PostController.php', 'controllers/respond-statements.php'],
];
}
}
47 changes: 42 additions & 5 deletions tests/Feature/Lexers/StatementLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Blueprint\Models\Statements\QueryStatement;
use Blueprint\Models\Statements\RedirectStatement;
use Blueprint\Models\Statements\RenderStatement;
use Blueprint\Models\Statements\RespondStatement;
use Blueprint\Models\Statements\SendStatement;
use Blueprint\Models\Statements\SessionStatement;
use Blueprint\Models\Statements\ValidateStatement;
Expand Down Expand Up @@ -314,13 +315,40 @@ public function it_returns_a_redirect_statement_with_data()
$this->assertEquals(['foo', 'bar', 'baz'], $actual[0]->data());
}

public function eloquentTokensProvider()
/**
* @test
*/
public function it_returns_a_response_statement_with_status_code()
{
return [
['save', 'post'],
['update', 'post'],
['delete', 'post.id'],
$tokens = [
'respond' => '204'
];

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

$this->assertCount(1, $actual);
$this->assertInstanceOf(RespondStatement::class, $actual[0]);

$this->assertEquals(204, $actual[0]->status());
$this->assertNull($actual[0]->content());
}

/**
* @test
*/
public function it_returns_a_response_statement_with_content()
{
$tokens = [
'respond' => 'post'
];

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

$this->assertCount(1, $actual);
$this->assertInstanceOf(RespondStatement::class, $actual[0]);

$this->assertEquals(200, $actual[0]->status());
$this->assertEquals('post', $actual[0]->content());
}

/**
Expand Down Expand Up @@ -444,4 +472,13 @@ public function sessionTokensProvider()
['store', 'post.id'],
];
}

public function eloquentTokensProvider()
{
return [
['save', 'post'],
['update', 'post'],
['delete', 'post.id'],
];
}
}
38 changes: 38 additions & 0 deletions tests/fixtures/controllers/respond-statements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Controllers\Api;

use App\Api\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$posts = Post::all();

return $posts;
}

/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
return response()->noContent();
}

/**
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function error(Request $request)
{
return response()->noContent(400);
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/definitions/respond-statements.bp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
controllers:
Api/Post:
index:
query: all:posts
respond: posts
store:
respond: 204
error:
respond: 400

0 comments on commit e685017

Please sign in to comment.