mirror of
https://github.com/flarum/core.git
synced 2025-07-21 16:51:34 +02:00
Add Service Provider Extender (#2437)
This commit is contained in:
40
framework/core/src/Extend/ServiceProvider.php
Normal file
40
framework/core/src/Extend/ServiceProvider.php
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Extend;
|
||||||
|
|
||||||
|
use Flarum\Extension\Extension;
|
||||||
|
use Illuminate\Contracts\Container\Container;
|
||||||
|
|
||||||
|
class ServiceProvider implements ExtenderInterface
|
||||||
|
{
|
||||||
|
private $providers = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a service provider.
|
||||||
|
*
|
||||||
|
* @param string $serviceProviderClass The ::class attribute of the service provider class.
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function register(string $serviceProviderClass)
|
||||||
|
{
|
||||||
|
$this->providers[] = $serviceProviderClass;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function extend(Container $container, Extension $extension = null)
|
||||||
|
{
|
||||||
|
$app = $container->make('flarum');
|
||||||
|
|
||||||
|
foreach ($this->providers as $provider) {
|
||||||
|
$app->register($provider);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,119 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* For detailed copyright and license information, please view the
|
||||||
|
* LICENSE file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Tests\integration\extenders;
|
||||||
|
|
||||||
|
use Flarum\Extend;
|
||||||
|
use Flarum\Foundation\AbstractServiceProvider;
|
||||||
|
use Flarum\Tests\integration\TestCase;
|
||||||
|
|
||||||
|
class ServiceProviderTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function providers_dont_work_by_default()
|
||||||
|
{
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$this->assertIsArray(
|
||||||
|
$this->app->getContainer()->make('flarum.forum.middleware')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function providers_first_register_order_is_correct()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\ServiceProvider())
|
||||||
|
->register(CustomServiceProvider::class)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'overriden_by_custom_provider_register',
|
||||||
|
$this->app->getContainer()->make('flarum.forum.middleware')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function providers_second_register_order_is_correct()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\ServiceProvider())
|
||||||
|
->register(CustomServiceProvider::class)
|
||||||
|
->register(SecondCustomServiceProvider::class)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'overriden_by_second_custom_provider_register',
|
||||||
|
$this->app->getContainer()->make('flarum.forum.middleware')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
*/
|
||||||
|
public function providers_boot_order_is_correct()
|
||||||
|
{
|
||||||
|
$this->extend(
|
||||||
|
(new Extend\ServiceProvider())
|
||||||
|
->register(ThirdCustomProvider::class)
|
||||||
|
->register(CustomServiceProvider::class)
|
||||||
|
->register(SecondCustomServiceProvider::class)
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->app();
|
||||||
|
|
||||||
|
$this->assertEquals(
|
||||||
|
'overriden_by_third_custom_provider_boot',
|
||||||
|
$this->app->getContainer()->make('flarum.forum.middleware')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CustomServiceProvider extends AbstractServiceProvider
|
||||||
|
{
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
// First we override the singleton here.
|
||||||
|
$this->app->extend('flarum.forum.middleware', function () {
|
||||||
|
return 'overriden_by_custom_provider_register';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SecondCustomServiceProvider extends AbstractServiceProvider
|
||||||
|
{
|
||||||
|
public function register()
|
||||||
|
{
|
||||||
|
// Second we check that the singleton was overriden here.
|
||||||
|
$this->app->extend('flarum.forum.middleware', function ($forumRoutes) {
|
||||||
|
return 'overriden_by_second_custom_provider_register';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ThirdCustomProvider extends AbstractServiceProvider
|
||||||
|
{
|
||||||
|
public function boot()
|
||||||
|
{
|
||||||
|
// Third we override one last time here, to make sure this is the final result.
|
||||||
|
$this->app->extend('flarum.forum.middleware', function ($forumRoutes) {
|
||||||
|
return 'overriden_by_third_custom_provider_boot';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user