Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioassuncao committed Jun 1, 2023
0 parents commit 9d7ef56
Show file tree
Hide file tree
Showing 13 changed files with 1,059 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 https://github.com/fabioassuncao

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
179 changes: 179 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# Laravel Modules With Livewire

Using [Laravel Livewire](https://github.com/livewire/livewire) in [Laravel Modules](https://github.com/nWidart/laravel-modules) package with automatically registered livewire components for every modules.



### Installation:

Install through composer:

```
composer require codions/laravel-modules-livewire
```

Publish the package's configuration file:

```
php artisan vendor:publish --tag=modules-livewire-config
```

### Making Components:

**Command Signature:**

`php artisan module:make-livewire <Component> <Module> --view= --force --inline --stub= --custom`

**Example:**

```
php artisan module:make-livewire Pages/AboutPage Core
```

```
php artisan module:make-livewire Pages\\AboutPage Core
```

```
php artisan module:make-livewire pages.about-page Core
```

**Force create component if the class already exists:**

```
php artisan module:make-livewire Pages/AboutPage Core --force
```

**Output:**

```
COMPONENT CREATED
CLASS: Modules/Core/Http/Livewire/Pages/AboutPage.php
VIEW: Modules/Core/Resources/views/livewire/pages/about-page.blade.php
TAG: <livewire:core::pages.about-page />
```

**Inline Component:**

```
php artisan module:make-livewire Core Pages/AboutPage --inline
```

**Output:**

```
COMPONENT CREATED
CLASS: Modules/Core/Http/Livewire/Pages/AboutPage.php
TAG: <livewire:core::pages.about-page />
```

**Modifying Stubs:**

Publish the package's stubs:

```
php artisan vendor:publish --tag=modules-livewire-stub
```

After publishing the stubs, will create these files. And when running the make command, will use these stub files by default.

```
stubs/modules-livewire/livewire.inline.stub
stubs/modules-livewire/livewire.stub
stubs/modules-livewire/livewire.view.stub
```

**You're able to set a custom stub directory for component with (--stub) option.**

```
php artisan module:make-livewire Core Pages/AboutPage --stub=about
```

```
php artisan module:make-livewire Core Pages/AboutPage --stub=modules-livewire/core
```

```
php artisan module:make-livewire Core Pages/AboutPage --stub=./
```

**Extra Option (--view):**

**You're able to set a custom view path for component with (--view) option.**

**Example:**

```
php artisan module:make-livewire Pages/AboutPage Core --view=pages/about
```

```
php artisan module:make-livewire Pages/AboutPage Core --view=pages.about
```

**Output:**

```
COMPONENT CREATED
CLASS: Modules/Core/Http/Livewire/Pages/AboutPage.php
VIEW: Modules/Core/Resources/views/livewire/pages/about.blade.php
TAG: <livewire:core::pages.about-page />
```
### Rendering Components:

`<livewire:{module-lower-name}::component-class-kebab-case />`

**Example:**

```
<livewire:core::pages.about-page />
```
### Custom Module:

**To create components for the custom module, should be add custom modules in the config file.**

The config file is located at `config/modules-livewire.php` after publishing the config file.

Remove comment for these lines & add your custom modules.

```
/*
|--------------------------------------------------------------------------
| Custom modules setup
|--------------------------------------------------------------------------
|
*/
// 'custom_modules' => [
// 'Chat' => [
// 'path' => base_path('libraries/Chat'),
// 'module_namespace' => 'Libraries\\Chat',
// // 'namespace' => 'Http\\Livewire',
// // 'view' => 'Resources/views/livewire',
// // 'name_lower' => 'chat',
// ],
// ],
```

**Custom module config details**

> **path:** Add module full path (required).
>
> **module_namespace:** Add module namespace (required).
>
> **namespace:** By default using `config('modules-livewire.namespace')` value. You can set a different value for the specific module.
>
> **view:** By default using `config('modules-livewire.view')` value. You can set a different value for the specific module.
>
> **name_lower:** By default using module name to lowercase. If you set a custom name, module components will be register by custom name.
>

## Credits
- This project is a modified version of [mhmiton/laravel-modules-livewire](https://github.com/mhmiton/laravel-modules-livewire), created as a fork with additional changes.

## License
Laravel Themes Manager is open-sourced software licensed under the [MIT license](LICENSE).
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "codions/laravel-modules-livewire",
"description": "Using Laravel Livewire in Laravel Modules package with automatically registered livewire components for every modules.",
"keywords": [
"laravel",
"modules",
"module",
"livewire",
"nwidart",
"mhmiton"
],
"license": "MIT",
"authors": [
{
"name": "Fábio Assunção",
"email": "[email protected]"
}
],
"type": "library",
"require": {
"php": ">=7.3"
},
"require-dev": {
"laravel/framework": "^7.0|^8.0|^9.0|^10.0"
},
"extra": {
"laravel": {
"providers": [
"Codions\\LaravelModulesLivewire\\LaravelModulesLivewireServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Codions\\LaravelModulesLivewire\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
40 changes: 40 additions & 0 deletions config/modules-livewire.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Class Namespace
|--------------------------------------------------------------------------
|
*/

'namespace' => 'Http\\Livewire',

/*
|--------------------------------------------------------------------------
| View Path
|--------------------------------------------------------------------------
|
*/

'view' => 'Resources/views/livewire',

/*
|--------------------------------------------------------------------------
| Custom modules setup
|--------------------------------------------------------------------------
|
*/

// 'custom_modules' => [
// 'Chat' => [
// 'path' => base_path('libraries/Chat'),
// 'module_namespace' => 'Libraries\\Chat',
// // 'namespace' => 'Http\\Livewire',
// // 'view' => 'Resources/views/livewire',
// // 'name_lower' => 'chat',
// ],
// ],

];
96 changes: 96 additions & 0 deletions src/Commands/LivewireMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Codions\LaravelModulesLivewire\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Codions\LaravelModulesLivewire\Traits\ComponentParser;

class LivewireMakeCommand extends Command
{
use ComponentParser;

protected $signature = 'module:make-livewire {component} {module} {--view=} {--force} {--inline} {--stub=} {--custom}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate Livewire Component.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
if (! $this->parser()) {
return false;
}

if (! $this->checkClassNameValid()) {
return false;
}

if (! $this->checkReservedClassName()) {
return false;
}

$class = $this->createClass();

$view = $this->createView();

if ($class || $view) {
$this->line("<options=bold,reverse;fg=green> COMPONENT CREATED </> 🤙\n");

$class && $this->line("<options=bold;fg=green>CLASS:</> {$this->getClassSourcePath()}");

$view && $this->line("<options=bold;fg=green>VIEW:</> {$this->getViewSourcePath()}");

$class && $this->line("<options=bold;fg=green>TAG:</> {$class->tag}");
}

return false;
}

protected function createClass()
{
$classFile = $this->component->class->file;

if (File::exists($classFile) && ! $this->isForce()) {
$this->line("<options=bold,reverse;fg=red> WHOOPS-IE-TOOTLES </> 😳 \n");
$this->line("<fg=red;options=bold>Class already exists:</> {$this->getClassSourcePath()}");

return false;
}

$this->ensureDirectoryExists($classFile);

File::put($classFile, $this->getClassContents());

return $this->component->class;
}

protected function createView()
{
if ($this->isInline()) {
return false;
}

$viewFile = $this->component->view->file;

if (File::exists($viewFile) && ! $this->isForce()) {
$this->line("<fg=red;options=bold>View already exists:</> {$this->getViewSourcePath()}");

return false;
}

$this->ensureDirectoryExists($viewFile);

File::put($viewFile, $this->getViewContents());

return $this->component->view;
}
}
17 changes: 17 additions & 0 deletions src/Commands/stubs/livewire.inline.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace [namespace];

use Livewire\Component;

class [class] extends Component
{
public function render()
{
return <<<'blade'
<div>
<h3>[quote]</h3>
</div>
blade;
}
}
Loading

0 comments on commit 9d7ef56

Please sign in to comment.