Triggering and using setting is creating/updating hooks

parent b0c4cdd3
<?php
namespace Modules\Setting\Events;
class SettingIsCreating
{
private $settingName;
private $settingValues;
private $original;
public function __construct($settingName, $settingValues)
{
$this->settingName = $settingName;
$this->settingValues = $settingValues;
$this->original = $settingValues;
}
/**
* @return mixed
*/
public function getSettingName()
{
return $this->settingName;
}
/**
* @return mixed
*/
public function getSettingValues()
{
return $this->settingValues;
}
/**
* @param mixed $settingValues
*/
public function setSettingValues($settingValues)
{
$this->settingValues = $settingValues;
}
/**
* @return mixed
*/
public function getOriginal()
{
return $this->original;
}
}
<?php
namespace Modules\Setting\Events;
use Modules\Setting\Entities\Setting;
class SettingIsUpdating
{
private $settingName;
private $settingValues;
private $original;
/**
* @var Setting
*/
private $setting;
public function __construct(Setting $setting, $settingName, $settingValues)
{
$this->settingName = $settingName;
$this->settingValues = $settingValues;
$this->original = $settingValues;
$this->setting = $setting;
}
/**
* @return mixed
*/
public function getSettingName()
{
return $this->settingName;
}
/**
* @return mixed
*/
public function getSettingValues()
{
return $this->settingValues;
}
/**
* @param mixed $settingValues
*/
public function setSettingValues($settingValues)
{
$this->settingValues = $settingValues;
}
/**
* @return mixed
*/
public function getOriginal()
{
return $this->original;
}
/**
* @return Setting
*/
public function getSetting()
{
return $this->setting;
}
}
...@@ -5,6 +5,8 @@ namespace Modules\Setting\Repositories\Eloquent; ...@@ -5,6 +5,8 @@ namespace Modules\Setting\Repositories\Eloquent;
use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\Config;
use Modules\Core\Repositories\Eloquent\EloquentBaseRepository; use Modules\Core\Repositories\Eloquent\EloquentBaseRepository;
use Modules\Setting\Entities\Setting; use Modules\Setting\Entities\Setting;
use Modules\Setting\Events\SettingIsCreating;
use Modules\Setting\Events\SettingIsUpdating;
use Modules\Setting\Events\SettingWasCreated; use Modules\Setting\Events\SettingWasCreated;
use Modules\Setting\Events\SettingWasUpdated; use Modules\Setting\Events\SettingWasUpdated;
use Modules\Setting\Repositories\SettingRepository; use Modules\Setting\Repositories\SettingRepository;
...@@ -82,15 +84,17 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin ...@@ -82,15 +84,17 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
*/ */
private function createForName($settingName, $settingValues) private function createForName($settingName, $settingValues)
{ {
event($event = new SettingIsCreating($settingName, $settingValues));
$setting = new $this->model(); $setting = new $this->model();
$setting->name = $settingName; $setting->name = $settingName;
if ($this->isTranslatableSetting($settingName)) { if ($this->isTranslatableSetting($settingName)) {
$setting->isTranslatable = true; $setting->isTranslatable = true;
$this->setTranslatedAttributes($settingValues, $setting); $this->setTranslatedAttributes($event->getSettingValues(), $setting);
} else { } else {
$setting->isTranslatable = false; $setting->isTranslatable = false;
$setting->plainValue = $this->getSettingPlainValue($settingValues); $setting->plainValue = $this->getSettingPlainValue($event->getSettingValues());
} }
$setting->save(); $setting->save();
...@@ -108,11 +112,12 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin ...@@ -108,11 +112,12 @@ class EloquentSettingRepository extends EloquentBaseRepository implements Settin
private function updateSetting($setting, $settingValues) private function updateSetting($setting, $settingValues)
{ {
$name = $setting->name; $name = $setting->name;
event($event = new SettingIsUpdating($setting, $name, $settingValues));
if ($this->isTranslatableSetting($name)) { if ($this->isTranslatableSetting($name)) {
$this->setTranslatedAttributes($settingValues, $setting); $this->setTranslatedAttributes($event->getSettingValues(), $setting);
} else { } else {
$setting->plainValue = $this->getSettingPlainValue($settingValues); $setting->plainValue = $this->getSettingPlainValue($event->getSettingValues());
} }
$setting->save(); $setting->save();
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
namespace Modules\Setting\Tests; namespace Modules\Setting\Tests;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Modules\Setting\Events\SettingIsCreating;
use Modules\Setting\Events\SettingIsUpdating;
use Modules\Setting\Events\SettingWasCreated; use Modules\Setting\Events\SettingWasCreated;
use Modules\Setting\Events\SettingWasUpdated; use Modules\Setting\Events\SettingWasUpdated;
...@@ -136,7 +138,55 @@ class EloquentSettingRepositoryTest extends BaseSettingTest ...@@ -136,7 +138,55 @@ class EloquentSettingRepositoryTest extends BaseSettingTest
} }
/** @test */ /** @test */
public function it_triggers_event_when_setting_was_update() public function it_triggers_event_when_setting_is_creating()
{
Event::fake();
$data = [
'core::template' => 'asgard',
'core::site-name' => [
'en' => 'AsgardCMS_en',
'fr' => 'AsgardCMS_fr',
],
];
$this->settingRepository->createOrUpdate($data);
Event::assertDispatched(SettingIsCreating::class, function (SettingIsCreating $e) {
return $e->getSettingName() === 'core::template' && $e->getSettingValues() === 'asgard';
});
}
/** @test */
public function it_can_change_data_when_it_is_creating_event()
{
Event::listen(SettingIsCreating::class, function (SettingIsCreating $event) {
if ($event->getSettingName() === 'core::template') {
$event->setSettingValues('my-template');
}
if ($event->getSettingName() === 'core::site-name') {
$event->setSettingValues([
'en' => 'English AsgardCMS'
]);
}
});
$data = [
'core::template' => 'asgard',
'blog::posts' => 10,
'core::site-name' => [
'en' => 'AsgardCMS_en',
'fr' => 'AsgardCMS_fr',
],
];
$this->settingRepository->createOrUpdate($data);
$this->assertEquals('my-template', $this->settingRepository->findByName('core::template')->plainValue);
$this->assertEquals(10, $this->settingRepository->findByName('blog::posts')->plainValue);
$this->assertEquals('English AsgardCMS', $this->settingRepository->findByName('core::site-name')->translate('en')->value);
}
/** @test */
public function it_triggers_event_when_setting_was_updated()
{ {
Event::fake(); Event::fake();
...@@ -154,4 +204,60 @@ class EloquentSettingRepositoryTest extends BaseSettingTest ...@@ -154,4 +204,60 @@ class EloquentSettingRepositoryTest extends BaseSettingTest
return $e->setting->name === 'core::template'; return $e->setting->name === 'core::template';
}); });
} }
/** @test */
public function it_triggers_event_when_setting_is_updating()
{
Event::fake();
$data = [
'core::template' => 'asgard',
'core::site-name' => [
'en' => 'AsgardCMS_en',
'fr' => 'AsgardCMS_fr',
],
];
$this->settingRepository->createOrUpdate($data);
$this->settingRepository->createOrUpdate(['core::template' => 'flatly']);
Event::assertDispatched(SettingIsUpdating::class, function ($e) {
return $e->getSetting()->name === 'core::template';
});
}
/** @test */
public function it_can_change_date_when_updating_setting()
{
Event::listen(SettingIsUpdating::class, function (SettingIsUpdating $event) {
if ($event->getSettingName() === 'core::template') {
$event->setSettingValues('my-template');
}
if ($event->getSettingName() === 'core::site-name') {
$event->setSettingValues([
'en' => 'English AsgardCMS'
]);
}
});
$data = [
'core::template' => 'asgard',
'blog::posts' => 10,
'core::site-name' => [
'en' => 'AsgardCMS_en',
'fr' => 'AsgardCMS_fr',
],
];
$this->settingRepository->createOrUpdate($data);
$this->settingRepository->createOrUpdate([
'core::template' => 'flatly',
'core::site-name' => [
'en' => 'The AsgardCMS_en',
'fr' => 'The AsgardCMS_fr',
],
]);
$this->assertEquals('my-template', $this->settingRepository->findByName('core::template')->plainValue);
$this->assertEquals(10, $this->settingRepository->findByName('blog::posts')->plainValue);
$this->assertEquals('English AsgardCMS', $this->settingRepository->findByName('core::site-name')->translate('en')->value);
}
} }
url: https://github.com/AsgardCms/Platform url: https://github.com/AsgardCms/Platform
versions: versions:
"2.5.0@unreleased":
changed:
- Normalise the setting was created event
- Normalise the setting was updated event
- Trigger <code>SettingIsCreating</code> hook
- Trigger <code>SettingIsUpdating</code> hook
"2.0.0": "2.0.0":
added: added:
- Laravel 5.2 compatibility - Laravel 5.2 compatibility
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment