Initial commit

This commit is contained in:
Marcel Pociot
2018-09-03 13:23:13 +02:00
commit 5253dcdf29
23 changed files with 881 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
<?php
namespace BeyondCode\Comments;
use Exception;
use Illuminate\Database\Eloquent\Model;
use BeyondCode\Comments\Traits\HasComments;
class Comment extends Model
{
use HasComments;
protected $fillable = [
'comment',
'user_id',
'is_approved'
];
protected $casts = [
'is_approved' => 'boolean'
];
public function scopeApproved($query)
{
return $query->where('is_approved', true);
}
public function commentable()
{
return $this->morphTo();
}
public function commentator()
{
return $this->belongsTo($this->getAuthModelName(), 'user_id');
}
public function approve()
{
$this->update([
'is_approved' => true,
]);
return $this;
}
protected function getAuthModelName()
{
if (config('comments.user_model')) {
return config('comments.user_model');
}
if (!is_null(config('auth.providers.users.model'))) {
return config('auth.providers.users.model');
}
throw new Exception('Could not determine the commentator model name.');
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace BeyondCode\Comments;
use Illuminate\Support\ServiceProvider;
class CommentsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('comments.php'),
], 'config');
if (! class_exists('CreateCommentsTable')) {
$this->publishes([
__DIR__.'/../database/migrations/create_comments_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_comments_table.php'),
], 'migrations');
}
}
}
/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'comments');
}
}
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace BeyondCode\Comments\Contracts;
interface Commentator
{
/**
* Check if a comment for a specific model needs to be approved.
* @param mixed $model
* @return bool
*/
public function needsCommentApproval($model): bool;
}
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace BeyondCode\Comments\Traits;
trait CanComment
{
/**
* Check if a comment for a specific model needs to be approved.
* @param mixed $model
* @return bool
*/
public function needsCommentApproval($model): bool
{
return true;
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace BeyondCode\Comments\Traits;
use Illuminate\Database\Eloquent\Model;
use BeyondCode\Comments\Contracts\Commentator;
use Illuminate\Database\Eloquent\Relations\MorphMany;
trait HasComments
{
/**
* Return all comments for this model.
*
* @return MorphMany
*/
public function comments()
{
return $this->morphMany(config('comments.comment_class'), 'commentable');
}
/**
* Attach a comment to this model.
*
* @param string $comment
* @return \Illuminate\Database\Eloquent\Model
*/
public function comment(string $comment)
{
return $this->commentAsUser(auth()->user(), $comment);
}
/**
* Attach a comment to this model as a specific user.
*
* @param Model|null $user
* @param string $comment
* @return \Illuminate\Database\Eloquent\Model
*/
public function commentAsUser(?Model $user, string $comment)
{
$commentClass = config('comments.comment_class');
$comment = new $commentClass([
'comment' => $comment,
'is_approved' => ($user instanceof Commentator) ? ! $user->needsCommentApproval($this) : false,
'user_id' => is_null($user) ? null : $user->getKey(),
'commentable_id' => $this->getKey(),
'commentable_type' => get_class(),
]);
return $this->comments()->save($comment);
}
}