Skip to content

Commit a92c3f8

Browse files
committed
logic to create domain names file
1 parent 1fc9e88 commit a92c3f8

File tree

4 files changed

+86
-17
lines changed

4 files changed

+86
-17
lines changed

src/Builder.php

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
namespace LaravelDomainOriented;
44

55
use Carbon\Carbon;
6-
use Illuminate\Filesystem\Filesystem;
76
use Illuminate\Support\Collection;
7+
use Illuminate\Support\Facades\File;
88
use Illuminate\Support\Str;
99

1010
class Builder
1111
{
12-
private Filesystem $filesystem;
1312
private array $filesPaths = [
1413
'Controller' => 'app/Http/Controllers/',
1514
'Resource' => 'app/Domain/%s/',
@@ -25,9 +24,9 @@ class Builder
2524
];
2625
private Collection $names;
2726

28-
public function __construct(Filesystem $filesystem)
27+
public function __construct()
2928
{
30-
$this->filesystem = $filesystem;
29+
$this->createDomainsFile();
3130
}
3231

3332
public function prepare(): array
@@ -41,6 +40,8 @@ public function prepare(): array
4140
}
4241
}
4342

43+
clearstatcache();
44+
4445
return array_filter($exists);
4546
}
4647

@@ -49,18 +50,22 @@ public function run(): void
4950
foreach ($this->filesPaths as $stubName => $finalPath) {
5051
$this->createFile($stubName, $finalPath);
5152
}
53+
54+
$this->insertDomain();
5255
}
5356

5457
public function clear()
5558
{
5659
foreach ($this->filesPaths as $stubName => $finalPath) {
5760
$this->removeFile($stubName, $finalPath);
5861
}
62+
63+
$this->removeDomain();
5964
}
6065

6166
public function createFile(string $stubName, string $finalPath): void
6267
{
63-
$stubFile = $this->filesystem->get($this->getStub($stubName));
68+
$stubFile = File::get($this->getStub($stubName));
6469
$content = $this->replacePlaceholders($stubFile);
6570
$path = base_path(sprintf($finalPath, $this->getDomainName()));
6671
$fileName = $this->getName('singularName', 'Dummy') . $stubName . '.php';
@@ -71,7 +76,7 @@ public function createFile(string $stubName, string $finalPath): void
7176
}
7277

7378
$file = $path.$fileName;
74-
$this->filesystem->put($file, $content);
79+
File::put($file, $content);
7580
}
7681

7782
// todo - we can use glob function as well, for the other files
@@ -83,10 +88,10 @@ public function removeFile(string $stubName, string $finalPath)
8388
$file = $path.$fileName;
8489

8590
if ($stubName === 'Migration') {
86-
$file = $this->filesystem->glob($path.'*_create_'.$this->getName('tableName').'_table.php');
91+
$file = File::glob($path.'*_create_'.$this->getName('tableName').'_table.php');
8792
}
8893

89-
$this->filesystem->delete($file);
94+
File::delete($file);
9095
}
9196

9297
public function getStub($stubName): string
@@ -144,7 +149,7 @@ public function replacePlaceholders($stubFile)
144149
private function checkMigrationExists(): string
145150
{
146151
$migrationPath = base_path('database/migrations');
147-
$migrationFiles = $this->filesystem->glob($migrationPath.'/*.php');
152+
$migrationFiles = File::glob($migrationPath.'/*.php');
148153

149154
$exists = false;
150155

@@ -163,11 +168,47 @@ private function checkClassExists(string $stubName, string $finalPath): string
163168
$fileName = $this->getName('singularName', 'Dummy') . $stubName . '.php';
164169
$file = $path.$fileName;
165170

166-
return $this->filesystem->exists($file) ? $file : false;
171+
return File::exists($file) ? $file : false;
167172
}
168173

169174
public function createDomainFolder()
170175
{
171-
$this->filesystem->ensureDirectoryExists($this->getDomainFolder());
176+
File::ensureDirectoryExists($this->getDomainFolder());
177+
}
178+
179+
public function createDomainsFile()
180+
{
181+
$filePath = app_path('domains.php');
182+
$exists = File::exists($filePath);
183+
$stubFile = File::get(__DIR__.'/Stubs/domains.stub');
184+
185+
if (!$exists) {
186+
File::put($filePath, $stubFile);
187+
}
188+
}
189+
190+
public function insertDomain(): void
191+
{
192+
$path = app_path('domains.php');
193+
$domains = require ($path);
194+
195+
array_push($domains, $this->getDomainName());
196+
$domains = array_unique($domains);
197+
198+
$content = "<?php \n\nreturn " . preg_replace("/[0-9]+ \=\>/i", '',var_export($domains, true)). ';';
199+
File::replace($path, $content);
200+
}
201+
202+
public function removeDomain()
203+
{
204+
$path = app_path('domains.php');
205+
$domains = require ($path);
206+
207+
if (($key = array_search($this->getDomainName(), $domains)) !== false) {
208+
unset($domains[$key]);
209+
}
210+
211+
$content = "<?php \n\nreturn " . preg_replace("/[0-9]+ \=\>/i", '',var_export($domains, true)). ';';
212+
File::replace($path, $content);
172213
}
173214
}

src/Commands/Command.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
namespace LaravelDomainOriented\Commands;
44

5-
use Illuminate\Filesystem\Filesystem;
65
use LaravelDomainOriented\Builder;
76

87
class Command extends \Illuminate\Console\Command
98
{
109
protected Builder $builder;
1110

12-
public function __construct(Filesystem $filesystem)
11+
public function __construct()
1312
{
1413
parent::__construct();
15-
$this->builder = new Builder($filesystem);
14+
$this->builder = new Builder();
1615
}
1716

1817
protected function getNameInput(): string

src/Stubs/domains.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
return array();

tests/Unit/BuilderTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public function setUp(): void
2929
{
3030
parent::setUp();
3131
$this->filesystem = new Filesystem();
32-
$this->builder = new Builder($this->filesystem);
32+
$this->builder = new Builder();
3333
$this->builder->setNames();
34-
$this->builder->createDomainFolder();
3534
}
3635

3736
/** @test **/
@@ -85,7 +84,6 @@ public function it_should_replace_placeholders()
8584
8685
namespace App\Http\Controllers;
8786
88-
use App\Domain\Dummy\DummyDataService;
8987
use App\Domain\Dummy\DummyPersistenceService;
9088
use App\Domain\Dummy\DummyResource;
9189
use App\Domain\Dummy\DummySearchService;
@@ -218,4 +216,32 @@ public function it_should_run_and_create_all_files()
218216

219217
$this->assertEquals($totalFiles, $createdFiles);
220218
}
219+
220+
/** @test **/
221+
public function it_should_insert_domain_name_to_file()
222+
{
223+
$domainName = 'Test';
224+
$this->builder->createDomainsFile();
225+
$this->builder->setNames($domainName);
226+
$this->builder->insertDomain();
227+
228+
$path = app_path('domains.php');
229+
$domains = require $path;
230+
231+
$this->assertContains($domainName, $domains);
232+
}
233+
234+
/** @test **/
235+
public function it_should_remove_domain_name_to_file()
236+
{
237+
$domainName = 'Test';
238+
$this->builder->createDomainsFile();
239+
$this->builder->setNames($domainName);
240+
$this->builder->removeDomain();
241+
242+
$path = app_path('domains.php');
243+
$domains = require $path;
244+
245+
$this->assertNotContains($domainName, $domains);
246+
}
221247
}

0 commit comments

Comments
 (0)