Skip to content

Commit e5a699d

Browse files
authored
Merge pull request #5 from Lomkit/quick-start
✨ quick start
2 parents 55441a4 + 7639216 commit e5a699d

File tree

6 files changed

+157
-4
lines changed

6 files changed

+157
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ TODO
8484
- Actions / Metrics
8585
- Automatic Gates
8686
- Aggregating (V2)
87-
- Automatic documentation with extending possible
87+
- Automatic documentation with extension possible
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
7+
use Lomkit\Rest\Console\ResolvesStubPath;
8+
9+
class QuickStartCommand extends Command
10+
{
11+
use ResolvesStubPath;
12+
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'rest:quick-start';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Demonstrate the app using user related resource registering.';
26+
27+
/**
28+
* Execute the console command.
29+
*
30+
* @return void
31+
*/
32+
public function handle()
33+
{
34+
$this->comment('Generating User Resource...');
35+
$this->callSilent('rest:resource', ['name' => 'UserResource']);
36+
copy($this->resolveStubPath('../stubs/user-resource.stub'), app_path('Rest/Resources/UserResource.php'));
37+
38+
if (file_exists(app_path('Models/User.php'))) {
39+
file_put_contents(
40+
app_path('Rest/Resources/UserResource.php'),
41+
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Resources/UserResource.php')))
42+
);
43+
}
44+
45+
$this->comment('Generating User Controller...');
46+
$this->callSilent('rest:controller', ['name' => 'UsersController']);
47+
copy($this->resolveStubPath('../stubs/user-controller.stub'), app_path('Http/Controllers/UsersController.php'));
48+
49+
if (file_exists(app_path('Models/User.php'))) {
50+
file_put_contents(
51+
app_path('Http/Controllers/UsersController.php'),
52+
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Http/Controllers/UsersController.php')))
53+
);
54+
}
55+
56+
$this->setAppNamespace();
57+
58+
if (file_exists(base_path('routes/api.php'))) {
59+
file_put_contents(
60+
base_path('routes/api.php'),
61+
file_get_contents(base_path('routes/api.php')).
62+
'\Lomkit\Rest\Facades\Rest::resource(\'users\', \App\Http\Controllers\UsersController::class);'
63+
);
64+
}
65+
66+
$this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !');
67+
}
68+
69+
/**
70+
* Set the proper application namespace on the installed files.
71+
*
72+
* @return void
73+
*/
74+
protected function setAppNamespace()
75+
{
76+
$namespace = $this->laravel->getNamespace();
77+
78+
$this->setAppNamespaceOn(app_path('Rest/Resource/UserResource.php'), $namespace);
79+
$this->setAppNamespaceOn(app_path('Http/Controllers/UsersController.php'), $namespace);
80+
}
81+
82+
/**
83+
* Set the namespace on the given file.
84+
*
85+
* @param string $file
86+
* @param string $namespace
87+
* @return void
88+
*/
89+
protected function setAppNamespaceOn($file, $namespace)
90+
{
91+
file_put_contents($file, str_replace(
92+
'App\\',
93+
$namespace,
94+
file_get_contents($file)
95+
));
96+
}
97+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Rest\Controller as RestController;
6+
7+
class UsersController extends RestController
8+
{
9+
/**
10+
* The resource the controller corresponds to.
11+
*
12+
* @var class-string<\Lomkit\Rest\Http\Resource>
13+
*/
14+
public static $resource = \App\User::class;
15+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Rest\Resources;
4+
5+
use App\Rest\Resource as RestResource;
6+
7+
class User extends Resource
8+
{
9+
/**
10+
* The model the resource corresponds to.
11+
*
12+
* @var class-string<\Illuminate\Database\Eloquent\Model>
13+
*/
14+
public static $model = \App\User::class;
15+
16+
public function exposedFields(\Lomkit\Rest\Http\Requests\RestRequest $request)
17+
{
18+
return [
19+
'id',
20+
'name',
21+
'email'
22+
];
23+
}
24+
25+
public function relations(\Lomkit\Rest\Http\Requests\RestRequest $request)
26+
{
27+
return [];
28+
}
29+
30+
public function exposedScopes(\Lomkit\Rest\Http\Requests\RestRequest $request) {
31+
return [];
32+
}
33+
34+
public function exposedPaginations(\Lomkit\Rest\Http\Requests\RestRequest $request) {
35+
return [
36+
10,
37+
25,
38+
50
39+
];
40+
}
41+
}

src/Http/Controllers/Controller.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ abstract class Controller extends \Illuminate\Routing\Controller
1818
use PerformsRestOperations,
1919
Authorizable;
2020

21-
// @TODO Make an installation command that provides base controller, resource, etc
22-
2321
/**
2422
* The resource the entry corresponds to.
2523
*

src/RestServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Lomkit\Rest\Console\Commands\BaseControllerCommand;
88
use Lomkit\Rest\Console\Commands\BaseResourceCommand;
99
use Lomkit\Rest\Console\Commands\ControllerCommand;
10+
use Lomkit\Rest\Console\Commands\QuickStartCommand;
1011
use Lomkit\Rest\Console\Commands\ResourceCommand;
1112
use Lomkit\Rest\Console\Commands\ResponseCommand;
1213
use Lomkit\Rest\Contracts\QueryBuilder;
@@ -46,7 +47,8 @@ protected function registerCommands()
4647
ControllerCommand::class,
4748
BaseResourceCommand::class,
4849
ResourceCommand::class,
49-
ResponseCommand::class
50+
ResponseCommand::class,
51+
QuickStartCommand::class
5052
]);
5153
}
5254
}

0 commit comments

Comments
 (0)