Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
dff456ea
Unverified
Commit
dff456ea
authored
Jul 12, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Triggering and using setting is creating/updating hooks
parent
b0c4cdd3
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
235 additions
and
5 deletions
+235
-5
SettingIsCreating.php
Modules/Setting/Events/SettingIsCreating.php
+49
-0
SettingIsUpdating.php
Modules/Setting/Events/SettingIsUpdating.php
+64
-0
EloquentSettingRepository.php
...tting/Repositories/Eloquent/EloquentSettingRepository.php
+9
-4
EloquentSettingRepositoryTest.php
Modules/Setting/Tests/EloquentSettingRepositoryTest.php
+107
-1
changelog.yml
Modules/Setting/changelog.yml
+6
-0
No files found.
Modules/Setting/Events/SettingIsCreating.php
0 → 100644
View file @
dff456ea
<?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
;
}
}
Modules/Setting/Events/SettingIsUpdating.php
0 → 100644
View file @
dff456ea
<?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
;
}
}
Modules/Setting/Repositories/Eloquent/EloquentSettingRepository.php
View file @
dff456ea
...
@@ -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
();
...
...
Modules/Setting/Tests/EloquentSettingRepositoryTest.php
View file @
dff456ea
...
@@ -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
);
}
}
}
Modules/Setting/changelog.yml
View file @
dff456ea
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment