Skip to content

Commit 2792269

Browse files
committed
Added command to generate repository
1 parent 0c50ad4 commit 2792269

12 files changed

+598
-124
lines changed

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
"CodeOfDigital\\CacheRepository\\": "src/"
3939
}
4040
},
41+
"autoload-dev": {
42+
"psr-4": {
43+
"CodeOfDigital\\CacheRepository\\Tests\\": "tests/"
44+
}
45+
},
4146
"scripts": {
4247
"tests": "./vendor/bin/phpunit --colors=always"
4348
},

src/CacheRepositoryServiceProvider.php

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

33
namespace CodeOfDigital\CacheRepository;
44

5+
use CodeOfDigital\CacheRepository\Commands\RepositoryCommand;
56
use Illuminate\Support\ServiceProvider;
67

78
class CacheRepositoryServiceProvider extends ServiceProvider
89
{
910
public function boot()
1011
{
12+
$this->publishAssets();
1113
$this->mergeConfigFrom(__DIR__.'/../config/repository.php', 'repository');
1214
}
1315

16+
protected function publishAssets()
17+
{
18+
if ($this->app->runningInConsole()) {
19+
$this->publishes([__DIR__.'/../config/repository.php' => base_path('config/repository.php')]);
20+
}
21+
}
22+
1423
public function register()
1524
{
1625
$this->app->register(EventServiceProvider::class);
26+
27+
if ($this->app->runningInConsole())
28+
$this->commands([RepositoryCommand::class]);
1729
}
1830
}

src/Commands/BaseCommand.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace CodeOfDigital\CacheRepository\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class BaseCommand extends Command
9+
{
10+
/**
11+
* File manager.
12+
*
13+
* @var Filesystem
14+
*/
15+
protected Filesystem $fileManager;
16+
17+
/**
18+
* Application namespace
19+
*
20+
* @var string
21+
*/
22+
protected string $appNamespace;
23+
24+
/**
25+
* Path for related model
26+
*
27+
* @var string
28+
*/
29+
protected string $model;
30+
31+
/**
32+
* Name of model
33+
*
34+
* @var string
35+
*/
36+
protected string $modelName;
37+
38+
public function __construct()
39+
{
40+
parent::__construct();
41+
$this->fileManager = app('files');
42+
$this->appNamespace = app()->getNamespace();
43+
}
44+
45+
/**
46+
* Determine if the user input is yes.
47+
*
48+
* @param $response
49+
* @return bool
50+
*/
51+
public function isResponsePositive($response): bool
52+
{
53+
return in_array(strtolower($response), ['y', 'yes']);
54+
}
55+
}

src/Commands/RepositoryCommand.php

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
3+
namespace CodeOfDigital\CacheRepository\Commands;
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
use Illuminate\Support\Pluralizer;
7+
8+
class RepositoryCommand extends BaseCommand
9+
{
10+
/**
11+
* The name of the command
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:repository {model} {--cache : Whether to use caching in your repository}';
16+
17+
/**
18+
* The description of command
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new repository';
23+
24+
/**
25+
* Stub paths
26+
*
27+
* @var array|string[]
28+
*/
29+
protected array $stubs = [
30+
'interface' => __DIR__ . '/stubs/repository-interface.stub',
31+
'repository' => __DIR__ . '/stubs/repository.stub'
32+
];
33+
34+
public function __construct()
35+
{
36+
parent::__construct();
37+
}
38+
39+
public function handle()
40+
{
41+
$this->checkModel();
42+
list($interface, $interfaceName) = $this->createInterface();
43+
$this->createRepository($interface, $interfaceName);
44+
}
45+
46+
protected function checkModel()
47+
{
48+
$model = $this->appNamespace.$this->getSingularName($this->argument('model'));
49+
50+
$this->model = str_replace('/', '\\', $model);
51+
52+
if ($this->laravel->runningInConsole()) {
53+
if (!class_exists($this->model)) {
54+
$response = $this->ask("Model [{$this->model}] does not exist. Would you like to create it?", 'Yes');
55+
56+
if ($this->isResponsePositive($response)) {
57+
Artisan::call('make:model', [
58+
'name' => $this->model
59+
]);
60+
61+
$this->info("Model [{$this->model}] has been successfully created.");
62+
} else {
63+
$this->info("Model [{$this->model}] will be skipped.");
64+
}
65+
}
66+
}
67+
68+
$modelInfo = explode('\\', $this->model);
69+
$this->modelName = $modelInfo[array_key_last($modelInfo)];
70+
}
71+
72+
/**
73+
* Create a new repository interface
74+
*
75+
* @return string[]|void
76+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
77+
* @throws \Psr\Container\ContainerExceptionInterface
78+
* @throws \Psr\Container\NotFoundExceptionInterface
79+
*/
80+
protected function createInterface()
81+
{
82+
$content = $this->fileManager->get($this->stubs['interface']);
83+
84+
$replacements = [
85+
'{{ namespace }}' => "{$this->appNamespace}\Repository\{$this->modelName}",
86+
'{{ model }}' => $this->modelName
87+
];
88+
89+
$content = str_replace(array_keys($replacements), array_values($replacements), $content);
90+
91+
$fileName = "{$this->modelName}RepositoryInterface";
92+
$fileDirectory = app()->basePath() . "/App/Repository/{$this->modelName}";
93+
$filePath = "{$fileDirectory}{$fileName}.php";
94+
95+
if (!$this->fileManager->exists($fileDirectory))
96+
$this->fileManager->makeDirectory($fileDirectory, 0755, true);
97+
98+
if ($this->laravel->runningInConsole() && $this->fileManager->exists($fileDirectory)) {
99+
$response = $this->ask("The interface [{$fileName}] has already exists. Do you want to overwrite it?", 'Yes');
100+
101+
if (!$this->isResponsePositive($response)) {
102+
$this->line("The interface [{$fileName}] will not be overwritten.");
103+
return;
104+
}
105+
}
106+
107+
$this->fileManager->put($filePath, $content);
108+
109+
$this->info("The interface [{$fileName}] has been created");
110+
111+
return ["{$this->appNamespace}\Repository\{$this->modelName}\{$fileName}", $fileName];
112+
}
113+
114+
/**
115+
* Create a new repository
116+
*
117+
* @param string $interface
118+
* @param string $fileName
119+
* @return void
120+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
121+
* @throws \Psr\Container\ContainerExceptionInterface
122+
* @throws \Psr\Container\NotFoundExceptionInterface
123+
*/
124+
protected function createRepository(string $interface, string $fileName)
125+
{
126+
$content = $this->fileManager->get($this->stubs['repository']);
127+
128+
$replacements = [
129+
'{{ interfaceNamespace }}' => "{$this->appNamespace}{$interface}",
130+
'{{ interface }}' => $fileName,
131+
'{{ model }}' => $this->model,
132+
'{{ modelName }}' => $this->modelName,
133+
'{{ namespace }}' => "{$this->appNamespace}\Repository\{$this->modelName}"
134+
];
135+
136+
$content = str_replace(array_keys($replacements), array_values($replacements), $content);
137+
138+
$fileName = "{$this->modelName}Repository";
139+
$fileDirectory = app()->basePath() . "/App/Repository/{$this->modelName}";
140+
$filePath = "{$fileDirectory}{$fileName}.php";
141+
142+
if (!$this->fileManager->exists($fileDirectory))
143+
$this->fileManager->makeDirectory($fileDirectory, 0755, true);
144+
145+
if ($this->laravel->runningInConsole() && $this->fileManager->exists($filePath)) {
146+
$response = $this->ask("The repository [{$fileName}] already exists. Do you want to overwrite it?", 'Yes');
147+
148+
if (!$this->isResponsePositive($response)) {
149+
$this->info("The repository [{$fileName}] will not be overwritten.");
150+
return;
151+
}
152+
}
153+
154+
$this->fileManager->put($filePath, $content);
155+
156+
$this->info("The repository [{$filePath}] has been created.");
157+
}
158+
159+
160+
public function getSingularName($model): string
161+
{
162+
return empty($model) ? '' : ucwords(Pluralizer::singular($model));
163+
}
164+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use CodeOfDigital\CacheRepository\Contracts\RepositoryInterface;
6+
7+
interface {{ model }}RepositoryInterface extends RepositoryInterface
8+
{
9+
\\ TODO: Add your functions here...
10+
}

src/Commands/stubs/repository.stub

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

src/Contracts/CacheableInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function getCacheRepository(): CacheRepository;
1212

1313
public function getCacheKey($method, $args = null): string;
1414

15-
public function getCacheTime(): int;
15+
public function getCacheTTL(): float|int;
1616

1717
public function skipCache($status = true): static;
1818
}

0 commit comments

Comments
 (0)