Skip to content

Commit

Permalink
Add view path (#1)
Browse files Browse the repository at this point in the history
* Add view path default config

* wip

* wip
  • Loading branch information
dillingham authored Mar 10, 2021
1 parent 0dde490 commit 3a5312a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 33 deletions.
8 changes: 8 additions & 0 deletions config/stubkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,12 @@
'bootstrap',
],

/*
|--------------------------------------------------------------------------
| Views
|--------------------------------------------------------------------------
| These settings refer to the make:views command.
|--------------------------------------------------------------------------
*/
'view_path' => 'views/{{model.slugPlural}}/{{view.slug}}.blade.php',
];
68 changes: 37 additions & 31 deletions src/Commands/ViewsMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use StubKit\Support\Syntax;

class ViewsMakeCommand extends Command
{
Expand Down Expand Up @@ -50,42 +51,24 @@ public function __construct()
*/
public function handle()
{
$folder = $this->getFolder();

if (is_dir(resource_path("views/${folder}"))) {
$this->info('Views already exists!');

return 1;
}

$all = $this->isUsingAllViews();

mkdir(resource_path("views/${folder}"));

foreach ($this->views as $index => $view) {
if (! $all && ! $this->option($view)) {
unset($this->views[$index]);
continue;
}

$this->makeView($folder, $view);
if(! $this->makeView($view)) {
return 1;
}
}

$this->handleOutput();

return 0;
}

/**
* Make a folder name based on argument.
*
* @return string
*/
public function getFolder()
{
return Str::reset($this->argument('name'))->plural()->slug();
}

/**
* Check if any view options were set.
*
Expand All @@ -99,13 +82,27 @@ public function isUsingAllViews()
/**
* Locate and make the view with the found stub.
*
* @param string $folder
* @param string $view
*
* @return void
* @return bool
*/
public function makeView(string $folder, string $view)
public function makeView(string $view)
{
// $path = config('stubkit.views.path', 'js/Pages/{{model.studlyPlural}}/{{view.studly}}.vue');
$path = config('stubkit.view_path');

$values = [
'view' => $view,
'model' => Str::reset($this->argument('name'))
];

$syntax = (new Syntax())->make(
$values,
config('stubkit.variables.*', [])
);

$path = $syntax->parse($path);

if (file_exists(base_path("stubs/view.${view}.stub"))) {
$stub = base_path("stubs/view.${view}.stub");
} elseif (file_exists(__DIR__."/../../stubs/view.${view}.stub")) {
Expand All @@ -114,14 +111,23 @@ public function makeView(string $folder, string $view)
$stub = false;
}

$content = ($stub)
? file_get_contents($stub)
: '';
$path = resource_path($path);

file_put_contents(
resource_path("views/${folder}/${view}.blade.php"),
$content
);
$content = ($stub) ? file_get_contents($stub) : '';

$folder = Str::beforeLast($path, DIRECTORY_SEPARATOR);

if (file_exists($path)) {
$this->info('Views already exists!');

return false;
}

if(! is_dir($folder)) {
mkdir($folder, 0777, true);
}

return file_put_contents($path, $content);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/StubKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Support\Arr;
use Illuminate\Support\Stringable;
use PHPUnit\Framework\Assert;
use StubKit\Support\Fields;
use StubKit\Support\Item;
Expand Down Expand Up @@ -373,6 +374,8 @@ public function helper($expression, array $variables = [])
unset($variables[$key]);
} elseif (is_a($value, Item::class)) {
$variables[$key] = $value->field;
} elseif (is_a($value, Stringable::class)) {
$variables[$key] = (string) $value;
} elseif (! is_string($value)) {
unset($variables[$key]);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Support/Syntax.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function parse(string $content)
{
foreach ($this->variables as $search => $replace) {
$content = preg_replace_callback("/\s*{{\s*${search}\s*}}/", function ($matches) use ($replace) {
preg_match('/(\s*){{/', $matches[0], $space);

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

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

preg_match('/(\s*){{/', $matches[0], $space);

if (count($lines) == 1) {
return $space[1].implode('', $lines);
}
Expand All @@ -45,6 +46,7 @@ public function parse(string $content)

return rtrim(implode('', $lines));
}, $content);

}

return $content;
Expand Down
3 changes: 2 additions & 1 deletion tests/MakeViewsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function test_make_views_with_file_flag()

public function test_make_views_when_already_exists()
{
mkdir(__DIR__.'/Fixtures/app/resources/views/users');
mkdir(__DIR__.'/Fixtures/app/resources/views/users', 0777, true);
file_put_contents(__DIR__.'/Fixtures/app/resources/views/users/index.blade.php', '');

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

0 comments on commit 3a5312a

Please sign in to comment.