diff --git a/README.md b/README.md index 75b8079..ce12839 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,18 @@ Laravel Packer was created by, and is maintained by [Sarthak](https://github.com ## Features -- [All Artisan command while you create laravel package](#creating-new-package-scaffolding) -- [Create CRUD for any model along with passing test cases ( feature and unit tests)](#crud-generator) -- [Smart Clone - 4 steps in just 1 step](#smart-clone) +- [Features](#features) +- [Installation](#installation) +- [Creating new Package Scaffolding](#creating-new-package-scaffolding) +- [Same as Artisan commands](#same-as-artisan-commands) +- [Smart Clone](#smart-clone) + - [Specify directory to clone](#specify-directory-to-clone) + - [Specify branch to clone](#specify-branch-to-clone) +- [CRUD Generator](#crud-generator) + - [step 1](#step-1) + - [step 2](#step-2) + - [Todo](#todo) +- [License](#license) ## Installation @@ -124,6 +133,10 @@ After giving all details you can now run command to actually create full crud fo packr crud:make {relativePathOfThatJsonFile} ``` +### Todo +- [ ] Add resource in controller for CRUD maker +- [ ] Add form request in controller for CRUD maker + ## License This package inherits the licensing of its parent framework, Laravel, and as such is open-sourced diff --git a/app/Commands/Crud/AddRouteCommand.php b/app/Commands/Crud/AddRouteCommand.php index 5cfed8f..df583e9 100644 --- a/app/Commands/Crud/AddRouteCommand.php +++ b/app/Commands/Crud/AddRouteCommand.php @@ -53,7 +53,7 @@ public function addLine($path) { $model = $this->argument('name'); $resourceType = $this->option('api') == 'api' ? 'apiResource' : 'resource'; - $routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . "Controller');"; + $routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . 'Controller::class);'; file_put_contents($path, $routes, FILE_APPEND); } diff --git a/app/Commands/Crud/Factories/FactoryMakeCommand.php b/app/Commands/Crud/Factories/FactoryMakeCommand.php index d972c03..18ec697 100644 --- a/app/Commands/Crud/Factories/FactoryMakeCommand.php +++ b/app/Commands/Crud/Factories/FactoryMakeCommand.php @@ -49,6 +49,8 @@ protected function getStub() */ protected function buildClass($name) { + $this->addFakerData(); + $factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name))); $namespaceModel = $this->option('model') @@ -57,11 +59,7 @@ protected function buildClass($name) $model = class_basename($namespaceModel); - if (Str::startsWith($namespaceModel, $this->rootNamespace() . 'Models')) { - $namespace = Str::beforeLast('Database\\Factories\\' . Str::after($namespaceModel, $this->rootNamespace() . 'Models\\'), '\\'); - } else { - $namespace = $this->rootNamespace() . 'Database\\Factories'; - } + $namespace = $this->rootNamespace() . 'Database\\Factories'; $replace = [ '{{ factoryNamespace }}' => $namespace, @@ -71,6 +69,7 @@ protected function buildClass($name) 'DummyModel' => $model, '{{ model }}' => $model, '{{model}}' => $model, + '//' => $this->addFakerData(), ]; return str_replace( @@ -106,7 +105,7 @@ protected function getOptions() ]; } - public function addFakerData($factory) + public function addFakerData() { $fields = cache()->get('structure')->fields; $newLines = ''; @@ -114,12 +113,13 @@ public function addFakerData($factory) foreach ($this->fakerType() as $key => $value) { // dump($field->type); if ($key == $field->type) { - $newLines .= "'$field->name'" . ' => $faker->' . $value . ', + $newLines .= "'$field->name'" . ' => $this->faker->' . $value . ', '; } } } - return str_replace('//', $newLines, $factory); + + return $newLines; } protected function fakerType() diff --git a/app/Commands/Crud/ModelMakeCommand.php b/app/Commands/Crud/ModelMakeCommand.php index e39b8af..b14dbb1 100755 --- a/app/Commands/Crud/ModelMakeCommand.php +++ b/app/Commands/Crud/ModelMakeCommand.php @@ -75,6 +75,8 @@ protected function buildClass($name) $stub = $this->addMoreTo($stub); + $stub = $this->replaceModelNamespace($stub); + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); } @@ -89,7 +91,7 @@ protected function createFactory() $this->call('crud:factory', [ 'name' => "{$factory}Factory", - '--model' => $this->qualifyClass($this->getNameInput()), + '--model' => $this->qualifyModel($this->getNameInput()), ]); } @@ -210,7 +212,28 @@ public function getPath($name) $content = $this->getComposer(); $name = Str::replaceFirst($this->rootNamespace(), '', $name); $path = getcwd() . $this->devPath(); - $path = $content->type === 'project' ? $path . '/app/' : $path . '/src/'; + $path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/'; return $path . str_replace('\\', '/', $name) . '.php'; } + + public function replaceModelNamespace($stub) + { + return str_replace('DummyNamespace', $this->rootNamespace() . 'Models', $stub); + } + + protected function qualifyModel(string $model) + { + $model = ltrim($model, '\\/'); + + $model = str_replace('/', '\\', $model); + + $rootNamespace = $this->rootNamespace(); + + if (Str::startsWith($model, $rootNamespace)) { + return $model; + } + return is_dir($this->projectPath() . 'Models') + ? $rootNamespace . 'Models\\' . $model + : $rootNamespace . $model; + } } diff --git a/app/Commands/Crud/TestMakeCommand.php b/app/Commands/Crud/TestMakeCommand.php index 99c751b..98cb28e 100644 --- a/app/Commands/Crud/TestMakeCommand.php +++ b/app/Commands/Crud/TestMakeCommand.php @@ -111,7 +111,7 @@ protected function replaceNamespace(&$stub, $name) ucfirst($model), $this->getNamespace($name), $this->rootNamespace(), - $this->namespaceFromComposer() . '' . $model, + $this->namespaceFromComposer() . 'Models\\' . $model, cache()->get('structure')->fields[0]->name, Str::plural(Str::snake($model)), $this->userProviderModel(), diff --git a/app/Commands/Crud/stubs/model.stub b/app/Commands/Crud/stubs/model.stub index f01a833..5969fda 100644 --- a/app/Commands/Crud/stubs/model.stub +++ b/app/Commands/Crud/stubs/model.stub @@ -2,9 +2,12 @@ namespace DummyNamespace; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class DummyClass extends Model { + use HasFactory; + // } diff --git a/app/Commands/Foundation/ModelMakeCommand.php b/app/Commands/Foundation/ModelMakeCommand.php index aaa8241..8478e91 100644 --- a/app/Commands/Foundation/ModelMakeCommand.php +++ b/app/Commands/Foundation/ModelMakeCommand.php @@ -28,7 +28,28 @@ public function getPath($name) $content = $this->getComposer(); $name = Str::replaceFirst($this->rootNamespace(), '', $name); $path = getcwd() . $this->devPath(); - $path = $content->type === 'project' ? $path . '/app/' : $path . '/src/'; + $path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/'; return $path . str_replace('\\', '/', $name) . '.php'; } + + /** + * Build the class with the given name. + * + * @param string $name + * @return string + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + protected function buildClass($name) + { + $stub = $this->files->get($this->getStub()); + + $stub = $this->replaceModelNamespace($stub); + + return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name); + } + + public function replaceModelNamespace($stub) + { + return str_replace('{{ namespace }}', $this->rootNamespace() . 'Models', $stub); + } } diff --git a/app/Commands/Foundation/stubs/model.stub b/app/Commands/Foundation/stubs/model.stub index f01a833..5969fda 100644 --- a/app/Commands/Foundation/stubs/model.stub +++ b/app/Commands/Foundation/stubs/model.stub @@ -2,9 +2,12 @@ namespace DummyNamespace; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class DummyClass extends Model { + use HasFactory; + // } diff --git a/app/Commands/Helpers/PackageDetail.php b/app/Commands/Helpers/PackageDetail.php index bee6f82..2e4b467 100644 --- a/app/Commands/Helpers/PackageDetail.php +++ b/app/Commands/Helpers/PackageDetail.php @@ -49,8 +49,12 @@ public function devPath() public function getPath($name) { $name = Str::replaceFirst($this->rootNamespace(), '', $name); + return $this->projectPath() . str_replace('\\', '/', $name) . '.php'; + } + + public function projectPath() + { $path = getcwd() . $this->devPath(); - $path = $this->getComposer()->type !== 'project' ? $path . 'src/' : $path; - return $path . str_replace('\\', '/', $name) . '.php'; + return $this->getComposer()->type !== 'project' ? $path . 'src/' : $path; } } diff --git a/builds/packr b/builds/packr index 16bd4b7..3644615 100755 Binary files a/builds/packr and b/builds/packr differ diff --git a/config/app.php b/config/app.php index d2a2c6c..d005ffe 100644 --- a/config/app.php +++ b/config/app.php @@ -25,7 +25,7 @@ | */ - 'version' => 'v4.7.4', + 'version' => 'v4.7.5', /* |-------------------------------------------------------------------------- diff --git a/tests/Feature/Crud/CrudMakeTest.php b/tests/Feature/Crud/CrudMakeTest.php index bd3d06c..e45d14d 100644 --- a/tests/Feature/Crud/CrudMakeTest.php +++ b/tests/Feature/Crud/CrudMakeTest.php @@ -1,6 +1,6 @@ isFileExists('src/Test.php'); + $this->isFileExists('src/Models/Test.php'); $this->isFileExists('src/database/factories/TestFactory.php'); $this->assertEquals(1, count(glob($this->_testPath() . 'src/database/migrations/*_create_tests_table.php'))); $this->isFileExists('src/Http/controllers/TestController.php'); @@ -30,13 +30,14 @@ public function test_it_can_create_a_crud_for_a_json_file() public function test_it_create_class_based_factory() { - Artisan::call('crud:json Test'); - Artisan::call('crud:make Test'); - $this->isFileExists('src/database/factories/TestFactory.php'); - $content = file_get_contents($this->_testPath() . 'src/database/factories/TestFactory.php'); - $this->assertStringContainsString('protected $model = Test::class;', $content); - $this->assertStringContainsString('use Bitfumes\TestApp\Test;', $content); + Artisan::call('crud:json User'); + Artisan::call('crud:make User'); + $this->isFileExists('src/database/factories/UserFactory.php'); + $content = file_get_contents($this->_testPath() . 'src/database/factories/UserFactory.php'); + $this->assertStringContainsString('protected $model = User::class;', $content); + $this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content); $this->assertStringContainsString('namespace Bitfumes\TestApp\Database\Factories;', $content); + $this->assertStringContainsString('\'title\' => $this->faker->word', $content); } public function test_it_create_tests_properly() @@ -46,6 +47,25 @@ public function test_it_create_tests_properly() $this->isFileExists('/tests/Feature/UserTest.php'); $content = file_get_contents($this->_testPath() . '/tests/Feature/UserTest.php'); $this->assertStringContainsString('class UserTest', $content); + $this->assertStringContainsString('use Bitfumes\TestApp\Models\User;', $content); $this->assertStringContainsString('User::factory()->count($num)->create($args);', $content);; } + + public function test_it_create_model_on_models_dir_with_correct_namespace() + { + Artisan::call('crud:json User'); + Artisan::call('crud:make User'); + $this->isFileExists('src/models/User.php'); + $content = file_get_contents($this->_testPath() . 'src/models/User.php'); + $this->assertStringContainsString('class User extends Model', $content); + $this->assertStringContainsString('namespace Bitfumes\TestApp\Models;', $content); + } + + public function test_it_add_resource_route() + { + Artisan::call('crud:json User'); + Artisan::call('crud:make User'); + $content = file_get_contents($this->_testPath() . 'src/Http/routes.php'); + $this->assertStringContainsString("Route::apiResource('user','UserController::class);", $content); + } } diff --git a/tests/Feature/Make/ModelTest.php b/tests/Feature/Make/ModelTest.php new file mode 100644 index 0000000..d321d76 --- /dev/null +++ b/tests/Feature/Make/ModelTest.php @@ -0,0 +1,21 @@ +isFileExists('src/models/User.php'); + $content = file_get_contents($this->_testPath() . 'src/models/User.php'); + $this->assertStringContainsString('namespace Bitfumes\TestApp\Models;', $content); + $this->assertStringContainsString('class User extends Model', $content); + } +} diff --git a/tests/Feature/Make/TestsTest.php b/tests/Feature/Make/TestsTest.php new file mode 100644 index 0000000..84ee5e0 --- /dev/null +++ b/tests/Feature/Make/TestsTest.php @@ -0,0 +1,28 @@ +isFileExists('tests/feature/UserTest.php'); + $content = file_get_contents($this->_testPath() . 'tests/feature/UserTest.php'); + $this->assertStringContainsString('class UserTest extends TestCase', $content); + } + + public function test_it_create_unit_test_file() + { + Artisan::call('make:test UserTest --unit'); + $this->isFileExists('tests/unit/UserTest.php'); + $content = file_get_contents($this->_testPath() . 'tests/unit/UserTest.php'); + $this->assertStringContainsString('class UserTest extends TestCase', $content); + } +}