Skip to content

Commit

Permalink
config updated
Browse files Browse the repository at this point in the history
Signed-off-by: Ritesh Singh <[email protected]>
  • Loading branch information
riteshsingh1 committed Apr 7, 2021
1 parent 60cd499 commit 81b1de4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "imritesh/livecrud",
"description": "Generate Basic Crud Operations With Livewire and Tailwind Css For Laravel",
"license": "MIT",
"version": "1.0.0",
"version": "1.1.0",
"authors": [
{
"name": "Ritesh Singh",
Expand Down
14 changes: 12 additions & 2 deletions config/livecrud.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<?php

return [
//
];
/**
* Default CSS Library / Framework
* Supported Values : 'tailwind', 'bootstrap'
*/
'template' => 'tailwind',

/**
* Models Path
*/
'models_path' => 'App\\Models'

];
21 changes: 19 additions & 2 deletions src/Commands/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,22 @@ public function handle()
{
parent::handle();


// Get the fully qualified class name (FQN)
$class = $this->qualifyClass($this->getNameInput());

// get the destination path, based on the default namespace
$path = $this->getPath($class);
$content = file_get_contents($path);
// Update the file content with additional data (regular expressions)
$content = $this->buildContent($content);
$this->info('Generating Livewire Component');

$content = $this->buildContent($content);
file_put_contents($path, $content);
$this->info('Component Generated',);
$this->info('Livewire Component Generated');

$this->info('Generating View');

Artisan::call('crud:view', ['name' => $this->arguments()['name']]);
}

Expand Down Expand Up @@ -114,6 +118,9 @@ public function buildProperties(): string
{
$class = 'App\\Models\\' . $this->arguments()['name'];
$model = new $class;
if (!class_exists($class)){
throw new \Exception('Model Not Found. Please Check if Model Exists at -'.$class);
}
$columns = $model->getFillable();
$str = '';
$c = 1;
Expand Down Expand Up @@ -185,10 +192,20 @@ protected function usedClasses()
*/
protected function getStub()
{
$this->checkIfModelExists();

if (file_exists(base_path() . '/stubs/crud.php.stub')){
return base_path() . '/stubs/crud.php.stub';
}
return base_path().'/vendor/imritesh/livecrud/src/stubs/crud.php.stub';
}

public function checkIfModelExists()
{
$class = 'App\\Models\\' . $this->arguments()['name'];
if (!class_exists($class)){
throw new \Exception('Model Not Found. Please Check if Model Exists at -'.$class);
}
}

}
30 changes: 29 additions & 1 deletion src/Commands/LiveCrudView.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class LiveCrudView extends GeneratorCommand

protected $description = 'Generate View For Crud Command';


protected $emailNames = [
'email',
'email_address',
];

public function handle()
{
if (!is_dir(resource_path('views/livewire'))){
Expand All @@ -22,6 +28,8 @@ public function handle()
$content = file_get_contents($this->getStub());
$content = $this->buildContent($content);
file_put_contents($viewPath, $content);

$this->info('View Generated');
}

public function buildContent($content)
Expand Down Expand Up @@ -58,9 +66,23 @@ public function getForm()

public function makeInput($name)
{
$type = $this->getType($name);
$label = ucfirst(str_replace('-', ' ', Str::slug($name)));
$message = '{{ $message }}';
return "<div><label class='block'><span class='text-gray-700 @error('{$name}') text-red-500 @enderror'>{$label}</span><input type='text' class='mt-1 block w-full rounded-md border-gray-300 shadow-sm @error('{$name}') border-red-500 @enderror focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50' wire:model='{$name}'>@error('{$name}')<span class='text-red-500 text-sm'>{$message}</span>@enderror</label></div>";
return "<div><label class='block'><span class='text-gray-700 @error('{$name}') text-red-500 @enderror'>{$label}</span><input type='{$type}' class='mt-1 block w-full rounded-md border-gray-300 shadow-sm @error('{$name}') border-red-500 @enderror focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50' wire:model='{$name}'>@error('{$name}')<span class='text-red-500 text-sm'>{$message}</span>@enderror</label></div>";
}

public function getType($name)
{
if(in_array(strtolower($name), $this->emailNames))
{
return 'email';
}
if (strtolower($name) == 'password')
{
return 'password';
}
return 'text';
}

public function getRenderedData()
Expand Down Expand Up @@ -126,9 +148,15 @@ public function getInput($name): string
* Get the stub file for the generator.
*
* @return string
* @throws \Exception
*/
protected function getStub()
{
if (config('livecrud.template') != 'tailwind')
{
throw new \Exception(config('livecrud.template').' is not currently supported, only supported template is tailwind css');
}

if (file_exists(base_path() . '/stubs/view.php.stub')){
return base_path() . '/stubs/view.php.stub';
}
Expand Down
2 changes: 1 addition & 1 deletion src/stubs/view.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
</div>
</div>
<div class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
<button wire:click="store()" type="button"
<button @if($mode == 'create') wire:click="store()" @else wire:click="update()" @endif type="button"
class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-green-600 text-base font-medium text-white hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 sm:ml-3 sm:w-auto sm:text-sm">
{{ $mode == 'create' ? 'Save Record' : 'Update Record' }}
</button>
Expand Down

0 comments on commit 81b1de4

Please sign in to comment.