Skip to content

Commit 3a5312a

Browse files
authored
Add view path (#1)
* Add view path default config * wip * wip
1 parent 0dde490 commit 3a5312a

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

config/stubkit.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,12 @@
189189
'bootstrap',
190190
],
191191

192+
/*
193+
|--------------------------------------------------------------------------
194+
| Views
195+
|--------------------------------------------------------------------------
196+
| These settings refer to the make:views command.
197+
|--------------------------------------------------------------------------
198+
*/
199+
'view_path' => 'views/{{model.slugPlural}}/{{view.slug}}.blade.php',
192200
];

src/Commands/ViewsMakeCommand.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Console\Command;
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Str;
8+
use StubKit\Support\Syntax;
89

910
class ViewsMakeCommand extends Command
1011
{
@@ -50,42 +51,24 @@ public function __construct()
5051
*/
5152
public function handle()
5253
{
53-
$folder = $this->getFolder();
54-
55-
if (is_dir(resource_path("views/${folder}"))) {
56-
$this->info('Views already exists!');
57-
58-
return 1;
59-
}
60-
6154
$all = $this->isUsingAllViews();
6255

63-
mkdir(resource_path("views/${folder}"));
64-
6556
foreach ($this->views as $index => $view) {
6657
if (! $all && ! $this->option($view)) {
6758
unset($this->views[$index]);
6859
continue;
6960
}
7061

71-
$this->makeView($folder, $view);
62+
if(! $this->makeView($view)) {
63+
return 1;
64+
}
7265
}
7366

7467
$this->handleOutput();
7568

7669
return 0;
7770
}
7871

79-
/**
80-
* Make a folder name based on argument.
81-
*
82-
* @return string
83-
*/
84-
public function getFolder()
85-
{
86-
return Str::reset($this->argument('name'))->plural()->slug();
87-
}
88-
8972
/**
9073
* Check if any view options were set.
9174
*
@@ -99,13 +82,27 @@ public function isUsingAllViews()
9982
/**
10083
* Locate and make the view with the found stub.
10184
*
102-
* @param string $folder
10385
* @param string $view
10486
*
105-
* @return void
87+
* @return bool
10688
*/
107-
public function makeView(string $folder, string $view)
89+
public function makeView(string $view)
10890
{
91+
// $path = config('stubkit.views.path', 'js/Pages/{{model.studlyPlural}}/{{view.studly}}.vue');
92+
$path = config('stubkit.view_path');
93+
94+
$values = [
95+
'view' => $view,
96+
'model' => Str::reset($this->argument('name'))
97+
];
98+
99+
$syntax = (new Syntax())->make(
100+
$values,
101+
config('stubkit.variables.*', [])
102+
);
103+
104+
$path = $syntax->parse($path);
105+
109106
if (file_exists(base_path("stubs/view.${view}.stub"))) {
110107
$stub = base_path("stubs/view.${view}.stub");
111108
} elseif (file_exists(__DIR__."/../../stubs/view.${view}.stub")) {
@@ -114,14 +111,23 @@ public function makeView(string $folder, string $view)
114111
$stub = false;
115112
}
116113

117-
$content = ($stub)
118-
? file_get_contents($stub)
119-
: '';
114+
$path = resource_path($path);
120115

121-
file_put_contents(
122-
resource_path("views/${folder}/${view}.blade.php"),
123-
$content
124-
);
116+
$content = ($stub) ? file_get_contents($stub) : '';
117+
118+
$folder = Str::beforeLast($path, DIRECTORY_SEPARATOR);
119+
120+
if (file_exists($path)) {
121+
$this->info('Views already exists!');
122+
123+
return false;
124+
}
125+
126+
if(! is_dir($folder)) {
127+
mkdir($folder, 0777, true);
128+
}
129+
130+
return file_put_contents($path, $content);
125131
}
126132

127133
/**

src/StubKit.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use Illuminate\Support\Arr;
7+
use Illuminate\Support\Stringable;
78
use PHPUnit\Framework\Assert;
89
use StubKit\Support\Fields;
910
use StubKit\Support\Item;
@@ -373,6 +374,8 @@ public function helper($expression, array $variables = [])
373374
unset($variables[$key]);
374375
} elseif (is_a($value, Item::class)) {
375376
$variables[$key] = $value->field;
377+
} elseif (is_a($value, Stringable::class)) {
378+
$variables[$key] = (string) $value;
376379
} elseif (! is_string($value)) {
377380
unset($variables[$key]);
378381
}

src/Support/Syntax.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public function parse(string $content)
2121
{
2222
foreach ($this->variables as $search => $replace) {
2323
$content = preg_replace_callback("/\s*{{\s*${search}\s*}}/", function ($matches) use ($replace) {
24-
preg_match('/(\s*){{/', $matches[0], $space);
2524

2625
if (! isset($replace['callback'])) {
2726
$replace = ['value' => $replace, 'callback' => ''];
@@ -35,6 +34,8 @@ public function parse(string $content)
3534

3635
$lines = explode("\n", $value);
3736

37+
preg_match('/(\s*){{/', $matches[0], $space);
38+
3839
if (count($lines) == 1) {
3940
return $space[1].implode('', $lines);
4041
}
@@ -45,6 +46,7 @@ public function parse(string $content)
4546

4647
return rtrim(implode('', $lines));
4748
}, $content);
49+
4850
}
4951

5052
return $content;

tests/MakeViewsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public function test_make_views_with_file_flag()
5050

5151
public function test_make_views_when_already_exists()
5252
{
53-
mkdir(__DIR__.'/Fixtures/app/resources/views/users');
53+
mkdir(__DIR__.'/Fixtures/app/resources/views/users', 0777, true);
54+
file_put_contents(__DIR__.'/Fixtures/app/resources/views/users/index.blade.php', '');
5455

5556
$this->artisan('make:views User')
5657
->expectsOutput('Views already exists!')

0 commit comments

Comments
 (0)