Skip to content
Merged
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
32 changes: 32 additions & 0 deletions src/Illuminate/Foundation/ComposerScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Illuminate\Foundation;

use Composer\Installer\PackageEvent;
use Composer\Script\Event;
use Illuminate\Contracts\Console\Kernel;

class ComposerScripts
{
Expand Down Expand Up @@ -45,6 +47,36 @@ public static function postAutoloadDump(Event $event)
static::clearCompiled();
}

/**
* Handle the pre-package-uninstall Composer event.
*
* @param \Composer\Installer\PackageEvent $event
* @return void
*/
public static function prePackageUninstall(PackageEvent $event)
{
$bootstrapFile = dirname($vendorDir = $event->getComposer()->getConfig()->get('vendor-dir')).'/bootstrap/app.php';

if (! file_exists($bootstrapFile)) {
return;
}

require_once $vendorDir.'/autoload.php';

define('LARAVEL_START', microtime(true));

/** @var Application $app */
$app = require_once $bootstrapFile;

$app->make(Kernel::class)->bootstrap();

/** @var \Composer\DependencyResolver\Operation\UninstallOperation $uninstallOperation */
$uninstallOperation = $event->getOperation()->getPackage();

$app['events']->dispatch('composer_package.'.$uninstallOperation->getName().':pre_uninstall');
}


/**
* Clear the cached Laravel bootstrapping files.
*
Expand Down
45 changes: 45 additions & 0 deletions src/Illuminate/Support/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,51 @@ public static function addProviderToBootstrapFile(string $provider, ?string $pat

$content = '<?php

return [
'.$providers.'
];';

file_put_contents($path, $content.PHP_EOL);

return true;
}

/**
* Remove a provider from the application's provider bootstrap file.
*
* @param string|array $providersToRemove
* @param string|null $path
* @param bool $strict
* @return bool
*/
public static function removeProviderFromBootstrapFile(string|array $providersToRemove, ?string $path = null, bool $strict = false)
{
$path ??= app()->getBootstrapProvidersPath();

if (! file_exists($path)) {
return false;
}

if (function_exists('opcache_invalidate')) {
opcache_invalidate($path, true);
}

$providersToRemove = Arr::wrap($providersToRemove);

$providers = (new Collection(require $path))
->unique()
->sort()
->values()
->when(
$strict,
static fn (Collection $providerCollection) => $providerCollection->reject(fn (string $p) => in_array($p, $providersToRemove, true)),
static fn (Collection $providerCollection) => $providerCollection->reject(fn (string $p) => Str::contains($p, $providersToRemove))
)
->map(fn ($p) => ' '.$p.'::class,')
->implode(PHP_EOL);

$content = '<?php

return [
'.$providers.'
];';
Expand Down
46 changes: 46 additions & 0 deletions tests/Support/SupportServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class SupportServiceProviderTest extends TestCase
{
protected $app;
protected string $tempFile;

protected function setUp(): void
{
Expand All @@ -35,6 +36,9 @@ protected function setUp(): void
protected function tearDown(): void
{
m::close();
if (isset($this->tempFile) && file_exists($this->tempFile)) {
@unlink($this->tempFile);
}
}

public function testPublishableServiceProviders()
Expand Down Expand Up @@ -191,6 +195,48 @@ public function testLoadTranslationsFromWithNamespace()
$provider = new ServiceProviderForTestingOne($this->app);
$provider->loadTranslationsFrom(__DIR__.'/translations', 'namespace');
}

public function test_can_remove_provider()
{
$this->tempFile = __DIR__.'/providers.php';
file_put_contents($this->tempFile, $contents = <<< PHP
<?php

return [
App\Providers\AppServiceProvider::class,
App\Providers\TelescopeServiceProvider::class,
];
PHP
);
ServiceProvider::removeProviderFromBootstrapFile('TelescopeServiceProvider', $this->tempFile, true);

// Should have deleted nothing
$this->assertStringEqualsStringIgnoringLineEndings($contents, trim(file_get_contents($this->tempFile)));

// Should delete the telescope provider
ServiceProvider::removeProviderFromBootstrapFile('App\Providers\TelescopeServiceProvider', $this->tempFile, true);

$this->assertStringEqualsStringIgnoringLineEndings(<<< PHP
<?php

return [
App\Providers\AppServiceProvider::class,
];
PHP
, trim(file_get_contents($this->tempFile)));

// Should fuzzily delete the App\Providers\AppServiceProvider class
ServiceProvider::removeProviderFromBootstrapFile('AppServiceProvider', $this->tempFile);

$this->assertStringEqualsStringIgnoringLineEndings(<<< 'PHP'
<?php

return [

];
PHP
, trim(file_get_contents($this->tempFile)));
}
}

class ServiceProviderForTestingOne extends ServiceProvider
Expand Down