diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100755 index 0000000..5df182f --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Slynova + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100755 index 0000000..0d61a2e --- /dev/null +++ b/README.md @@ -0,0 +1,65 @@ +![Slynova](https://cloud.githubusercontent.com/assets/2793951/8206037/35841f80-14f6-11e5-8538-b378cd632d28.png) + +# Laravel-Commentable + +[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://tldrlegal.com/license/mit-license) +[![Total Downloads](https://img.shields.io/packagist/dt/slynova/laravel-commentable.svg?style=flat-square)](https://packagist.org/packages/slynova/laravel-commentable) + +[![SensioLabsInsight](https://insight.sensiolabs.com/projects/8d9f7ba6-6801-486f-aa04-570855860d57/big.png)](https://insight.sensiolabs.com/projects/8d9f7ba6-6801-486f-aa04-570855860d57) + +Laravel Commentable adds polymorphic threaded comments to Laravel 5.1 and above. + +This package use Nested Set pattern with [Baum](https://github.com/etrepat/baum).
+[More information about Nested Set](http://en.wikipedia.org/wiki/Nested_set_model) + +# Table of Contents + +* [Requirements](#requirements) +* [Getting Started](#getting-started) +* [Example](#example) +* [Change Logs](#change-logs) +* [Contribution Guidelines](#contribution-guidelines) + +# Requirements + +* As Laravel 5.1 require PHP 5.5.9+, we required the same version. + +# Getting Started + +1. Require the package with [Composer](https://getcomposer.org). + ```shell + $ composer require slynova/laravel-commentable + ``` + +2. Add the package to your application service providers in `config/app.php`. + ```php + 'providers' => [ + + Illuminate\Foundation\Providers\ArtisanServiceProvider::class, + Illuminate\Auth\AuthServiceProvider::class, + ... + Slynova\Commentable\ServiceProvider::class, + + ], + ``` + +3. Publish the package's migrations to your application and migrate. + ```shell + $ php artisan vendor:publish --provider="Slynova\Commentable\ServiceProvider" --tag="migrations" + $ php artisan migrate + ``` + +# Example + +You can find an usage example of this package in the [laravel-commentable-example](https://github.com/Slynova-Org/laravel-commentable-example) repository. + +# Change Logs + +Nothing has been changed from the first release. + +# Contribution Guidelines + +Support follows PSR-2 PHP coding standards, and semantic versioning. + +Please report any issue you find in the issues page. +Pull requests are welcome. diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..0f5508a --- /dev/null +++ b/composer.json @@ -0,0 +1,27 @@ +{ + "name": "slynova/laravel-commentable", + "description": "Polymorphic threaded comments for Laravel", + "keywords": ["laravel", "commentable", "comment", "threaded", "nested set"], + "license": "MIT", + "support": { + "issues": "https://github.com/slynova-org/laravel-commentable/issues", + "source": "https://github.com/slynova-org/laravel-commentable" + }, + "authors": [ + { + "name": "Romain Lanz", + "homepage": "https://github.com/RomainLanz", + "email": "romain.lanz@slynova.ch", + "role": "Developer" + } + ], + "require": { + "php": ">=5.5.9", + "baum/baum": "~1.1" + }, + "autoload": { + "psr-4": { + "Slynova\\Commentable\\": "src/Commentable" + } + } +} diff --git a/src/Commentable/Models/Comment.php b/src/Commentable/Models/Comment.php new file mode 100755 index 0000000..9219984 --- /dev/null +++ b/src/Commentable/Models/Comment.php @@ -0,0 +1,79 @@ +children()->count() > 0; + } + + /** + * Get all of comment's children. + * + * @param array $columns + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getChildren($columns = ['*']) + { + return $this->children()->get($columns); + } + + /** + * Get all of the owning commentable models. + * + * @return Illuminate\Database\Eloquent\Relations\MorphTo + */ + public function commentable() + { + return $this->morphTo(); + } + + /** + * Get the user that creates the comment. + * + * @param $configKey string + * @return Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function user($configKey = 'auth.providers.users.model') + { + return $this->belongsTo(config()->get($configKey)); + } +} diff --git a/src/Commentable/ServiceProvider.php b/src/Commentable/ServiceProvider.php new file mode 100755 index 0000000..434209a --- /dev/null +++ b/src/Commentable/ServiceProvider.php @@ -0,0 +1,52 @@ +publishes([ + __DIR__.'/../migrations/' => database_path('migrations'), + ], 'migrations'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app->register(BaumServiceProvider::class); + } +} diff --git a/src/Commentable/Traits/Commentable.php b/src/Commentable/Traits/Commentable.php new file mode 100755 index 0000000..c484e2f --- /dev/null +++ b/src/Commentable/Traits/Commentable.php @@ -0,0 +1,33 @@ +morphMany(Comment::class, 'commentable'); + } +} diff --git a/src/migrations/2015_11_06_000000_create_comments_table.php b/src/migrations/2015_11_06_000000_create_comments_table.php new file mode 100755 index 0000000..65a5bef --- /dev/null +++ b/src/migrations/2015_11_06_000000_create_comments_table.php @@ -0,0 +1,63 @@ +increments('id'); + $table->timestamps(); + + $table->string('title')->nullable(); + $table->text('body'); + + $table->integer('parent_id')->nullable(); + $table->integer('lft')->nullable(); + $table->integer('rgt')->nullable(); + $table->integer('depth')->nullable(); + + $table->integer('commentable_id')->unsigned()->nullable(); + $table->string('commentable_type')->nullable(); + $table->integer('user_id')->unsigned(); + + $table->index('user_id'); + $table->index('commentable_id'); + $table->index('commentable_type'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('comments'); + } +}