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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ TODO
- Actions / Metrics
- Automatic Gates
- Aggregating (V2)
- Automatic documentation with extending possible
- Automatic documentation with extension possible
97 changes: 97 additions & 0 deletions src/Console/Commands/QuickStartCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace Lomkit\Rest\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Lomkit\Rest\Console\ResolvesStubPath;

class QuickStartCommand extends Command
{
use ResolvesStubPath;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'rest:quick-start';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Demonstrate the app using user related resource registering.';

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$this->comment('Generating User Resource...');
$this->callSilent('rest:resource', ['name' => 'UserResource']);
copy($this->resolveStubPath('../stubs/user-resource.stub'), app_path('Rest/Resources/UserResource.php'));

if (file_exists(app_path('Models/User.php'))) {
file_put_contents(
app_path('Rest/Resources/UserResource.php'),
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Rest/Resources/UserResource.php')))
);
}

$this->comment('Generating User Controller...');
$this->callSilent('rest:controller', ['name' => 'UsersController']);
copy($this->resolveStubPath('../stubs/user-controller.stub'), app_path('Http/Controllers/UsersController.php'));

if (file_exists(app_path('Models/User.php'))) {
file_put_contents(
app_path('Http/Controllers/UsersController.php'),
str_replace('App\User::class', 'App\Models\User::class', file_get_contents(app_path('Http/Controllers/UsersController.php')))
);
}

$this->setAppNamespace();

if (file_exists(base_path('routes/api.php'))) {
file_put_contents(
base_path('routes/api.php'),
file_get_contents(base_path('routes/api.php')).
'\Lomkit\Rest\Facades\Rest::resource(\'users\', \App\Http\Controllers\UsersController::class);'
);
}

$this->info('Laravel Rest Api is ready. Type \'php artisan route:list\' to see your new routes !');
}

/**
* Set the proper application namespace on the installed files.
*
* @return void
*/
protected function setAppNamespace()
{
$namespace = $this->laravel->getNamespace();

$this->setAppNamespaceOn(app_path('Rest/Resource/UserResource.php'), $namespace);
$this->setAppNamespaceOn(app_path('Http/Controllers/UsersController.php'), $namespace);
}

/**
* Set the namespace on the given file.
*
* @param string $file
* @param string $namespace
* @return void
*/
protected function setAppNamespaceOn($file, $namespace)
{
file_put_contents($file, str_replace(
'App\\',
$namespace,
file_get_contents($file)
));
}
}
15 changes: 15 additions & 0 deletions src/Console/stubs/user-controller.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Controllers;

use App\Rest\Controller as RestController;

class UsersController extends RestController
{
/**
* The resource the controller corresponds to.
*
* @var class-string<\Lomkit\Rest\Http\Resource>
*/
public static $resource = \App\User::class;
}
41 changes: 41 additions & 0 deletions src/Console/stubs/user-resource.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Rest\Resources;

use App\Rest\Resource as RestResource;

class User extends Resource
{
/**
* The model the resource corresponds to.
*
* @var class-string<\Illuminate\Database\Eloquent\Model>
*/
public static $model = \App\User::class;

public function exposedFields(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
return [
'id',
'name',
'email'
];
}

public function relations(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
return [];
}

public function exposedScopes(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [];
}

public function exposedPaginations(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [
10,
25,
50
];
}
}
2 changes: 0 additions & 2 deletions src/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ abstract class Controller extends \Illuminate\Routing\Controller
use PerformsRestOperations,
Authorizable;

// @TODO Make an installation command that provides base controller, resource, etc

/**
* The resource the entry corresponds to.
*
Expand Down
4 changes: 3 additions & 1 deletion src/RestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Lomkit\Rest\Console\Commands\BaseControllerCommand;
use Lomkit\Rest\Console\Commands\BaseResourceCommand;
use Lomkit\Rest\Console\Commands\ControllerCommand;
use Lomkit\Rest\Console\Commands\QuickStartCommand;
use Lomkit\Rest\Console\Commands\ResourceCommand;
use Lomkit\Rest\Console\Commands\ResponseCommand;
use Lomkit\Rest\Contracts\QueryBuilder;
Expand Down Expand Up @@ -46,7 +47,8 @@ protected function registerCommands()
ControllerCommand::class,
BaseResourceCommand::class,
ResourceCommand::class,
ResponseCommand::class
ResponseCommand::class,
QuickStartCommand::class
]);
}
}
Expand Down