Skip to content

Commit 0c8895b

Browse files
committed
Update repository command
1 parent 7ca6201 commit 0c8895b

File tree

3 files changed

+84
-24
lines changed

3 files changed

+84
-24
lines changed

README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ composer require dannyvilla/artisan-commands
1212

1313
## Usage
1414

15+
### View command
16+
#### Generate an empty view
17+
```bash
18+
php artisan make:view folder.subfolder.view
19+
```
20+
21+
#### Generate a view with a layout
22+
```bash
23+
php artisan make:view folder.subfolder.view --layout=app
24+
```
25+
1526
### Repository command
1627
#### Generate an empty repository file
1728
```bash
@@ -21,16 +32,9 @@ php artisan make:repository UserRepository
2132
```bash
2233
php artisan make:repository UserRepository --model=User
2334
```
24-
25-
### View command
26-
#### Generate an empty view
27-
```bash
28-
php artisan make:view folder.subfolder.view
29-
```
30-
31-
#### Generate a view with a layout
35+
OR
3236
```bash
33-
php artisan make:view folder.subfolder.view --layout=app
37+
php artisan make:repository UserRepository --model=App\Models\User
3438
```
3539

3640
### Lang command

src/Commands/Repository.php

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Davinet\ArtisanCommand\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
67

78
class Repository extends Command
89
{
@@ -89,6 +90,18 @@ protected function replaceModelName($name, $stub)
8990
return str_replace('DummyModel', $model, $stub);
9091
}
9192

93+
/**
94+
* Replace the namespace of the namespace of the model.
95+
*
96+
* @param $namespace
97+
* @param $stub
98+
* @return mixed
99+
*/
100+
protected function replaceModelNamespace($namespace, $stub)
101+
{
102+
return str_replace('DummyModelNamespace', ucfirst($namespace), $stub);
103+
}
104+
92105
/**
93106
* Rewrite actually the content in the file.
94107
*
@@ -102,6 +115,36 @@ protected function putInFile($filename, $content)
102115
file_put_contents($filename, $content);
103116
}
104117

118+
/**
119+
* Set the right name and namespace.
120+
*
121+
* @param $model
122+
* @param $namespace
123+
* @return void
124+
*/
125+
protected function setModelAndNamespace(&$model, &$namespace)
126+
{
127+
$exploded = explode('\\', $model);
128+
$model = array_last($exploded);
129+
$namespace = '';
130+
131+
for ($i = 0; $i < count($exploded) - 1; $i++)
132+
$namespace .= $exploded[$i].'\\';
133+
134+
$namespace = Str::replaceLast('\\','', $namespace);
135+
}
136+
137+
/**
138+
* Check if a model file exists.
139+
*
140+
* @param $model
141+
* @return bool
142+
*/
143+
protected function modelFileExists($model)
144+
{
145+
return file_exists( base_path(lcfirst($model).'.php'));
146+
}
147+
105148
/**
106149
* Execute the console command.
107150
*
@@ -111,32 +154,45 @@ public function handle()
111154
{
112155
$name = $this->argument('name');
113156
$model = $this->option('model');
114-
157+
$namespace = 'App';
115158
if (empty($name)) {
116159
$this->error('Please the name of the repository is expected.');
117160
} else {
118-
if ((empty($model) || !isset($model)) || (!file_exists(app_path('/'.$model.'.php')))) {
161+
$content = null;
162+
163+
if (is_null($model)) {
119164
$content = $this->replaceClassName($name, $this->getEmptyStub());
120165
} else {
121-
$content = $this->replaceClassName($name, $this->getStub());
122-
$content = $this->replaceModelName($model, $content);
123-
$content = $this->replacePropertyName($model, $content);
166+
if (Str::contains($model, '\\')) {
167+
$this->setModelAndNamespace($model, $namespace);
168+
}
169+
170+
if ($this->modelFileExists($namespace.'\\'.$model)) {
171+
$content = $this->replaceModelNamespace($namespace, $this->getStub());
172+
$content = $this->replaceModelName($model, $content);
173+
$content = $this->replacePropertyName($model, $content);
174+
$content = $this->replaceClassName($name, $content);
175+
} else {
176+
$this->output->error('The specified model "'.$this->option('model').'" does not exist.');
177+
}
124178
}
125179

126-
$filename = app_path('Repositories/'.ucfirst($name).'.php');
180+
if (!is_null($content)) {
181+
$filename = app_path('Repositories/'.ucfirst($name).'.php');
127182

128-
if (file_exists($filename)) {
129-
do {
130-
$input = $this->ask("There is a repository with this name ($name) do you want to replace it ? [o/n] ");
131-
} while (strtolower($input) != 'o' && strtolower($input) != 'n');
183+
if (file_exists($filename)) {
184+
do {
185+
$input = $this->ask("There is a repository with this name ($name) do you want to replace it ? [o/n] ");
186+
} while (strtolower($input) != 'o' && strtolower($input) != 'n');
132187

133-
if('o' == strtolower($input)){
188+
if('o' == strtolower($input)){
189+
$this->putInFile($filename, $content);
190+
$this->info('Reporitory created successfully.');
191+
}
192+
} else {
134193
$this->putInFile($filename, $content);
135194
$this->info('Reporitory created successfully.');
136195
}
137-
} else {
138-
$this->putInFile($filename, $content);
139-
$this->info('Reporitory created successfully.');
140196
}
141197
}
142198
}

src/Commands/stubs/repository.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\Repositories;
44

5-
use App\DummyModel;
5+
use DummyModelNamespace\DummyModel;
66
use Illuminate\Database\Eloquent\Collection;
77
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
88

0 commit comments

Comments
 (0)