Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Service Provider Extender #2437

Merged
merged 5 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
40 changes: 40 additions & 0 deletions src/Extend/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
87 changes: 87 additions & 0 deletions tests/integration/extenders/ServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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_order_is_correct()
{
$this->extend(
(new Extend\ServiceProvider())
->register(CustomServiceProvider::class)
->register(SecondCustomServiceProvider::class)
);

$this->app();

$this->assertEquals(
'overriden_by_provider_test__final',
$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_provider_test';
});
}

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_provider_test__final';
});
}
}

class SecondCustomServiceProvider extends AbstractServiceProvider
{
public function register()
{
// Second we check that the singleton was overriden here.
$this->app->extend('flarum.forum.middleware', function ($forumRoutes) {
if ($forumRoutes !== 'overriden_by_provider_test') {
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
throw new ProviderTestException('CustomServiceProvider did not override the singleton.');
}

return $forumRoutes;
});
}
}

class ProviderTestException extends \Exception
{
// ...
}