Skip to content

Commit

Permalink
feat(L8): update resources to be compatible with L8 [WIP] (#32)
Browse files Browse the repository at this point in the history
* feat(upgrade): add support for L8

* feat(upgrade): add support for L8

* feat(factories): update factory generator

* Apply fixes from StyleCI (#31)

* feat(routes): update routes generator

* Apply fixes from StyleCI (#33)

* feat(models): update models generator

* Apply fixes from StyleCI (#34)

* feat(seeds): update seeders generator

* Apply fixes from StyleCI (#35)

* feat(docs): update docs

* Apply fixes from StyleCI (#36)

* feat(config): update config seed path

* fix: tests

Co-authored-by: Tiago Sousa <[email protected]>
  • Loading branch information
tiagomichaelsousa and Tiago Sousa authored Sep 29, 2020
1 parent 62155f4 commit 965c4b7
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 132 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

Laravel Resources is a speed-up development package that allows you to create a boilerplate for Laravel apps with a default API structure.

## Versions

| Laravel | Laravel Resources |
| ------- | ----------------- |
| 7.x | v1.7 |
| 8.x | v2.x |

## Installation

Via Composer
Expand All @@ -46,9 +53,9 @@ $ php artisan vendor:publish --provider="tiagomichaelsousa\LaravelResources\Lara

**Notes:**

- This package is fully configurable. You can change all the namespaces for the resources that will be created in the config file.
- Don't forget to edit the request file in order to add your default validation for the model.
- Don't forget to edit the policy file in order to fulfill your app business logic.
- This package is fully configurable. You can change all the namespaces for the resources that will be created in the config file.
- Don't forget to edit the request file in order to add your default validation for the model.
- Don't forget to edit the policy file in order to fulfill your app business logic.

## Change log

Expand Down Expand Up @@ -76,8 +83,8 @@ If you discover any security related issues, please email the [author](mailto:ti

## Credits

- [@tiagomichaelsousa][link-author]
- [All Contributors][link-contributors]
- [@tiagomichaelsousa][link-author]
- [All Contributors][link-contributors]

## License

Expand Down Expand Up @@ -116,6 +123,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
104 changes: 52 additions & 52 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,56 @@
{
"name": "tiagomichaelsousa/laravelresources",
"description": "Laravel Resources is a speed-up development package that allows you to create a boilerplate for Laravel apps with a default API structure.",
"license": "MIT",
"authors": [
{
"name": "tiagomichaelsousa",
"email": "[email protected]",
"homepage": "https://github.com/tiagomichaelsousa",
"role": "Developer"
}
],
"homepage": "https://github.com/tiagomichaelsousa/laravelresources",
"keywords": [
"Laravel",
"LaravelResources",
"laravelresources",
"laravel-resources"
],
"require": {
"illuminate/support": "^7.0|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.0",
"mockery/mockery": "^1.3.1",
"orchestra/testbench": "^5.0|^6.0",
"sempro/phpunit-pretty-print": "^1.0"
},
"autoload": {
"files": [
"src/Helpers/helpers.php"
"name": "tiagomichaelsousa/laravelresources",
"description": "Laravel Resources is a speed-up development package that allows you to create a boilerplate for Laravel apps with a default API structure.",
"license": "MIT",
"authors": [
{
"name": "tiagomichaelsousa",
"email": "[email protected]",
"homepage": "https://github.com/tiagomichaelsousa",
"role": "Developer"
}
],
"psr-4": {
"tiagomichaelsousa\\LaravelResources\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"tiagomichaelsousa\\LaravelResources\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-report": "vendor/bin/phpunit --coverage-html report"
},
"extra": {
"laravel": {
"providers": [
"tiagomichaelsousa\\LaravelResources\\LaravelResourcesServiceProvider"
],
"aliases": {
"LaravelResources": "tiagomichaelsousa\\LaravelResources\\Facades\\LaravelResources"
}
"homepage": "https://github.com/tiagomichaelsousa/laravelresources",
"keywords": [
"Laravel",
"LaravelResources",
"laravelresources",
"laravel-resources"
],
"require": {
"illuminate/support": "^7.0|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5|^9.0",
"mockery/mockery": "^1.3.1",
"orchestra/testbench": "^5.0|^6.0",
"sempro/phpunit-pretty-print": "^1.0"
},
"autoload": {
"files": [
"src/Helpers/helpers.php"
],
"psr-4": {
"tiagomichaelsousa\\LaravelResources\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"tiagomichaelsousa\\LaravelResources\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-report": "vendor/bin/phpunit --coverage-html report"
},
"extra": {
"laravel": {
"providers": [
"tiagomichaelsousa\\LaravelResources\\LaravelResourcesServiceProvider"
],
"aliases": {
"LaravelResources": "tiagomichaelsousa\\LaravelResources\\Facades\\LaravelResources"
}
}
}
}
}
4 changes: 2 additions & 2 deletions config/laravel-resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
|
*/
'models' => [
'namespace' => 'App',
'namespace' => 'App\Models',
],

/*
Expand Down Expand Up @@ -116,6 +116,6 @@
'database' => [
'migrations' => 'database/migrations',
'factories' => 'database/factories',
'seeds' => 'database/seeds',
'seeds' => 'database/seeders',
],
];
31 changes: 11 additions & 20 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Laravel Resources Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Laravel Resources Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 3 additions & 3 deletions src/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ private function createResources()

$bar->finish();
$this->line('');
$this->line('');

exec('composer dumpautoload');

$this->line('');

$this->info('🚀 Resources created successfully 🚀');
$this->line('');

exec('composer dumpautoload');
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function replacements()
return array_merge([
'{{MODEL_NAMESPACE}}' => config('laravel-resources.models.namespace'),
'{{MODEL_NAME}}' => $this->model,
'{{CLASS_NAME}}' => $this->className(),
]);
}

Expand Down
19 changes: 17 additions & 2 deletions src/Generators/RouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function replacements()
return array_merge([
'{{MODEL_NAME}}' => $this->model,
'{{MODEL_VARIABLE}}' => lcfirst($this->model),
'{{CONTROLLER_NAME}}' => create_class_name($this->model, 'ControllerGenerator'),
'{{CONTROLLER_NAME}}' => $this->generateControllerName(),
'{{ROUTE_NAME}}' => Str::plural(Str::kebab($this->model)),
]);
}
Expand Down Expand Up @@ -83,6 +83,21 @@ public function handle()

$this->directoryExists($directory);

file_put_contents($path, [$stub, PHP_EOL], FILE_APPEND);
$controllerNamespace = config('laravel-resources.controllers.namespace');
$controllerImport = "use {$controllerNamespace}\\{$this->generateControllerName()}";
$content = str_replace("<?php\n", "<?php\n\n$controllerImport;", file_get_contents($path));
file_put_contents($path, $content);

file_put_contents($path, [$stub, PHP_EOL], FILE_APPEND | FILE_USE_INCLUDE_PATH);
}

/**
* Generates the controller name.
*
* @return string
*/
private function generateControllerName()
{
return create_class_name($this->model, 'ControllerGenerator');
}
}
39 changes: 22 additions & 17 deletions src/stubs/factories/factory.stub
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */
namespace Database\Factories;

use {{MODEL_NAMESPACE}}\{{MODEL_NAME}};
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
class {{CLASS_NAME}} extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = {{MODEL_NAME}}::class;

$factory->define({{MODEL_NAME}}::class, function (Faker $faker) {
return [
'name' => $faker->name,
];
});
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name,
];
}
}
2 changes: 2 additions & 0 deletions src/stubs/models/model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace {{NAMESPACE}};

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class {{CLASS_NAME}} extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
Expand Down
10 changes: 5 additions & 5 deletions src/stubs/routes/api.routes.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
|--------------------------------------------------------------------------
*/
Route::name('{{ROUTE_NAME}}.')->prefix('{{ROUTE_NAME}}')->group(function () {
Route::get('/', '{{CONTROLLER_NAME}}@index')->name('index');
Route::post('/', '{{CONTROLLER_NAME}}@store')->name('create');
Route::get('/{{{MODEL_VARIABLE}}}', '{{CONTROLLER_NAME}}@show')->name('show');
Route::patch('/{{{MODEL_VARIABLE}}}', '{{CONTROLLER_NAME}}@update')->name('update');
Route::delete('/{{{MODEL_VARIABLE}}}', '{{CONTROLLER_NAME}}@destroy')->name('destroy');
Route::get('/', [{{CONTROLLER_NAME}}::class, 'index'])->name('index');
Route::post('/', [{{CONTROLLER_NAME}}::class, 'store'])->name('create');
Route::get('/{{{MODEL_VARIABLE}}}', [{{CONTROLLER_NAME}}::class, 'show'])->name('show');
Route::patch('/{{{MODEL_VARIABLE}}}', [{{CONTROLLER_NAME}}::class, 'update'])->name('update');
Route::delete('/{{{MODEL_VARIABLE}}}', [{{CONTROLLER_NAME}}::class, 'destroy'])->name('destroy');
});
4 changes: 3 additions & 1 deletion src/stubs/seeds/seed.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Database\Seeders;

use {{MODEL_NAMESPACE}}\{{MODEL_NAME}};
use Illuminate\Database\Seeder;

Expand All @@ -22,7 +24,7 @@ class {{CLASS_NAME}} extends Seeder
$bar = $this->command->getOutput()->createProgressBar($this->numberOf{{MODEL_PLURAL}});

for ($i = 0; $i < $this->numberOf{{MODEL_PLURAL}}; ++$i) {
factory({{MODEL_NAME}}::class)->create();
{{MODEL_NAME}}::factory()->create();
$bar->advance();
}

Expand Down
Loading

0 comments on commit 965c4b7

Please sign in to comment.