Skip to content

Commit 154b5b0

Browse files
committed
remove provider
1 parent 559976f commit 154b5b0

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

src/Illuminate/Support/ServiceProvider.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,51 @@ public static function addProviderToBootstrapFile(string $provider, ?string $pat
572572

573573
$content = '<?php
574574
575+
return [
576+
'.$providers.'
577+
];';
578+
579+
file_put_contents($path, $content.PHP_EOL);
580+
581+
return true;
582+
}
583+
584+
/**
585+
* Remove a provider from the application's provider bootstrap file.
586+
*
587+
* @param string|array $providersToRemove
588+
* @param string|null $path
589+
* @param bool $strict
590+
* @return bool
591+
*/
592+
public static function removeProviderFromBootstrapFile(string|array $providersToRemove, ?string $path = null, bool $strict = false)
593+
{
594+
$path ??= app()->getBootstrapProvidersPath();
595+
596+
if (! file_exists($path)) {
597+
return false;
598+
}
599+
600+
if (function_exists('opcache_invalidate')) {
601+
opcache_invalidate($path, true);
602+
}
603+
604+
$providersToRemove = Arr::wrap($providersToRemove);
605+
606+
$providers = (new Collection(require $path))
607+
->unique()
608+
->sort()
609+
->values()
610+
->when(
611+
$strict,
612+
static fn (Collection $providerCollection) => $providerCollection->reject(fn (string $p) => in_array($p, $providersToRemove, true)),
613+
static fn (Collection $providerCollection) => $providerCollection->reject(fn (string $p) => Str::contains($p, $providersToRemove))
614+
)
615+
->map(fn ($p) => ' '.$p.'::class,')
616+
->implode(PHP_EOL);
617+
618+
$content = '<?php
619+
575620
return [
576621
'.$providers.'
577622
];';

tests/Support/SupportServiceProviderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
class SupportServiceProviderTest extends TestCase
1313
{
1414
protected $app;
15+
protected string $tempFile;
1516

1617
protected function setUp(): void
1718
{
@@ -30,11 +31,16 @@ protected function setUp(): void
3031
$one->boot();
3132
$two = new ServiceProviderForTestingTwo($app);
3233
$two->boot();
34+
35+
$this->tempFile = __DIR__ . '/providers.php';
3336
}
3437

3538
protected function tearDown(): void
3639
{
3740
m::close();
41+
if ($this->tempFile && file_exists($this->tempFile)) {
42+
@unlink($this->tempFile);
43+
}
3844
}
3945

4046
public function testPublishableServiceProviders()
@@ -191,6 +197,46 @@ public function testLoadTranslationsFromWithNamespace()
191197
$provider = new ServiceProviderForTestingOne($this->app);
192198
$provider->loadTranslationsFrom(__DIR__.'/translations', 'namespace');
193199
}
200+
201+
public function test_can_remove_provider()
202+
{
203+
204+
$r = file_put_contents($this->tempFile, $contents = <<< PHP
205+
<?php
206+
207+
return [
208+
App\Providers\AppServiceProvider::class,
209+
App\Providers\TelescopeServiceProvider::class,
210+
];
211+
PHP
212+
);
213+
ServiceProvider::removeProviderFromBootstrapFile('TelescopeServiceProvider', $this->tempFile, true);
214+
215+
// Should have deleted nothing
216+
$this->assertSame($contents, trim(file_get_contents($this->tempFile)));
217+
218+
ServiceProvider::removeProviderFromBootstrapFile('App\Providers\TelescopeServiceProvider', $this->tempFile, true);
219+
220+
$this->assertSame(<<< PHP
221+
<?php
222+
223+
return [
224+
App\Providers\AppServiceProvider::class,
225+
];
226+
PHP
227+
, trim(file_get_contents($this->tempFile)));
228+
229+
ServiceProvider::removeProviderFromBootstrapFile('AppServiceProvider', $this->tempFile);
230+
231+
$this->assertSame(<<< 'PHP'
232+
<?php
233+
234+
return [
235+
236+
];
237+
PHP
238+
, trim(file_get_contents($this->tempFile)));
239+
}
194240
}
195241

196242
class ServiceProviderForTestingOne extends ServiceProvider

0 commit comments

Comments
 (0)