Skip to content

Commit 2d87fbe

Browse files
committed
more tests with many error fixed
1 parent 4e90f68 commit 2d87fbe

File tree

14 files changed

+163
-27
lines changed

14 files changed

+163
-27
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,18 @@ Laravel Packer was created by, and is maintained by [Sarthak](https://github.com
2020

2121
## Features
2222

23-
- [All Artisan command while you create laravel package](#creating-new-package-scaffolding)
24-
- [Create CRUD for any model along with passing test cases ( feature and unit tests)](#crud-generator)
25-
- [Smart Clone - 4 steps in just 1 step](#smart-clone)
23+
- [Features](#features)
24+
- [Installation](#installation)
25+
- [Creating new Package Scaffolding](#creating-new-package-scaffolding)
26+
- [Same as Artisan commands](#same-as-artisan-commands)
27+
- [Smart Clone](#smart-clone)
28+
- [Specify directory to clone](#specify-directory-to-clone)
29+
- [Specify branch to clone](#specify-branch-to-clone)
30+
- [CRUD Generator](#crud-generator)
31+
- [step 1](#step-1)
32+
- [step 2](#step-2)
33+
- [Todo](#todo)
34+
- [License](#license)
2635

2736
## Installation
2837

@@ -124,6 +133,10 @@ After giving all details you can now run command to actually create full crud fo
124133
packr crud:make {relativePathOfThatJsonFile}
125134
```
126135

136+
### Todo
137+
- [ ] Add resource in controller for CRUD maker
138+
- [ ] Add form request in controller for CRUD maker
139+
127140
## License
128141

129142
This package inherits the licensing of its parent framework, Laravel, and as such is open-sourced

app/Commands/Crud/AddRouteCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function addLine($path)
5353
{
5454
$model = $this->argument('name');
5555
$resourceType = $this->option('api') == 'api' ? 'apiResource' : 'resource';
56-
$routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . "Controller');";
56+
$routes = "\nRoute::{$resourceType}('" . strToLower($model) . "','" . Str::studly($model) . 'Controller::class);';
5757
file_put_contents($path, $routes, FILE_APPEND);
5858
}
5959

app/Commands/Crud/Factories/FactoryMakeCommand.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ protected function getStub()
4949
*/
5050
protected function buildClass($name)
5151
{
52+
$this->addFakerData();
53+
5254
$factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));
5355

5456
$namespaceModel = $this->option('model')
@@ -57,11 +59,7 @@ protected function buildClass($name)
5759

5860
$model = class_basename($namespaceModel);
5961

60-
if (Str::startsWith($namespaceModel, $this->rootNamespace() . 'Models')) {
61-
$namespace = Str::beforeLast('Database\\Factories\\' . Str::after($namespaceModel, $this->rootNamespace() . 'Models\\'), '\\');
62-
} else {
63-
$namespace = $this->rootNamespace() . 'Database\\Factories';
64-
}
62+
$namespace = $this->rootNamespace() . 'Database\\Factories';
6563

6664
$replace = [
6765
'{{ factoryNamespace }}' => $namespace,
@@ -71,6 +69,7 @@ protected function buildClass($name)
7169
'DummyModel' => $model,
7270
'{{ model }}' => $model,
7371
'{{model}}' => $model,
72+
'//' => $this->addFakerData(),
7473
];
7574

7675
return str_replace(
@@ -106,20 +105,21 @@ protected function getOptions()
106105
];
107106
}
108107

109-
public function addFakerData($factory)
108+
public function addFakerData()
110109
{
111110
$fields = cache()->get('structure')->fields;
112111
$newLines = '';
113112
foreach ($fields as $field) {
114113
foreach ($this->fakerType() as $key => $value) {
115114
// dump($field->type);
116115
if ($key == $field->type) {
117-
$newLines .= "'$field->name'" . ' => $faker->' . $value . ',
116+
$newLines .= "'$field->name'" . ' => $this->faker->' . $value . ',
118117
';
119118
}
120119
}
121120
}
122-
return str_replace('//', $newLines, $factory);
121+
122+
return $newLines;
123123
}
124124

125125
protected function fakerType()

app/Commands/Crud/ModelMakeCommand.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ protected function buildClass($name)
7575

7676
$stub = $this->addMoreTo($stub);
7777

78+
$stub = $this->replaceModelNamespace($stub);
79+
7880
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
7981
}
8082

@@ -89,7 +91,7 @@ protected function createFactory()
8991

9092
$this->call('crud:factory', [
9193
'name' => "{$factory}Factory",
92-
'--model' => $this->qualifyClass($this->getNameInput()),
94+
'--model' => $this->qualifyModel($this->getNameInput()),
9395
]);
9496
}
9597

@@ -210,7 +212,28 @@ public function getPath($name)
210212
$content = $this->getComposer();
211213
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
212214
$path = getcwd() . $this->devPath();
213-
$path = $content->type === 'project' ? $path . '/app/' : $path . '/src/';
215+
$path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/';
214216
return $path . str_replace('\\', '/', $name) . '.php';
215217
}
218+
219+
public function replaceModelNamespace($stub)
220+
{
221+
return str_replace('DummyNamespace', $this->rootNamespace() . 'Models', $stub);
222+
}
223+
224+
protected function qualifyModel(string $model)
225+
{
226+
$model = ltrim($model, '\\/');
227+
228+
$model = str_replace('/', '\\', $model);
229+
230+
$rootNamespace = $this->rootNamespace();
231+
232+
if (Str::startsWith($model, $rootNamespace)) {
233+
return $model;
234+
}
235+
return is_dir($this->projectPath() . 'Models')
236+
? $rootNamespace . 'Models\\' . $model
237+
: $rootNamespace . $model;
238+
}
216239
}

app/Commands/Crud/TestMakeCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function replaceNamespace(&$stub, $name)
111111
ucfirst($model),
112112
$this->getNamespace($name),
113113
$this->rootNamespace(),
114-
$this->namespaceFromComposer() . '' . $model,
114+
$this->namespaceFromComposer() . 'Models\\' . $model,
115115
cache()->get('structure')->fields[0]->name,
116116
Str::plural(Str::snake($model)),
117117
$this->userProviderModel(),

app/Commands/Crud/stubs/model.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace DummyNamespace;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
class DummyClass extends Model
89
{
10+
use HasFactory;
11+
912
//
1013
}

app/Commands/Foundation/ModelMakeCommand.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,28 @@ public function getPath($name)
2828
$content = $this->getComposer();
2929
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
3030
$path = getcwd() . $this->devPath();
31-
$path = $content->type === 'project' ? $path . '/app/' : $path . '/src/';
31+
$path = $content->type === 'project' ? $path . '/app/Models/' : $path . '/src/Models/';
3232
return $path . str_replace('\\', '/', $name) . '.php';
3333
}
34+
35+
/**
36+
* Build the class with the given name.
37+
*
38+
* @param string $name
39+
* @return string
40+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
41+
*/
42+
protected function buildClass($name)
43+
{
44+
$stub = $this->files->get($this->getStub());
45+
46+
$stub = $this->replaceModelNamespace($stub);
47+
48+
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
49+
}
50+
51+
public function replaceModelNamespace($stub)
52+
{
53+
return str_replace('{{ namespace }}', $this->rootNamespace() . 'Models', $stub);
54+
}
3455
}

app/Commands/Foundation/stubs/model.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace DummyNamespace;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
class DummyClass extends Model
89
{
10+
use HasFactory;
11+
912
//
1013
}

app/Commands/Helpers/PackageDetail.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ public function devPath()
4949
public function getPath($name)
5050
{
5151
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
52+
return $this->projectPath() . str_replace('\\', '/', $name) . '.php';
53+
}
54+
55+
public function projectPath()
56+
{
5257
$path = getcwd() . $this->devPath();
53-
$path = $this->getComposer()->type !== 'project' ? $path . 'src/' : $path;
54-
return $path . str_replace('\\', '/', $name) . '.php';
58+
return $this->getComposer()->type !== 'project' ? $path . 'src/' : $path;
5559
}
5660
}

builds/packr

239 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)