Skip to content

Commit

Permalink
Major updates
Browse files Browse the repository at this point in the history
  • Loading branch information
craigAtCD committed Jul 11, 2019
1 parent edc24d6 commit 820d9fb
Show file tree
Hide file tree
Showing 7 changed files with 648 additions and 152 deletions.
19 changes: 10 additions & 9 deletions config/laravel-api-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
* Relative path from the app directory to api controllers directory.
*/
'controllers_dir' => 'Http/Controllers/Api',
/*
* Relative path from the app directory to api repositories directory.
*/
'repositorys_dir' => 'Repositories/Api',
/*
* Relative path from the app directory to the api routes file.
*/
Expand All @@ -20,13 +16,18 @@
* Relative path from the base directory to the api controller stub.
*/
'controller_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub',
/*
* Relative path from the base directory to the api controller stub.
*/
'repository_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/repository.stub',
/*
* Relative path from the base directory to the route stub.
*/
'route_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub',
'route_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub',

'parameters' => [
'include' => 'include',
'filter' => 'filter',
'sort' => 'sort',
'fields' => 'fields',
'page' => 'page',
'group' => 'group'
],

];
49 changes: 19 additions & 30 deletions src/Generator/ApiMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Input\InputArgument;

use Illuminate\Support\Str;

class ApiMakeCommand extends Command
{
Expand Down Expand Up @@ -37,8 +37,7 @@ class ApiMakeCommand extends Command
'app' => [],
'model' => [],
'controller' => [],
'route' => [],
'repository' => []
'route' => []
];

protected $modelsBaseNamespace;
Expand All @@ -61,7 +60,6 @@ public function handle()
{
$this->prepareVariablesForStubs($this->argument('name'));
$this->createController();
$this->createRepository();
$this->addRoutes();
}
/**
Expand All @@ -76,8 +74,7 @@ protected function prepareVariablesForStubs($name)
$this->modelsBaseNamespace = $baseDir ? trim($baseDir, '\\').'\\' : '';
$this->setModelData($name)
->setControllerData()
->setRouteData()
->setRepositoryData();
->setRouteData();
}
/**
* Set the model name and namespace.
Expand All @@ -86,7 +83,7 @@ protected function prepareVariablesForStubs($name)
*/
protected function setModelData($name)
{
if (str_contains($name, '/')) {
if (Str::contains($name, '/')) {
$name = $this->convertSlashes($name);
}
$name = trim($name, '\\');
Expand Down Expand Up @@ -118,19 +115,11 @@ protected function setControllerData()
protected function setRouteData()
{
$name = str_replace('\\', '', $this->stubVariables['model']['fullNameWithoutRoot']);
$name = snake_case($name);
$this->stubVariables['route']['name'] = str_plural($name);
$name = Str::snake($name);
$this->stubVariables['route']['name'] = Str::plural($name);
return $this;
}
/**
* Set the names and namespaces.
*
* @return $this
*/
protected function setRepositoryData()
{
return $this->setDataForEntity('repository');
}

/**
* Set entity's names and namespaces.
*
Expand Down Expand Up @@ -159,25 +148,21 @@ protected function createController()
{
$this->createClass('controller');
}
/**
* Create controller class file from a stub.
*/
protected function createRepository()
{
$this->createClass('repository');
}


/**
* Add routes to routes file.
*/
protected function addRoutes()
{

$stub = $this->constructStub(base_path(config('laravel-api-controller.route_stub')));
$routesFile = app_path(config('laravel-api-controller.routes_file'));


// read file
$lines = file($routesFile);
$lines = file($routesFile);
if(!$lines){
//@todo - better error handling here
return false;
}
$lastLine = trim($lines[count($lines) - 1]);
// modify file
if (strcmp($lastLine, '});') === 0) {
Expand All @@ -187,7 +172,11 @@ protected function addRoutes()
$lines[] = "$stub\r\n";
}
// save file
$fp = fopen($routesFile, 'w');
$fp = fopen($routesFile, 'w');
if(!$fp){
//@todo - better error handling here
return false;
}
fwrite($fp, implode('', $lines));
fclose($fp);
$this->info('Routes added successfully.');
Expand Down
13 changes: 2 additions & 11 deletions src/Generator/stubs/controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Phpsa\LaravelApiController\Http\Api\Controller;
use {{model.fullName}};
use {{repository.fullName}};

class {{controller.name}} extends Controller
{
Expand All @@ -13,16 +12,8 @@ class {{controller.name}} extends Controller
*/
protected function model()
{
return new {{model.name}};
return {{model.name}}::class;
}

/**
* Repository for the current model.
*
* @return App\Repositories\BaseRepository
*/
protected function repository()
{
return new {{repository.name}};
}

}
19 changes: 0 additions & 19 deletions src/Generator/stubs/repository.stub

This file was deleted.

0 comments on commit 820d9fb

Please sign in to comment.