-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add a
make:navi
view component generator command for Acorn (#76)
- Loading branch information
Showing
8 changed files
with
280 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
39 changes: 39 additions & 0 deletions
39
examples/sage/resources/views/components/navigation.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
@props([ | ||
'name' => null, | ||
'inactive' => 'hover:text-blue-500', | ||
'active' => 'text-blue-500', | ||
]) | ||
|
||
@php($menu = Navi::build($name)) | ||
|
||
@if ($menu->isNotEmpty()) | ||
<ul {{ $attributes }}> | ||
@foreach ($menu->all() as $item) | ||
<li @class([ | ||
$item->classes, | ||
$inactive => ! $item->active, | ||
$active => $item->active, | ||
])> | ||
<a href="{{ $item->url }}"> | ||
{{ $item->label }} | ||
</a> | ||
|
||
@if ($item->children) | ||
<ul> | ||
@foreach ($item->children as $child) | ||
<li @class([ | ||
$child->classes, | ||
$inactive => ! $child->active, | ||
$active => $child->active, | ||
])> | ||
<a href="{{ $child->url }}"> | ||
{{ $child->label }} | ||
</a> | ||
</li> | ||
@endforeach | ||
</ul> | ||
@endif | ||
</li> | ||
@endforeach | ||
</ul> | ||
@endif |
23 changes: 0 additions & 23 deletions
23
examples/sage/resources/views/partials/navigation.blade.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
namespace Log1x\Navi\Console; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class NaviMakeCommand extends GeneratorCommand | ||
{ | ||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a Navi component'; | ||
|
||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'make:navi'; | ||
|
||
/** | ||
* The type of file being generated. | ||
* | ||
* @var string | ||
*/ | ||
protected $type = 'Component'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return bool|null | ||
*/ | ||
public function handle() | ||
{ | ||
if (parent::handle() === false) { | ||
return false; | ||
} | ||
|
||
$name = strtolower(trim($this->argument('name'))); | ||
|
||
$this->components->info("Navi component <fg=blue><x-{$name} /></> is ready for use."); | ||
} | ||
|
||
/** | ||
* Build the class with the given name. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
protected function buildClass($name) | ||
{ | ||
$contents = parent::buildClass($name); | ||
|
||
return str_replace( | ||
'{{ default }}', | ||
$this->option('default') ? Str::wrap($this->option('default'), "'") : 'null', | ||
$contents, | ||
); | ||
} | ||
|
||
/** | ||
* Get the destination view path. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
protected function getPath($name) | ||
{ | ||
$path = $this->viewPath( | ||
str_replace('.', '/', 'components.'.$this->getView()).'.blade.php' | ||
); | ||
|
||
if (! $this->files->isDirectory(dirname($path))) { | ||
$this->files->makeDirectory(dirname($path), 0777, true, true); | ||
} | ||
|
||
return $path; | ||
} | ||
|
||
/** | ||
* Get the view name relative to the components directory. | ||
* | ||
* @return string | ||
*/ | ||
protected function getView() | ||
{ | ||
$name = str_replace('\\', '/', $this->argument('name')); | ||
|
||
return collect(explode('/', $name)) | ||
->map(fn ($part) => Str::kebab($part)) | ||
->implode('.'); | ||
} | ||
|
||
/** | ||
* Get the desired view name from the input. | ||
* | ||
* @return string | ||
*/ | ||
protected function getNameInput() | ||
{ | ||
$name = trim($this->argument('name')); | ||
|
||
$name = str_replace(['\\', '.'], '/', $this->argument('name')); | ||
|
||
return $name; | ||
} | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
* | ||
* @return string | ||
*/ | ||
protected function getStub() | ||
{ | ||
return $this->resolveStubPath( | ||
'/stubs/view.stub', | ||
); | ||
} | ||
|
||
/** | ||
* Resolve the fully-qualified path to the stub. | ||
* | ||
* @param string $stub | ||
* @return string | ||
*/ | ||
protected function resolveStubPath($stub) | ||
{ | ||
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) | ||
? $customPath | ||
: __DIR__.$stub; | ||
} | ||
|
||
/** | ||
* Prompt for missing input arguments using the returned questions. | ||
* | ||
* @return array | ||
*/ | ||
protected function promptForMissingArgumentsUsing() | ||
{ | ||
return [ | ||
'name' => [ | ||
'What should the Navi component be named?', | ||
'E.g. Navigation', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getOptions() | ||
{ | ||
return [ | ||
['default', 'd', InputOption::VALUE_OPTIONAL, 'The default menu name'], | ||
['force', 'f', InputOption::VALUE_NONE, 'Create the view component even if the component already exists'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
@props([ | ||
'name' => {{ default }}, | ||
'inactive' => 'hover:text-blue-500', | ||
'active' => 'text-blue-500', | ||
]) | ||
|
||
@php($menu = Navi::build($name)) | ||
|
||
@if ($menu->isNotEmpty()) | ||
<ul {{ $attributes }}> | ||
@foreach ($menu->all() as $item) | ||
<li @class([ | ||
$item->classes, | ||
$inactive => ! $item->active, | ||
$active => $item->active, | ||
])> | ||
<a href="{{ $item->url }}"> | ||
{{ $item->label }} | ||
</a> | ||
|
||
@if ($item->children) | ||
<ul> | ||
@foreach ($item->children as $child) | ||
<li @class([ | ||
$child->classes, | ||
$inactive => ! $child->active, | ||
$active => $child->active, | ||
])> | ||
<a href="{{ $child->url }}"> | ||
{{ $child->label }} | ||
</a> | ||
</li> | ||
@endforeach | ||
</ul> | ||
@endif | ||
</li> | ||
@endforeach | ||
</ul> | ||
@endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters