-
-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
196 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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\Api\Serializer\AbstractSerializer; | ||
use Flarum\Api\Serializer\ForumSerializer; | ||
use Flarum\Extension\Extension; | ||
use Flarum\Foundation\ContainerUtil; | ||
use Flarum\Settings\SettingsRepositoryInterface; | ||
use Illuminate\Contracts\Container\Container; | ||
|
||
class Settings implements ExtenderInterface | ||
{ | ||
private $settings = []; | ||
|
||
/** | ||
* Serialize a setting value to the ForumSerializer attributes. | ||
* | ||
* @param string $attributeName: The attribute name to be used in the ForumSerializer attributes array. | ||
* @param string $key: The key of the setting. | ||
* @param string|callable|null $callback: Optional callback to modify the value before serialization. | ||
* @return $this | ||
*/ | ||
public function serializeToForum(string $attributeName, string $key, $callback = null) | ||
{ | ||
$this->settings[$key] = compact('attributeName', 'callback'); | ||
|
||
return $this; | ||
} | ||
|
||
public function extend(Container $container, Extension $extension = null) | ||
{ | ||
if (! empty($this->settings)) { | ||
AbstractSerializer::addMutator( | ||
ForumSerializer::class, | ||
function () use ($container) { | ||
$settings = $container->make(SettingsRepositoryInterface::class); | ||
$attributes = []; | ||
|
||
foreach ($this->settings as $key => $setting) { | ||
$value = $settings->get($key, null); | ||
|
||
if (isset($setting['callback'])) { | ||
$callback = ContainerUtil::wrapCallback($setting['callback'], $container); | ||
$value = $callback($value); | ||
} | ||
|
||
$attributes[$setting['attributeName']] = $value; | ||
} | ||
|
||
return $attributes; | ||
} | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
<?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\Tests\integration\RetrievesAuthorizedUsers; | ||
use Flarum\Tests\integration\TestCase; | ||
|
||
class SettingsTest extends TestCase | ||
{ | ||
use RetrievesAuthorizedUsers; | ||
|
||
protected function prepDb() | ||
{ | ||
$this->prepareDatabase([ | ||
'users' => [ | ||
$this->adminUser(), | ||
$this->normalUser() | ||
], | ||
'settings' => [ | ||
['key' => 'custom-prefix.custom_setting', 'value' => 'customValue'], | ||
['key' => 'custom-prefix.custom_setting2', 'value' => 'customValue'] | ||
] | ||
]); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function custom_setting_isnt_serialized_by_default() | ||
{ | ||
$this->prepDb(); | ||
|
||
$response = $this->send( | ||
$this->request('GET', '/api', [ | ||
'authenticatedAs' => 1, | ||
]) | ||
); | ||
|
||
$payload = json_decode($response->getBody(), true); | ||
|
||
$this->assertArrayNotHasKey('customPrefix.customSetting', $payload['data']['attributes']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function custom_setting_serialized_if_added() | ||
{ | ||
$this->extend( | ||
(new Extend\Settings()) | ||
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting') | ||
); | ||
|
||
$this->prepDb(); | ||
|
||
$response = $this->send( | ||
$this->request('GET', '/api', [ | ||
'authenticatedAs' => 1, | ||
]) | ||
); | ||
|
||
$payload = json_decode($response->getBody(), true); | ||
|
||
$this->assertArrayHasKey('customPrefix.customSetting', $payload['data']['attributes']); | ||
$this->assertEquals('customValue', $payload['data']['attributes']['customPrefix.customSetting']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function custom_setting_callback_works_if_added() | ||
{ | ||
$this->extend( | ||
(new Extend\Settings()) | ||
->serializeToForum('customPrefix.customSetting', 'custom-prefix.custom_setting', function ($value) { | ||
return $value.'Modified'; | ||
}) | ||
); | ||
|
||
$this->prepDb(); | ||
|
||
$response = $this->send( | ||
$this->request('GET', '/api', [ | ||
'authenticatedAs' => 1, | ||
]) | ||
); | ||
|
||
$payload = json_decode($response->getBody(), true); | ||
|
||
$this->assertArrayHasKey('customPrefix.customSetting', $payload['data']['attributes']); | ||
$this->assertEquals('customValueModified', $payload['data']['attributes']['customPrefix.customSetting']); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function custom_setting_callback_works_with_invokable_class() | ||
{ | ||
$this->extend( | ||
(new Extend\Settings()) | ||
->serializeToForum('customPrefix.customSetting2', 'custom-prefix.custom_setting2', CustomInvokableClass::class) | ||
); | ||
|
||
$this->prepDb(); | ||
|
||
$response = $this->send( | ||
$this->request('GET', '/api', [ | ||
'authenticatedAs' => 1, | ||
]) | ||
); | ||
|
||
$payload = json_decode($response->getBody(), true); | ||
|
||
$this->assertArrayHasKey('customPrefix.customSetting2', $payload['data']['attributes']); | ||
$this->assertEquals('customValueModifiedByInvokable', $payload['data']['attributes']['customPrefix.customSetting2']); | ||
} | ||
} | ||
|
||
class CustomInvokableClass | ||
{ | ||
public function __invoke($value) | ||
{ | ||
return $value.'ModifiedByInvokable'; | ||
} | ||
} |