Skip to content

Commit 591c452

Browse files
authored
Features for the 3.x (#160)
* Valid codes * Generator * Add Stub Command * Index Query * With relationships * Field tests * Mergeable repositories * Default callback. Default resolver. Computed attributes. * Performance * Show fields works
1 parent 05c9983 commit 591c452

File tree

62 files changed

+2438
-1543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2438
-1543
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"require": {
2121
"php": "^7.4",
2222
"ext-json": "*",
23-
"illuminate/support": "^6.0|^7.0"
23+
"illuminate/support": "^6.0|^7.0",
24+
"doctrine/dbal": "^2.10"
2425
},
2526
"require-dev": {
2627
"mockery/mockery": "^1.3",

config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
|
4242
*/
4343

44-
'base' => '/restify-api',
44+
'base' => '/api/restify',
4545

4646
/*
4747
|--------------------------------------------------------------------------

phpunit.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="true">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
<exclude>
21+
<directory suffix="CheckPassport.php">src/Commands</directory>
22+
</exclude>
23+
</whitelist>
24+
</filter>
25+
<!-- <logging>-->
26+
<!-- <log type="tap" target="build/report.tap"/>-->
27+
<!-- <log type="junit" target="build/report.junit.xml"/>-->
28+
<!-- <log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>-->
29+
<!-- <log type="coverage-text" target="build/coverage.txt"/>-->
30+
<!-- <log type="coverage-clover" target="build/logs/clover.xml"/>-->
31+
<!-- </logging>-->
32+
<php>
33+
<env name="APP_KEY" value="base64:la8jDWcqBHfGO6PR+OA9FAZqdi0XQKuhnzqc5tUATZs="/>
34+
<env name="MAIL_DRIVER" value="log"/>
35+
</php>
36+
</phpunit>

routes/api.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<?php
22

3-
Route::get('/{repository}', 'RepositoryIndexController@handle');
4-
Route::post('/{repository}', 'RepositoryStoreController@handle');
5-
Route::get('/{repository}/{repositoryId}', 'RepositoryShowController@handle');
6-
Route::patch('/{repository}/{repositoryId}', 'RepositoryUpdateController@handle');
7-
Route::put('/{repository}/{repositoryId}', 'RepositoryUpdateController@handle');
8-
Route::delete('/{repository}/{repositoryId}', 'RepositoryDestroyController@handle');
3+
use Binaryk\LaravelRestify\Http\Controllers\RepositoryDestroyController;
4+
use Binaryk\LaravelRestify\Http\Controllers\RepositoryIndexController;
5+
use Binaryk\LaravelRestify\Http\Controllers\RepositoryShowController;
6+
use Binaryk\LaravelRestify\Http\Controllers\RepositoryStoreController;
7+
use Binaryk\LaravelRestify\Http\Controllers\RepositoryUpdateController;
8+
9+
Route::get('/{repository}', '\\'.RepositoryIndexController::class);
10+
Route::post('/{repository}', '\\'.RepositoryStoreController::class);
11+
Route::get('/{repository}/{repositoryId}', '\\'.RepositoryShowController::class);
12+
Route::patch('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class);
13+
Route::put('/{repository}/{repositoryId}', '\\'.RepositoryUpdateController::class);
14+
Route::delete('/{repository}/{repositoryId}', '\\'.RepositoryDestroyController::class);

src/Commands/BaseRepositoryCommand.php

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,24 @@
66

77
class BaseRepositoryCommand extends GeneratorCommand
88
{
9-
/**
10-
* The console command name.
11-
*
12-
* @var string
13-
*/
149
protected $name = 'restify:base-repository';
1510

16-
/**
17-
* The console command description.
18-
*
19-
* @var string
20-
*/
2111
protected $description = 'Create a new base repository class';
2212

23-
/**
24-
* Indicates whether the command should be shown in the Artisan command list.
25-
*
26-
* @var bool
27-
*/
2813
protected $hidden = true;
2914

30-
/**
31-
* The type of class being generated.
32-
*
33-
* @var string
34-
*/
3515
protected $type = 'Repository';
3616

37-
/**
38-
* Execute the console command.
39-
*
40-
* @return bool|null
41-
*/
4217
public function handle()
4318
{
4419
parent::handle();
4520
}
4621

47-
/**
48-
* Get the stub file for the generator.
49-
*
50-
* @return string
51-
*/
5222
protected function getStub()
5323
{
5424
return __DIR__.'/stubs/base-repository.stub';
5525
}
5626

57-
/**
58-
* Get the default namespace for the class.
59-
*
60-
* @param string $rootNamespace
61-
* @return string
62-
*/
6327
protected function getDefaultNamespace($rootNamespace)
6428
{
6529
return $rootNamespace.'\Restify';

src/Commands/PolicyCommand.php

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,90 +6,66 @@
66
use Illuminate\Support\Str;
77
use Symfony\Component\Console\Input\InputOption;
88

9-
/**
10-
* @author Eduard Lupacescu <[email protected]>
11-
*/
129
class PolicyCommand extends GeneratorCommand
1310
{
14-
/**
15-
* The console command name.
16-
*
17-
* @var string
18-
*/
1911
protected $name = 'restify:policy';
2012

21-
/**
22-
* The console command description.
23-
*
24-
* @var string
25-
*/
2613
protected $description = 'Create a new policy for a specific model.';
2714

28-
/**
29-
* The type of class being generated.
30-
*
31-
* @var string
32-
*/
33-
protected $type = 'Policy';
34-
35-
/**
36-
* Execute the console command.
37-
*
38-
* @return bool|null
39-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
40-
*/
4115
public function handle()
4216
{
43-
parent::handle();
17+
if (parent::handle() === false && ! $this->option('force')) {
18+
return false;
19+
}
4420
}
4521

46-
/**
47-
* Build the class with the given name.
48-
*
49-
* @param string $name
50-
* @return string
51-
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
52-
*/
5322
protected function buildClass($name)
5423
{
55-
$namespacedModel = null;
56-
$model = $this->option('model');
24+
$class = $this->replaceModel(parent::buildClass($name));
5725

58-
if (is_null($model)) {
59-
$model = $this->argument('name');
60-
}
26+
$class = $this->replaceQualifiedModel($class);
6127

62-
if ($model && ! Str::startsWith($model, [$this->laravel->getNamespace(), '\\'])) {
63-
$namespacedModel = $this->laravel->getNamespace().$model;
64-
}
28+
return $class;
29+
}
6530

66-
$name .= 'Policy';
31+
protected function replaceClass($stub, $name)
32+
{
33+
$class = str_replace($this->getNamespace($name).'\\', '', $this->guessPolicyName());
6734

68-
$rendered = str_replace(
69-
'UseDummyModel', $namespacedModel ?? $model, parent::buildClass($name)
70-
);
35+
return str_replace(['{{ class }}', '{{class}}'], $class, $stub);
36+
}
7137

72-
$rendered = str_replace(
73-
'DummyModel', $model, $rendered
74-
);
38+
protected function replaceModel($stub)
39+
{
40+
return str_replace(['{{ model }}', '{{model}}'], class_basename($this->guessQualifiedModel()), $stub);
41+
}
42+
43+
protected function replaceQualifiedModel($stub)
44+
{
45+
return str_replace('{{ modelQualified }}', $this->guessQualifiedModel(), $stub);
46+
}
47+
48+
protected function guessQualifiedModel(): string
49+
{
50+
$model = Str::singular(class_basename(Str::beforeLast($this->getNameInput(), 'Policy')));
7551

76-
return $rendered;
52+
return str_replace('/', '\\', $this->rootNamespace().'Models/'.$model);
7753
}
7854

79-
public function nameWithEnd()
55+
protected function guessPolicyName()
8056
{
81-
$model = $this->option('model');
57+
$name = $this->getNameInput();
8258

83-
if (is_null($model)) {
84-
$model = $this->argument('name');
59+
if (false === Str::endsWith($name, 'Policy')) {
60+
$name .= 'Policy';
8561
}
8662

87-
return $model.'Policy';
63+
return $name;
8864
}
8965

9066
protected function getPath($name)
9167
{
92-
return $this->laravel['path'].'/Policies/'.$this->nameWithEnd().'.php';
68+
return $this->laravel['path'].'/Policies/'.$this->guessPolicyName().'.php';
9369
}
9470

9571
/**
@@ -105,12 +81,12 @@ protected function getStub()
10581
/**
10682
* Get the default namespace for the class.
10783
*
108-
* @param string $rootNamespace
84+
* @param string $rootNamespace
10985
* @return string
11086
*/
11187
protected function getDefaultNamespace($rootNamespace)
11288
{
113-
return $rootNamespace.'\Restify';
89+
return $rootNamespace.'\Policies';
11490
}
11591

11692
/**
@@ -122,6 +98,7 @@ protected function getOptions()
12298
{
12399
return [
124100
['model', 'm', InputOption::VALUE_REQUIRED, 'The model class being protected.'],
101+
['force', null, InputOption::VALUE_NONE, 'Create the class even if the model already exists.'],
125102
];
126103
}
127104
}

0 commit comments

Comments
 (0)