mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2024-12-26 14:05:29 +03:00
Merge pull request #40 from josiahdahl/laravel-routes
Enable Creating Laravel Style Routes
This commit is contained in:
commit
73e470ec15
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,4 +7,5 @@ tests/_output/*
|
||||
lumen-test/app
|
||||
lumen-test/database
|
||||
lumen-test/tests/tmp
|
||||
|
||||
.idea
|
||||
|
22
README.md
22
README.md
@ -511,6 +511,8 @@ The second command is `wn:controller` which actually generates the controller. T
|
||||
|
||||
- **--force**: tells the generator to override the existing file.
|
||||
|
||||
- **--laravel**: create Laravel style routes
|
||||
|
||||
`php artisan wn:controller Task --no-routes` gives:
|
||||
|
||||
|
||||
@ -539,6 +541,9 @@ The `wn:route` command is used to generate RESTfull routes for a controller. It
|
||||
|
||||
- **--force**: tells the generator to override the existing file.
|
||||
|
||||
- **--laravel**: create Laravel style routes
|
||||
|
||||
|
||||
`php artisan wn:route project-type` adds the following routes:
|
||||
|
||||
```php
|
||||
@ -549,6 +554,17 @@ $app->put('project-type/{id}', 'ProjectTypesController@put');
|
||||
$app->delete('project-type/{id}', 'ProjectTypesController@remove');
|
||||
```
|
||||
|
||||
|
||||
`php artisan wn:route project-type --laravel` adds the following routes:
|
||||
|
||||
```php
|
||||
Route::get('project-type', 'ProjectTypesController@all');
|
||||
Route::get('project-type/{id}', 'ProjectTypesController@get');
|
||||
Route::post('project-type', 'ProjectTypesController@add');
|
||||
Route::put('project-type/{id}', 'ProjectTypesController@put');
|
||||
Route::delete('project-type/{id}', 'ProjectTypesController@remove');
|
||||
```
|
||||
|
||||
### Resource Generator
|
||||
|
||||
The `wn:resource` command makes it very easy to generate a RESTful resource. It generates a model, migration, controller and routes. The syntax is : `wn:resource name fields [--add=...] [--has-many=...] [--has-one=...] [--belongs-to=...] [--migration-file=...] [--path=...] [--force=true]`
|
||||
@ -581,6 +597,9 @@ The `wn:resource` command makes it very easy to generate a RESTful resource. It
|
||||
|
||||
- **--force**: tells the generator to override the existing file.
|
||||
|
||||
- **--laravel**: create Laravel style routes
|
||||
|
||||
|
||||
### Multiple Resources From File
|
||||
|
||||
The `wn:resources` (note the "s" in "resources") command takes the generation process to an other level by parsing a file and generating multiple resources based on it. The syntax is
|
||||
@ -595,6 +614,9 @@ The file given to the command should be a valid YAML file ( for the moment, supp
|
||||
|
||||
- **--path**: Defines where to store the model files as well as their namespace.
|
||||
|
||||
- **--laravel**: create Laravel style routes
|
||||
|
||||
|
||||
```yaml
|
||||
---
|
||||
Store:
|
||||
|
3
lumen-test/.gitignore
vendored
3
lumen-test/.gitignore
vendored
@ -6,3 +6,6 @@ tests/_output/*
|
||||
|
||||
composer.lock
|
||||
tests/_output/*
|
||||
|
||||
codecept.phar
|
||||
|
||||
|
@ -1,5 +1,16 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Application Routes
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here is where you can register all of the routes for an application.
|
||||
| It is a breeze. Simply tell Lumen the URIs it should respond to
|
||||
| and give it the Closure to call when that URI is requested.
|
||||
|
|
||||
*/
|
||||
|
||||
$app->get("/", function () use ($app) {
|
||||
return $app->welcome();
|
||||
});
|
||||
|
@ -11,6 +11,14 @@ echo "<?php
|
||||
return \$app->welcome();
|
||||
});" > app/Http/routes.php
|
||||
|
||||
echo "<?php
|
||||
/*
|
||||
|------------------------------------------
|
||||
| ***** DUMMY ROUTES FOR TESTING ONLY *****
|
||||
|------------------------------------------
|
||||
*/
|
||||
" > routes/api.php
|
||||
|
||||
# Controllers
|
||||
rm app/Http/Controllers/*.php 2> /dev/null
|
||||
echo "<?php
|
||||
|
@ -1,5 +1,16 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Model Factories
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here you may define all of your model factories. Model factories give
|
||||
| you a convenient way to create models for testing and seeding your
|
||||
| database. Just tell the factory how a default model should look.
|
||||
|
|
||||
*/
|
||||
|
||||
$factory->define(App\User::class, function ($faker) {
|
||||
return [
|
||||
'name' => $faker->name,
|
||||
@ -8,4 +19,3 @@ $factory->define(App\User::class, function ($faker) {
|
||||
'remember_token' => str_random(10),
|
||||
];
|
||||
});
|
||||
|
||||
|
124
lumen-test/tests/acceptance/ResourcesWithLaravelRoutesCept.php
Normal file
124
lumen-test/tests/acceptance/ResourcesWithLaravelRoutesCept.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
$I = new AcceptanceTester($scenario);
|
||||
|
||||
$I->wantTo('Generate RESTful resources from a file with Laravel Routes');
|
||||
$I->writeToFile('database/database.sqlite', '');
|
||||
$I->runShellCommand('php artisan wn:resources tests/_data/ResourcesTest.yml --laravel=true');
|
||||
|
||||
// Checking the model
|
||||
$I->seeInShellOutput('Author model generated');
|
||||
$I->seeInShellOutput('Book model generated');
|
||||
$I->seeInShellOutput('Library model generated');
|
||||
$I->seeFileFound('./app/Author.php');
|
||||
$I->seeFileFound('./app/Book.php');
|
||||
$I->seeFileFound('./app/Library.php');
|
||||
$I->deleteFile('./app/Author.php');
|
||||
$I->deleteFile('./app/Book.php');
|
||||
$I->deleteFile('./app/Library.php');
|
||||
|
||||
// Checking the migration
|
||||
$I->seeInShellOutput('authors migration generated');
|
||||
$I->seeInShellOutput('books migration generated');
|
||||
$I->seeInShellOutput('libraries migration generated');
|
||||
// Can't check for specific file names, so we'll just strip the directory
|
||||
$I->cleanDir('database/migrations');
|
||||
$I->writeToFile('database/migrations/.gitkeep', '');
|
||||
|
||||
// Checking the RESTActions trait
|
||||
$I->seeFileFound('./app/Http/Controllers/RESTActions.php');
|
||||
$I->deleteFile('./app/Http/Controllers/RESTActions.php');
|
||||
|
||||
// Checking the controller
|
||||
$I->seeInShellOutput('AuthorsController generated');
|
||||
$I->seeInShellOutput('LibrariesController generated');
|
||||
$I->seeInShellOutput('BooksController generated');
|
||||
$I->seeFileFound('./app/Http/Controllers/AuthorsController.php');
|
||||
$I->seeFileFound('./app/Http/Controllers/LibrariesController.php');
|
||||
$I->seeFileFound('./app/Http/Controllers/BooksController.php');
|
||||
|
||||
$I->deleteFile('./app/Http/Controllers/AuthorsController.php');
|
||||
$I->deleteFile('./app/Http/Controllers/LibrariesController.php');
|
||||
$I->deleteFile('./app/Http/Controllers/BooksController.php');
|
||||
|
||||
$I->seeFileFound('./routes/api.php');
|
||||
|
||||
$I->seeInThisFile('
|
||||
Route::get(\'author\', \'AuthorsController@all\');
|
||||
Route::get(\'author/{id}\', \'AuthorsController@get\');
|
||||
Route::post(\'author\', \'AuthorsController@add\');
|
||||
Route::put(\'author/{id}\', \'AuthorsController@put\');
|
||||
Route::delete(\'author/{id}\', \'AuthorsController@remove\');');
|
||||
|
||||
$I->seeInThisFile('
|
||||
Route::get(\'book\', \'BooksController@all\');
|
||||
Route::get(\'book/{id}\', \'BooksController@get\');
|
||||
Route::post(\'book\', \'BooksController@add\');
|
||||
Route::put(\'book/{id}\', \'BooksController@put\');
|
||||
Route::delete(\'book/{id}\', \'BooksController@remove\');');
|
||||
|
||||
$I->seeInThisFile('
|
||||
Route::get(\'library\', \'LibrariesController@all\');
|
||||
Route::get(\'library/{id}\', \'LibrariesController@get\');
|
||||
Route::post(\'library\', \'LibrariesController@add\');
|
||||
Route::put(\'library/{id}\', \'LibrariesController@put\');
|
||||
Route::delete(\'library/{id}\', \'LibrariesController@remove\');');
|
||||
$I->writeToFile('./app/Http/routes.php', '<?php
|
||||
|
||||
/*
|
||||
|------------------------------------------
|
||||
| ***** DUMMY ROUTES FOR TESTING ONLY *****
|
||||
|------------------------------------------
|
||||
*/
|
||||
');
|
||||
|
||||
// Checking model factory
|
||||
// $I->openFile('./database/factories/ModelFactory.php');
|
||||
// $I->seeInThisFile(
|
||||
// "/**
|
||||
// * Factory definition for model App\TaskCategory.
|
||||
// */
|
||||
// \$factory->define(App\TaskCategory::class, function (\$faker) {
|
||||
// return [
|
||||
// 'name' => \$faker->word,
|
||||
// 'descr' => \$faker->paragraph,
|
||||
// 'due' => \$faker->date,
|
||||
// ];
|
||||
// });");
|
||||
$I->writeToFile('./database/factories/ModelFactory.php', "<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Model Factories
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here you may define all of your model factories. Model factories give
|
||||
| you a convenient way to create models for testing and seeding your
|
||||
| database. Just tell the factory how a default model should look.
|
||||
|
|
||||
*/
|
||||
|
||||
\$factory->define(App\User::class, function (\$faker) {
|
||||
return [
|
||||
'name' => \$faker->name,
|
||||
'email' => \$faker->email,
|
||||
'password' => str_random(10),
|
||||
'remember_token' => str_random(10),
|
||||
];
|
||||
});
|
||||
");
|
||||
|
||||
$I->deleteFile('database/database.sqlite');
|
||||
|
||||
// Checking database seeder
|
||||
// $I->openFile('./database/seeds/TaskCategoriesTableSeeder.php');
|
||||
// $I->seeInThisFile('
|
||||
// use Illuminate\Database\Seeder;
|
||||
|
||||
// class TaskCategoriesTableSeeder extends Seeder
|
||||
// {
|
||||
// public function run()
|
||||
// {
|
||||
// factory(App\TaskCategory::class, 10)->create();
|
||||
// }
|
||||
// }');
|
||||
// $I->deleteFile('./database/seeds/TaskCategoriesTableSeeder.php');
|
@ -62,7 +62,9 @@ $app->get("/", function () use ($app) {
|
||||
|
||||
|
||||
$I->wantTo('run wn:routes in Lumen 5.3+');
|
||||
mkdir('./routes');
|
||||
if(!file_exists('./routes')) {
|
||||
mkdir('./routes');
|
||||
}
|
||||
$I->writeToFile('./routes/web.php', '<?php
|
||||
|
||||
/*
|
||||
|
@ -1,12 +1,15 @@
|
||||
<?php namespace Wn\Generators\Commands;
|
||||
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class ControllerCommand extends BaseCommand {
|
||||
|
||||
protected $signature = 'wn:controller
|
||||
{model : Name of the model (with namespace if not App)}
|
||||
{--no-routes= : without routes}
|
||||
{--force= : override the existing files}
|
||||
{--laravel : Use Laravel style route definitions}
|
||||
';
|
||||
|
||||
protected $description = 'Generates RESTful controller using the RESTActions trait';
|
||||
@ -31,12 +34,17 @@ class ControllerCommand extends BaseCommand {
|
||||
->get();
|
||||
|
||||
$this->save($content, "./app/Http/Controllers/{$controller}.php", "{$controller}");
|
||||
|
||||
if(! $this->option('no-routes')){
|
||||
$this->call('wn:route', [
|
||||
$options = [
|
||||
'resource' => snake_case($name, '-'),
|
||||
'--controller' => $controller
|
||||
]);
|
||||
'--controller' => $controller,
|
||||
];
|
||||
|
||||
if ($this->option('laravel')) {
|
||||
$options['--laravel'] = true;
|
||||
}
|
||||
|
||||
$this->call('wn:route', $options);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
<?php namespace Wn\Generators\Commands;
|
||||
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class ResourceCommand extends BaseCommand {
|
||||
|
||||
protected $signature = 'wn:resource
|
||||
@ -15,6 +17,7 @@ class ResourceCommand extends BaseCommand {
|
||||
{--path=app : where to store the model file.}
|
||||
{--parsed : tells the command that arguments have been already parsed. To use when calling the command from an other command and passing the parsed arguments and options}
|
||||
{--force= : override the existing files}
|
||||
{--laravel= : Use Laravel style route definitions}
|
||||
';
|
||||
|
||||
protected $description = 'Generates a model, migration, controller and routes for RESTful resource';
|
||||
@ -61,13 +64,16 @@ class ResourceCommand extends BaseCommand {
|
||||
if(! $this->fs->exists('./app/Http/Controllers/RESTActions.php')){
|
||||
$this->call('wn:controller:rest-actions');
|
||||
}
|
||||
|
||||
// generating the controller and routes
|
||||
$this->call('wn:controller', [
|
||||
$controllerOptions = [
|
||||
'model' => $modelName,
|
||||
'--force' => $this->option('force'),
|
||||
'--no-routes' => false
|
||||
]);
|
||||
'--no-routes' => false,
|
||||
];
|
||||
if ($this->option('laravel')) {
|
||||
$controllerOptions['--laravel'] = true;
|
||||
}
|
||||
$this->call('wn:controller', $controllerOptions);
|
||||
|
||||
// generating model factory
|
||||
$this->call('wn:factory', [
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php namespace Wn\Generators\Commands;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
|
||||
@ -9,6 +10,8 @@ class ResourcesCommand extends BaseCommand {
|
||||
{file : Path to the file containing resources declarations}
|
||||
{--path=app : where to store the model files.}
|
||||
{--force= : override the existing files}
|
||||
{--laravel= : Use Laravel style route definitions}
|
||||
|
||||
';
|
||||
|
||||
protected $description = 'Generates multiple resources from a file';
|
||||
@ -27,7 +30,7 @@ class ResourcesCommand extends BaseCommand {
|
||||
$migrationFile = date('Y_m_d_His') . '-' . str_pad($modelIndex , 3, 0, STR_PAD_LEFT) . '_' . snake_case($migrationName) . '_table';
|
||||
|
||||
|
||||
$this->call('wn:resource', [
|
||||
$options = [
|
||||
'name' => $i['name'],
|
||||
'fields' => $i['fields'],
|
||||
'--add' => $i['add'],
|
||||
@ -38,7 +41,12 @@ class ResourcesCommand extends BaseCommand {
|
||||
'--path' => $this->option('path'),
|
||||
'--force' => $this->option('force'),
|
||||
'--migration-file' => $migrationFile
|
||||
]);
|
||||
];
|
||||
if ($this->option('laravel')) {
|
||||
$options['--laravel'] = true;
|
||||
}
|
||||
|
||||
$this->call('wn:resource', $options);
|
||||
$modelIndex++;
|
||||
}
|
||||
|
||||
|
@ -1,31 +1,66 @@
|
||||
<?php namespace Wn\Generators\Commands;
|
||||
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class RouteCommand extends BaseCommand {
|
||||
|
||||
protected $signature = 'wn:route
|
||||
{resource : Name of the resource.}
|
||||
{--controller= : Name of the RESTful controller.}';
|
||||
{--controller= : Name of the RESTful controller.}
|
||||
{--laravel= : Use Laravel style route definitions}
|
||||
';
|
||||
|
||||
protected $description = 'Generates RESTful routes.';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$resource = $this->argument('resource');
|
||||
$laravelRoutes = $this->option('laravel');
|
||||
$templateFile = 'routes';
|
||||
$routesPath = 'routes/web.php';
|
||||
if ($laravelRoutes) {
|
||||
$templateFile = 'routes-laravel';
|
||||
$routesPath = 'routes/api.php';
|
||||
if (!$this->fs->isFile($routesPath)) {
|
||||
if (!$this->fs->isDirectory('./routes')) {
|
||||
$this->fs->makeDirectory('./routes');
|
||||
}
|
||||
$this->fs->put($routesPath, "
|
||||
<?php
|
||||
|
||||
$routesPath = './routes/web.php';
|
||||
if (! $this->fs->exists($routesPath))
|
||||
$routesPath = './app/Http/routes.php';
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| API Routes
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| Here is where you can register API routes for your application. These
|
||||
| routes are loaded by the RouteServiceProvider within a group which
|
||||
| is assigned the \"api\" middleware group. Enjoy building your API!
|
||||
|
|
||||
*/
|
||||
|
||||
Route::middleware('auth:api')->get('/user', function (Request \$request) {
|
||||
return \$request->user();
|
||||
});
|
||||
|
||||
");
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->fs->isFile($routesPath)) {
|
||||
$routesPath = 'app/Http/routes.php';
|
||||
}
|
||||
$content = $this->fs->get($routesPath);
|
||||
|
||||
$content .= PHP_EOL . $this->getTemplate('routes')
|
||||
$content .= PHP_EOL . $this->getTemplate($templateFile)
|
||||
->with([
|
||||
'resource' => $resource,
|
||||
'controller' => $this->getController()
|
||||
])
|
||||
->get();
|
||||
|
||||
$this->save($content, $routesPath, "{$resource} routes", true);
|
||||
}
|
||||
|
||||
|
8
templates/routes-laravel.wnt
Normal file
8
templates/routes-laravel.wnt
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Routes for resource {{resource}}
|
||||
*/
|
||||
Route::get('{{resource}}', '{{controller}}@all');
|
||||
Route::get('{{resource}}/{id}', '{{controller}}@get');
|
||||
Route::post('{{resource}}', '{{controller}}@add');
|
||||
Route::put('{{resource}}/{id}', '{{controller}}@put');
|
||||
Route::delete('{{resource}}/{id}', '{{controller}}@remove');
|
Loading…
Reference in New Issue
Block a user