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
3997dc5f
Unverified
Commit
3997dc5f
authored
Jul 12, 2017
by
Nicolas Widart
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Creating and triggering event hooks when tags are creating and updating
parent
ef6948a0
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
107 additions
and
2 deletions
+107
-2
TagIsCreating.php
Modules/Tag/Events/TagIsCreating.php
+10
-0
TagIsUpdating.php
Modules/Tag/Events/TagIsUpdating.php
+29
-0
EloquentTagRepository.php
Modules/Tag/Repositories/Eloquent/EloquentTagRepository.php
+6
-2
EloquentTagRepositoryTest.php
Modules/Tag/Tests/Integration/EloquentTagRepositoryTest.php
+57
-0
changelog.yml
Modules/Tag/changelog.yml
+5
-0
No files found.
Modules/Tag/Events/TagIsCreating.php
0 → 100644
View file @
3997dc5f
<?php
namespace
Modules\Tag\Events
;
use
Modules\Core\Abstracts\AbstractEntityHook
;
use
Modules\Core\Contracts\EntityIsChanging
;
class
TagIsCreating
extends
AbstractEntityHook
implements
EntityIsChanging
{
}
Modules/Tag/Events/TagIsUpdating.php
0 → 100644
View file @
3997dc5f
<?php
namespace
Modules\Tag\Events
;
use
Modules\Core\Abstracts\AbstractEntityHook
;
use
Modules\Core\Contracts\EntityIsChanging
;
use
Modules\Tag\Entities\Tag
;
class
TagIsUpdating
extends
AbstractEntityHook
implements
EntityIsChanging
{
/**
* @var Tag
*/
private
$tag
;
public
function
__construct
(
Tag
$tag
,
$attributes
)
{
$this
->
tag
=
$tag
;
parent
::
__construct
(
$attributes
);
}
/**
* @return Tag
*/
public
function
getTag
()
{
return
$this
->
tag
;
}
}
Modules/Tag/Repositories/Eloquent/EloquentTagRepository.php
View file @
3997dc5f
...
...
@@ -3,6 +3,8 @@
namespace
Modules\Tag\Repositories\Eloquent
;
use
Modules\Core\Repositories\Eloquent\EloquentBaseRepository
;
use
Modules\Tag\Events\TagIsCreating
;
use
Modules\Tag\Events\TagIsUpdating
;
use
Modules\Tag\Events\TagWasCreated
;
use
Modules\Tag\Events\TagWasUpdated
;
use
Modules\Tag\Repositories\TagRepository
;
...
...
@@ -21,7 +23,8 @@ class EloquentTagRepository extends EloquentBaseRepository implements TagReposit
public
function
create
(
$data
)
{
$tag
=
$this
->
model
->
create
(
$data
);
event
(
$event
=
new
TagIsCreating
(
$data
));
$tag
=
$this
->
model
->
create
(
$event
->
getAttributes
());
event
(
new
TagWasCreated
(
$tag
));
...
...
@@ -30,7 +33,8 @@ class EloquentTagRepository extends EloquentBaseRepository implements TagReposit
public
function
update
(
$tag
,
$data
)
{
$tag
->
update
(
$data
);
event
(
$event
=
new
TagIsUpdating
(
$tag
,
$data
));
$tag
->
update
(
$event
->
getAttributes
());
event
(
new
TagWasUpdated
(
$tag
));
...
...
Modules/Tag/Tests/Integration/EloquentTagRepositoryTest.php
View file @
3997dc5f
...
...
@@ -3,6 +3,8 @@
namespace
Modules\Tag\Tests\Integration
;
use
Illuminate\Support\Facades\Event
;
use
Modules\Tag\Events\TagIsCreating
;
use
Modules\Tag\Events\TagIsUpdating
;
use
Modules\Tag\Events\TagWasCreated
;
use
Modules\Tag\Events\TagWasUpdated
;
use
Modules\Tag\Repositories\TagRepository
;
...
...
@@ -68,6 +70,42 @@ class EloquentTagRepositoryTest extends BaseTestCase
});
}
/** @test */
public
function
it_triggers_event_when_tag_is_creating
()
{
Event
::
fake
();
$tag
=
$this
->
tag
->
create
([
'namespace'
=>
'asgardcms/media'
,
'en'
=>
[
'slug'
=>
'media-tag'
,
'name'
=>
'media tag'
,
],
]);
Event
::
assertDispatched
(
TagIsCreating
::
class
,
function
(
$e
)
use
(
$tag
)
{
return
$e
->
getAttribute
(
'namespace'
)
===
$tag
->
namespace
;
});
}
/** @test */
public
function
it_can_change_data_when_it_is_creating_event
()
{
Event
::
listen
(
TagIsCreating
::
class
,
function
(
TagIsCreating
$event
)
{
$event
->
setAttributes
([
'en'
=>
[
'name'
=>
'MEDIA TAG'
]]);
});
$tag
=
$this
->
tag
->
create
([
'namespace'
=>
'asgardcms/media'
,
'en'
=>
[
'slug'
=>
'media-tag'
,
'name'
=>
'media tag'
,
],
]);
$this
->
assertEquals
(
'MEDIA TAG'
,
$tag
->
translate
(
'en'
)
->
name
);
}
/** @test */
public
function
it_triggers_event_when_tag_was_updated
()
{
...
...
@@ -86,4 +124,23 @@ class EloquentTagRepositoryTest extends BaseTestCase
return
$e
->
tag
->
id
===
$tag
->
id
;
});
}
/** @test */
public
function
it_can_change_data_when_it_is_updating_event
()
{
Event
::
listen
(
TagIsUpdating
::
class
,
function
(
TagIsUpdating
$event
)
{
$event
->
setAttributes
([
'en'
=>
[
'name'
=>
'MEDIA TAG'
]]);
});
$tag
=
$this
->
tag
->
create
([
'namespace'
=>
'asgardcms/media'
,
'en'
=>
[
'slug'
=>
'media-tag'
,
'name'
=>
'media tag'
,
],
]);
$this
->
tag
->
update
(
$tag
,
[]);
$this
->
assertEquals
(
'MEDIA TAG'
,
$tag
->
translate
(
'en'
)
->
name
);
}
}
Modules/Tag/changelog.yml
View file @
3997dc5f
url
:
https://github.com/AsgardCms/Platform
versions
:
"
2.5.0"
:
added
:
-
Create and trigger events when tags are created and updated
-
Trigger event before a tag is created (<code>TagIsCreating</code>) allow data to be changed
-
Trigger event before a tag is updated (<code>TagIsUpdating</code>) allow data to be changed
"
2.1.0"
:
changed
:
-
Fixed tags not being removed probably on update & delete
...
...
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