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
52 changes: 52 additions & 0 deletions src/Console/Commands/Foundation/VendorPublishCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace InterNACHI\Modular\Console\Commands\Foundation;

use Illuminate\Foundation\Events\VendorTagPublished;
use InterNACHI\Modular\Console\Commands\Modularize;
use InterNACHI\Modular\Support\ModuleConfig;

class VendorPublishCommand extends \Illuminate\Foundation\Console\VendorPublishCommand
{
use Modularize;

protected function publishTag($tag)
{
$pathsToPublish = $this->pathsToPublish($tag);

if ($publishing = count($pathsToPublish) > 0) {
$this->components->info(sprintf(
'Publishing %sassets',
$tag ? "[$tag] " : '',
));
}

$module = $this->module();

foreach ($pathsToPublish as $from => &$to) {
if ($module) {
$this->updateTargetPath($module, $to);
}

$this->publishItem($from, $to);
}

if ($publishing === false) {
$this->components->info('No publishable resources for tag ['.$tag.'].');
} else {
$this->laravel['events']->dispatch(new VendorTagPublished($tag, $pathsToPublish));

$this->newLine();
}
}

protected function updateTargetPath(ModuleConfig $module, string &$path)
{
$base_path = $this->laravel->basePath();
$module_path = $module->path();

$path = str_replace($base_path, $module_path, $path);
}
}
24 changes: 17 additions & 7 deletions src/Support/ModularizedCommandsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Illuminate\Console\Application;
use Illuminate\Console\Application as Artisan;
use Illuminate\Database\Console\Migrations\MigrateMakeCommand as OriginalMakeMigrationCommand;
use Illuminate\Foundation\Console\VendorPublishCommand as OriginalVendorPublishCommand;
use Illuminate\Support\ServiceProvider;
use InterNACHI\Modular\Console\Commands\Database\SeedCommand;
use InterNACHI\Modular\Console\Commands\Foundation\VendorPublishCommand;
use InterNACHI\Modular\Console\Commands\Make\MakeCast;
use InterNACHI\Modular\Console\Commands\Make\MakeChannel;
use InterNACHI\Modular\Console\Commands\Make\MakeCommand;
Expand Down Expand Up @@ -60,7 +62,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 @@ -71,41 +73,42 @@ public function register(): void
$this->registerMakeCommandOverrides();
$this->registerMigrationCommandOverrides();
$this->registerLivewireOverrides($artisan);
$this->registerVendorPuslishCommandOverride();
});
});
}

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 All @@ -115,4 +118,11 @@ protected function registerLivewireOverrides(Artisan $artisan)
return new MakeLivewire();
});
}

protected function registerVendorPuslishCommandOverride()
{
$this->app->singleton(OriginalVendorPublishCommand::class, function($app) {
return new VendorPublishCommand($app['files']);
});
}
}
36 changes: 36 additions & 0 deletions tests/Commands/Foundation/VendorPublishCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace InterNACHI\Modular\Tests\Commands\Foundation;

use InterNACHI\Modular\Console\Commands\Foundation\VendorPublishCommand;
use InterNACHI\Modular\Tests\Concerns\TestsMakeCommands;
use InterNACHI\Modular\Tests\Concerns\WritesToAppFilesystem;
use InterNACHI\Modular\Tests\TestCase;

class VendorPublishCommandTest extends TestCase
{
use WritesToAppFilesystem;
use TestsMakeCommands;

public function test_it_overrides_the_default_command(): void
{
$this->requiresLaravelVersion('9.2.0');

$this->artisan('vendor:publish', ['--help' => true])
->expectsOutputToContain('--module')
->assertExitCode(0);
}

public function test_it_published_to_correct_module()
{
$this->requiresLaravelVersion('9.2.0');

$command = VendorPublishCommand::class;
$arguments = ['--tag' => 'laravel-mail'];
$expected_path = 'resources/views/vendor/mail/html/layout.blade.php';

$this->assertModuleCommandResults($command, $arguments, $expected_path, []);
}
}