Skip to content

Commit 9605cd8

Browse files
authored
Merge pull request #41 from beyondcode/updates
1.6.0
2 parents 972ff76 + 30d9556 commit 9605cd8

15 files changed

+245
-71
lines changed

README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ php artisan vendor:publish --provider="BeyondCode\Comments\CommentsServiceProvid
4747

4848
To let your models be able to receive comments, add the `HasComments` trait to the model classes.
4949

50-
``` php
50+
```php
5151
namespace App\Models;
5252

5353
use Illuminate\Database\Eloquent\Model;
@@ -146,10 +146,18 @@ $comment = BeyondCode\Comments\Comment::first();
146146
$comment->commentAsUser($user, "Hey there!");
147147
```
148148

149+
#### Deleting Replies
150+
151+
When you delete a comment, you may optionally want to delete all its nested comments (replies). To optionally enable this feature, set the `delete_replies_along_comments` config property in the `config/comments.php` file to `true`.
152+
153+
### Events
154+
155+
When a new comment is added the `BeyondCode\Comments\Events\CommentAdded` event will be dispatched.
156+
When a comment is deleted the `BeyondCode\Comments\Events\CommentDeleted` event will be dispatched.
149157

150158
### Testing
151159

152-
``` bash
160+
```bash
153161
composer test
154162
```
155163

@@ -167,8 +175,8 @@ If you discover any security related issues, please email [email protected] ins
167175

168176
## Credits
169177

170-
- [Marcel Pociot](https://github.com/mpociot)
171-
- [All Contributors](../../contributors)
178+
- [Marcel Pociot](https://github.com/mpociot)
179+
- [All Contributors](../../contributors)
172180

173181
## License
174182

config/config.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@
1515
*/
1616
'user_model' => null,
1717

18-
];
18+
/**
19+
* Determines if replies will be deleted when comments are deleted
20+
*/
21+
'delete_replies_along_comments' => false,
22+
];

phpunit.xml.dist

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
verbose="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
15+
<coverage>
16+
<include>
17+
<directory suffix=".php">src/</directory>
18+
</include>
19+
<report>
20+
<clover outputFile="build/logs/clover.xml" />
21+
<html outputDirectory="build/coverage" />
22+
<text outputFile="build/coverage.txt" />
23+
</report>
24+
</coverage>
1225
<testsuites>
1326
<testsuite name="BeyondCode Test Suite">
1427
<directory>tests</directory>
1528
</testsuite>
1629
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
2230
<logging>
23-
<log type="junit" target="build/report.junit.xml"/>
24-
<log type="coverage-html" target="build/coverage" />
25-
<log type="coverage-text" target="build/coverage.txt"/>
26-
<log type="coverage-clover" target="build/logs/clover.xml"/>
31+
<junit outputFile="build/report.junit.xml" />
2732
</logging>
2833
</phpunit>

phpunit.xml.dist.bak

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
bootstrap="vendor/autoload.php"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="true"
7+
verbose="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false">
13+
<testsuites>
14+
<testsuite name="BeyondCode Test Suite">
15+
<directory>tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
<filter>
19+
<whitelist>
20+
<directory suffix=".php">src/</directory>
21+
</whitelist>
22+
</filter>
23+
<logging>
24+
<log type="junit" target="build/report.junit.xml" />
25+
<log type="coverage-html" target="build/coverage" />
26+
<log type="coverage-text" target="build/coverage.txt" />
27+
<log type="coverage-clover" target="build/logs/clover.xml" />
28+
</logging>
29+
</phpunit>

src/Comment.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace BeyondCode\Comments;
44

5+
use BeyondCode\Comments\Events\CommentAdded;
6+
use BeyondCode\Comments\Events\CommentDeleted;
7+
use BeyondCode\Comments\Traits\HasComments;
58
use Exception;
69
use Illuminate\Database\Eloquent\Model;
7-
use BeyondCode\Comments\Traits\HasComments;
810

911
class Comment extends Model
1012
{
@@ -13,13 +15,32 @@ class Comment extends Model
1315
protected $fillable = [
1416
'comment',
1517
'user_id',
16-
'is_approved'
18+
'is_approved',
1719
];
1820

1921
protected $casts = [
20-
'is_approved' => 'boolean'
22+
'is_approved' => 'boolean',
2123
];
2224

25+
public static function boot(): void
26+
{
27+
parent::boot();
28+
29+
static::deleting(function (self $model) {
30+
if (config('comments.delete_replies_along_comments')) {
31+
$model->comments()->delete();
32+
}
33+
});
34+
35+
static::deleted(function (self $model) {
36+
CommentDeleted::dispatch($model);
37+
});
38+
39+
static::created(function (self $model) {
40+
CommentAdded::dispatch($model);
41+
});
42+
}
43+
2344
public function scopeApproved($query)
2445
{
2546
return $query->where('is_approved', true);
@@ -43,7 +64,7 @@ public function approve()
4364

4465
return $this;
4566
}
46-
67+
4768
public function disapprove()
4869
{
4970
$this->update([
@@ -59,11 +80,10 @@ protected function getAuthModelName()
5980
return config('comments.user_model');
6081
}
6182

62-
if (!is_null(config('auth.providers.users.model'))) {
83+
if (! is_null(config('auth.providers.users.model'))) {
6384
return config('auth.providers.users.model');
6485
}
6586

6687
throw new Exception('Could not determine the commentator model name.');
6788
}
68-
6989
}

src/CommentsServiceProvider.php

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public function boot()
1616
__DIR__.'/../config/config.php' => config_path('comments.php'),
1717
], 'config');
1818

19-
2019
if (! class_exists('CreateCommentsTable')) {
2120
$this->publishes([
2221
__DIR__.'/../database/migrations/create_comments_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_comments_table.php'),

src/Contracts/Commentator.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
namespace BeyondCode\Comments\Contracts;
44

5-
65
interface Commentator
76
{
87
/**
98
* Check if a comment for a specific model needs to be approved.
10-
* @param mixed $model
11-
* @return bool
9+
*
10+
* @param mixed $model
1211
*/
1312
public function needsCommentApproval($model): bool;
14-
15-
}
13+
}

src/Events/CommentAdded.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace BeyondCode\Comments\Events;
4+
5+
use BeyondCode\Comments\Comment;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class CommentAdded implements ShouldDispatchAfterCommit
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
/**
16+
* Create a new event instance.
17+
*/
18+
public function __construct(
19+
public Comment $comment,
20+
) {
21+
}
22+
}

src/Events/CommentDeleted.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace BeyondCode\Comments\Events;
4+
5+
use BeyondCode\Comments\Comment;
6+
use Illuminate\Broadcasting\InteractsWithSockets;
7+
use Illuminate\Contracts\Events\ShouldDispatchAfterCommit;
8+
use Illuminate\Foundation\Events\Dispatchable;
9+
use Illuminate\Queue\SerializesModels;
10+
11+
class CommentDeleted implements ShouldDispatchAfterCommit
12+
{
13+
use Dispatchable, InteractsWithSockets, SerializesModels;
14+
15+
/**
16+
* Create a new event instance.
17+
*/
18+
public function __construct(
19+
public Comment $comment,
20+
) {
21+
}
22+
}

src/Traits/CanComment.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
namespace BeyondCode\Comments\Traits;
44

5-
65
trait CanComment
76
{
87
/**
98
* Check if a comment for a specific model needs to be approved.
10-
* @param mixed $model
11-
* @return bool
9+
*
10+
* @param mixed $model
1211
*/
1312
public function needsCommentApproval($model): bool
1413
{
1514
return true;
1615
}
17-
}
16+
}

src/Traits/HasComments.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace BeyondCode\Comments\Traits;
44

5-
6-
use Illuminate\Database\Eloquent\Model;
75
use BeyondCode\Comments\Contracts\Commentator;
6+
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Database\Eloquent\Relations\MorphMany;
98

109
trait HasComments
@@ -22,7 +21,6 @@ public function comments()
2221
/**
2322
* Attach a comment to this model.
2423
*
25-
* @param string $comment
2624
* @return \Illuminate\Database\Eloquent\Model
2725
*/
2826
public function comment(string $comment)
@@ -33,8 +31,6 @@ public function comment(string $comment)
3331
/**
3432
* Attach a comment to this model as a specific user.
3533
*
36-
* @param Model|null $user
37-
* @param string $comment
3834
* @return \Illuminate\Database\Eloquent\Model
3935
*/
4036
public function commentAsUser(?Model $user, string $comment)
@@ -46,10 +42,9 @@ public function commentAsUser(?Model $user, string $comment)
4642
'is_approved' => ($user instanceof Commentator) ? ! $user->needsCommentApproval($this) : false,
4743
'user_id' => is_null($user) ? null : $user->getKey(),
4844
'commentable_id' => $this->getKey(),
49-
'commentable_type' => get_class(),
45+
'commentable_type' => get_class($this),
5046
]);
5147

5248
return $this->comments()->save($comment);
5349
}
54-
55-
}
50+
}

0 commit comments

Comments
 (0)