Skip to content

Commit 6b7793b

Browse files
committed
Fix package command and added cache repository stub
1 parent 2792269 commit 6b7793b

File tree

9 files changed

+77
-47
lines changed

9 files changed

+77
-47
lines changed

config/repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
| Instance of Illuminate\Contracts\Cache\Repository
4848
|
4949
*/
50-
'repository' => 'cache',
50+
'repository' => 'cache.store',
5151

5252
/*
5353
|--------------------------------------------------------------------------

src/Commands/RepositoryCommand.php

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ class RepositoryCommand extends BaseCommand
2828
*/
2929
protected array $stubs = [
3030
'interface' => __DIR__ . '/stubs/repository-interface.stub',
31-
'repository' => __DIR__ . '/stubs/repository.stub'
31+
'repository' => __DIR__ . '/stubs/repository.stub',
32+
'cache-repository' => __DIR__ .'/stubs/cache-repository.stub'
3233
];
3334

35+
/**
36+
* Boolean check to skip methods being executed
37+
*
38+
* @var bool
39+
*/
40+
protected bool $skip = false;
41+
3442
public function __construct()
3543
{
3644
parent::__construct();
@@ -39,34 +47,38 @@ public function __construct()
3947
public function handle()
4048
{
4149
$this->checkModel();
42-
list($interface, $interfaceName) = $this->createInterface();
43-
$this->createRepository($interface, $interfaceName);
50+
51+
if (!$this->skip) {
52+
list($interface, $interfaceName) = $this->createInterface();
53+
$this->createRepository($interface, $interfaceName);
54+
}
4455
}
4556

4657
protected function checkModel()
4758
{
48-
$model = $this->appNamespace.$this->getSingularName($this->argument('model'));
59+
$model = $this->getSingularName($this->argument('model'));
60+
61+
$modelParts = explode('\\', $model);
62+
$this->modelName = $modelParts[array_key_last($modelParts)];
4963

50-
$this->model = str_replace('/', '\\', $model);
64+
$this->model = $this->appNamespace."{$model}";
5165

5266
if ($this->laravel->runningInConsole()) {
5367
if (!class_exists($this->model)) {
54-
$response = $this->ask("Model [{$this->model}] does not exist. Would you like to create it?", 'Yes');
68+
$response = $this->ask("Model [{$this->modelName}] does not exist. Would you like to create it?", 'Yes');
5569

5670
if ($this->isResponsePositive($response)) {
5771
Artisan::call('make:model', [
5872
'name' => $this->model
5973
]);
6074

61-
$this->info("Model [{$this->model}] has been successfully created.");
75+
$this->info("Model [{$this->modelName}] has been successfully created.");
6276
} else {
63-
$this->info("Model [{$this->model}] will be skipped.");
77+
$this->info("Model [{$this->modelName}] will be skipped. No repository class will be created.");
78+
$this->skip = true;
6479
}
6580
}
6681
}
67-
68-
$modelInfo = explode('\\', $this->model);
69-
$this->modelName = $modelInfo[array_key_last($modelInfo)];
7082
}
7183

7284
/**
@@ -82,24 +94,24 @@ protected function createInterface()
8294
$content = $this->fileManager->get($this->stubs['interface']);
8395

8496
$replacements = [
85-
'{{ namespace }}' => "{$this->appNamespace}\Repository\{$this->modelName}",
86-
'{{ model }}' => $this->modelName
97+
'%namespace%' => "{$this->appNamespace}Repository\\{$this->modelName}",
98+
'%model%' => $this->modelName
8799
];
88100

89101
$content = str_replace(array_keys($replacements), array_values($replacements), $content);
90102

91103
$fileName = "{$this->modelName}RepositoryInterface";
92104
$fileDirectory = app()->basePath() . "/App/Repository/{$this->modelName}";
93-
$filePath = "{$fileDirectory}{$fileName}.php";
105+
$filePath = "{$fileDirectory}/{$fileName}.php";
94106

95107
if (!$this->fileManager->exists($fileDirectory))
96108
$this->fileManager->makeDirectory($fileDirectory, 0755, true);
97109

98-
if ($this->laravel->runningInConsole() && $this->fileManager->exists($fileDirectory)) {
110+
if ($this->laravel->runningInConsole() && $this->fileManager->exists($filePath)) {
99111
$response = $this->ask("The interface [{$fileName}] has already exists. Do you want to overwrite it?", 'Yes');
100112

101113
if (!$this->isResponsePositive($response)) {
102-
$this->line("The interface [{$fileName}] will not be overwritten.");
114+
$this->info("The interface [{$fileName}] will not be overwritten.");
103115
return;
104116
}
105117
}
@@ -108,7 +120,7 @@ protected function createInterface()
108120

109121
$this->info("The interface [{$fileName}] has been created");
110122

111-
return ["{$this->appNamespace}\Repository\{$this->modelName}\{$fileName}", $fileName];
123+
return ["{$this->appNamespace}Repository\\{$this->modelName}\\{$fileName}", $fileName];
112124
}
113125

114126
/**
@@ -123,21 +135,22 @@ protected function createInterface()
123135
*/
124136
protected function createRepository(string $interface, string $fileName)
125137
{
126-
$content = $this->fileManager->get($this->stubs['repository']);
138+
if ($this->hasOption('cache')) $content = $this->fileManager->get($this->stubs['cache-repository']);
139+
else $content = $this->fileManager->get($this->stubs['repository']);
127140

128141
$replacements = [
129-
'{{ interfaceNamespace }}' => "{$this->appNamespace}{$interface}",
130-
'{{ interface }}' => $fileName,
131-
'{{ model }}' => $this->model,
132-
'{{ modelName }}' => $this->modelName,
133-
'{{ namespace }}' => "{$this->appNamespace}\Repository\{$this->modelName}"
142+
'%interfaceNamespace%' => "{$interface}",
143+
'%interface%' => $fileName,
144+
'%model%' => $this->model,
145+
'%modelName%' => $this->modelName,
146+
'%namespace%' => "{$this->appNamespace}Repository\\{$this->modelName}"
134147
];
135148

136149
$content = str_replace(array_keys($replacements), array_values($replacements), $content);
137150

138151
$fileName = "{$this->modelName}Repository";
139152
$fileDirectory = app()->basePath() . "/App/Repository/{$this->modelName}";
140-
$filePath = "{$fileDirectory}{$fileName}.php";
153+
$filePath = "{$fileDirectory}/{$fileName}.php";
141154

142155
if (!$this->fileManager->exists($fileDirectory))
143156
$this->fileManager->makeDirectory($fileDirectory, 0755, true);
@@ -153,7 +166,7 @@ protected function createRepository(string $interface, string $fileName)
153166

154167
$this->fileManager->put($filePath, $content);
155168

156-
$this->info("The repository [{$filePath}] has been created.");
169+
$this->info("The repository [{$fileName}] has been created.");
157170
}
158171

159172

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace %namespace%;
4+
5+
use %model%;
6+
use CodeOfDigital\CacheRepository\Eloquent\CacheRepository;
7+
8+
class %modelName%Repository extends CacheRepository implements %interface%
9+
{
10+
/**
11+
* Specify Model class name
12+
*
13+
* @return string
14+
*/
15+
public function model()
16+
{
17+
return %modelName%::class;
18+
}
19+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace {{ namespace }};
3+
namespace %namespace%;
44

55
use CodeOfDigital\CacheRepository\Contracts\RepositoryInterface;
66

7-
interface {{ model }}RepositoryInterface extends RepositoryInterface
7+
interface %model%RepositoryInterface extends RepositoryInterface
88
{
9-
\\ TODO: Add your functions here...
9+
// TODO: Add your functions here...
1010
}

src/Commands/stubs/repository.stub

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<?php
22

3-
namespace {{ namespace }};
4-
5-
use {{ model }};
6-
use {{ interfaceNamespace }};
3+
namespace %namespace%;
74

5+
use %model%;
86
use CodeOfDigital\CacheRepository\Eloquent\BaseRepository;
97

10-
class {{ modelName }}Repository extends BaseRepository implements {{ interface }}
8+
class %modelName%Repository extends BaseRepository implements %interface%
119
{
1210
/**
1311
* Specify Model class name
@@ -16,6 +14,6 @@ class {{ modelName }}Repository extends BaseRepository implements {{ interface }
1614
*/
1715
public function model()
1816
{
19-
return {{ modelName }}::class;
17+
return %modelName%::class;
2018
}
21-
}
19+
}

src/Contracts/RepositoryInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public function paginate($limit = null, $columns = ['*']);
1818

1919
public function simplePaginate($limit = null, $columns = ['*']);
2020

21-
public function find($id, $columns = ['*']);
21+
public function find(int $id, $columns = ['*']);
2222

23-
public function findByField($field, $value, $columns = ['*']);
23+
public function findByField(string $field, $value, $columns = ['*']);
2424

2525
public function findWhere(array $where, $columns = ['*']);
2626

@@ -61,4 +61,4 @@ public function firstOrCreate(array $attributes = []);
6161
public static function __callStatic($method, $arguments);
6262

6363
public function __call($method, $arguments);
64-
}
64+
}

src/Eloquent/BaseRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function simplePaginate($limit = null, $columns = ['*'])
179179
return $this->paginate($limit, $columns, "simplePaginate");
180180
}
181181

182-
public function find($id, $columns = ['*'])
182+
public function find(int $id, $columns = ['*'])
183183
{
184184
$this->applyScope();
185185
$model = $this->model->findOrFail($id, $columns);
@@ -188,7 +188,7 @@ public function find($id, $columns = ['*'])
188188
return $model;
189189
}
190190

191-
public function findByField($field, $value = null, $columns = ['*'])
191+
public function findByField(string $field, $value = null, $columns = ['*'])
192192
{
193193
$this->applyScope();
194194
$model = $this->model->where($field, '=', $value)->get($columns);

src/Listeners/RepositoryEventSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RepositoryEventSubscriber
2525

2626
public function __construct()
2727
{
28-
$this->cache = app(Config::get('repository.cache.repository', 'cache'));
28+
$this->cache = app(Config::get('repository.cache.repository', 'cache.store'));
2929
}
3030

3131
public function handleCleanCache(RepositoryEventBase $eventBase): void

src/Traits/Cacheable.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function getCacheRepository(): CacheRepository
4141
if (isset($this->cacheRepository))
4242
return $this->cacheRepository;
4343

44-
return app(Config::get('repository.cache.repository', 'cache'));
44+
return app(Config::get('repository.cache.repository', 'cache.store'));
4545
}
4646

4747
public function skipCache($status = true): static
@@ -161,16 +161,16 @@ public function find(int $id, $columns = ['*'])
161161
return $value;
162162
}
163163

164-
public function findByField(int $id, $value = null, $columns = ['*'])
164+
public function findByField(string $field, $value = null, $columns = ['*'])
165165
{
166166
if (!$this->allowedCache('findByField') || $this->isSkippedCache()) {
167-
return parent::findByField($id, $columns);
167+
return parent::findByField($field, $columns);
168168
}
169169

170170
$key = $this->getCacheKey('findByField', func_get_args());
171171
$minutes = $this->cacheMinutes;
172-
$value = $this->cacheRepository->remember($key, $minutes, function () use ($id, $columns) {
173-
return parent::findByField($id, $columns);
172+
$value = $this->cacheRepository->remember($key, $minutes, function () use ($field, $columns) {
173+
return parent::findByField($field, $columns);
174174
});
175175

176176
$this->resetModel();

0 commit comments

Comments
 (0)