Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions src/Console/Commands/Make/MakeLivewire.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,84 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Livewire\Commands\MakeCommand;
use Livewire\Features\SupportConsoleCommands\Commands\MakeCommand;
use Livewire\Livewire;
use Livewire\LivewireComponentsFinder;

if (class_exists(MakeCommand::class)) {
class MakeLivewire extends MakeCommand
{
use Modularize;

public function getAliases(): array
{
return ['make:livewire', 'livewire:make'];
}

public function handle()
{
if ($module = $this->module()) {
Config::set('livewire.class_namespace', $module->qualify('Http\\Livewire'));
Config::set('livewire.view_path', $module->path('resources/views/livewire'));

$app = $this->getLaravel();

$defaultManifestPath = $app['livewire']->isRunningServerless()
? '/tmp/storage/bootstrap/cache/livewire-components.php'
: $app->bootstrapPath('cache/livewire-components.php');

$componentsFinder = new LivewireComponentsFinder(
new Filesystem(),
Config::get('livewire.manifest_path') ?? $defaultManifestPath,
$module->path('src/Http/Livewire')
);

$app->instance(LivewireComponentsFinder::class, $componentsFinder);

if (class_exists("Livewire\LivewireComponentsFinder")) {
$componentsFinder = new \Livewire\LivewireComponentsFinder(
new Filesystem(),
Config::get('livewire.manifest_path') ?? $defaultManifestPath,
$module->path('src/Http/Livewire')
);

$app->instance(\Livewire\LivewireComponentsFinder::class, $componentsFinder);
}

}

parent::handle();
}

protected function createClass($force = false, $inline = false)
{
if ($module = $this->module()) {
$name = Str::of($this->argument('name'))
->split('/[.\/(\\\\)]+/')
->map([Str::class, 'studly'])
->join(DIRECTORY_SEPARATOR);

$classPath = $module->path('src/Http/Livewire/'.$name.'.php');

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

return false;
}

$this->ensureDirectoryExists($classPath);

File::put($classPath, $this->parser->classContents($inline));

$component_name = Str::of($name)
->explode('/')
->filter()
->map([Str::class, 'kebab'])
->implode('.');

$fully_qualified_component = Str::of($this->argument('name'))
->prepend('Http/Livewire/')
->split('/[.\/(\\\\)]+/')
->map([Str::class, 'studly'])
->join('\\');

Livewire::component("{$module->name}::{$component_name}", $module->qualify($fully_qualified_component));

return $classPath;
}

return parent::createClass($force, $inline);
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/Support/ModularizedCommandsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use InterNACHI\Modular\Console\Commands\Make\MakeRule;
use InterNACHI\Modular\Console\Commands\Make\MakeSeeder;
use InterNACHI\Modular\Console\Commands\Make\MakeTest;
use Livewire\Commands as Livewire;
use Livewire\Features\SupportConsoleCommands\Commands as Livewire;

class ModularizedCommandsServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -60,7 +60,7 @@ class ModularizedCommandsServiceProvider extends ServiceProvider
'command.component.make' => MakeComponent::class,
'command.seed' => SeedCommand::class,
];

public function register(): void
{
// Register our overrides via the "booted" event to ensure that we override
Expand All @@ -74,38 +74,38 @@ public function register(): void
});
});
}

protected function registerMakeCommandOverrides()
{
foreach ($this->overrides as $alias => $class_name) {
$this->app->singleton($alias, $class_name);
$this->app->singleton(get_parent_class($class_name), $class_name);
}
}

protected function registerMigrationCommandOverrides()
{
// Laravel 8
$this->app->singleton('command.migrate.make', function($app) {
return new MakeMigration($app['migration.creator'], $app['composer']);
});

// Laravel 9
$this->app->singleton(OriginalMakeMigrationCommand::class, function($app) {
return new MakeMigration($app['migration.creator'], $app['composer']);
});
}

protected function registerLivewireOverrides(Artisan $artisan)
{
// Don't register commands if Livewire isn't installed
if (! class_exists(Livewire\MakeCommand::class)) {
return;
}

// Replace the resolved command with our subclass
$artisan->resolveCommands([MakeLivewire::class]);

// Ensure that if 'make:livewire' or 'livewire:make' is resolved from the container
// in the future, our subclass is used instead
$this->app->extend(Livewire\MakeCommand::class, function() {
Expand Down