Skip to content

Commit 5253dcd

Browse files
committed
Initial commit
0 parents  commit 5253dcd

23 files changed

+881
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore
11+
/.editorconfig export-ignore

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
build
3+
composer.lock
4+
docs
5+
vendor
6+
coverage

.scrutinizer.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
filter:
2+
excluded_paths: [tests/*]
3+
4+
checks:
5+
php:
6+
remove_extra_empty_lines: true
7+
remove_php_closing_tag: true
8+
remove_trailing_whitespace: true
9+
fix_use_statements:
10+
remove_unused: true
11+
preserve_multiple: false
12+
preserve_blanklines: true
13+
order_alphabetically: true
14+
fix_php_opening_tag: true
15+
fix_linefeed: true
16+
fix_line_ending: true
17+
fix_identation_4spaces: true
18+
fix_doc_comments: true
19+

.styleci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
preset: laravel
2+
3+
disabled:
4+
- single_class_element_per_statement

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
7+
env:
8+
matrix:
9+
- COMPOSER_FLAGS="--prefer-lowest"
10+
- COMPOSER_FLAGS=""
11+
12+
before_script:
13+
- travis_retry composer self-update
14+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
15+
16+
script:
17+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
18+
19+
after_script:
20+
- php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All notable changes to `laravel-comments` will be documented in this file
4+
5+
## 1.0.0 - 201X-XX-XX
6+
7+
- initial release

CONTRIBUTING.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
Please read and understand the contribution guide before creating an issue or pull request.
6+
7+
## Etiquette
8+
9+
This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10+
held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11+
extremely unfair for them to suffer abuse or anger for their hard work.
12+
13+
Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14+
world that developers are civilized and selfless people.
15+
16+
It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17+
quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18+
19+
## Viability
20+
21+
When requesting or submitting new features, first consider whether it might be useful to others. Open
22+
source projects are used by many developers, who may have entirely different needs to your own. Think about
23+
whether or not your feature is likely to be used by other users of the project.
24+
25+
## Procedure
26+
27+
Before filing an issue:
28+
29+
- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30+
- Check to make sure your feature suggestion isn't already present within the project.
31+
- Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32+
- Check the pull requests tab to ensure that the feature isn't already in progress.
33+
34+
Before submitting a pull request:
35+
36+
- Check the codebase to ensure that your feature doesn't already exist.
37+
- Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38+
39+
## Requirements
40+
41+
If the project maintainer has any additional requirements, you will find them listed here.
42+
43+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
44+
45+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46+
47+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48+
49+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50+
51+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52+
53+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54+
55+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Beyond Code GmbH <hello@beyondco.de>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Add comments to your Laravel application
2+
3+
[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-comments.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-comments)
4+
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-comments/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-comments)
5+
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-comments.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-comments)
6+
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-comments.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-comments)
7+
8+
Add the ability to associate comments to your Laravel Eloquent models. The comments can be approved and nested.
9+
10+
```php
11+
$post = Post::find(1);
12+
13+
$post->comment('This is a comment');
14+
15+
$post->commentAsUser($user, 'This is a comment from someone else');
16+
```
17+
18+
## Installation
19+
20+
You can install the package via composer:
21+
22+
```bash
23+
composer require beyondcode/laravel-comments
24+
```
25+
26+
The package will automatically register itself.
27+
28+
You can publish the migration with:
29+
30+
```bash
31+
php artisan vendor:publish --provider=BeyondCode\Comments\CommentsServiceProvider --tag="migrations"
32+
```
33+
34+
After the migration has been published you can create the media-table by running the migrations:
35+
36+
```bash
37+
php artisan migrate
38+
```
39+
40+
You can publish the config-file with:
41+
42+
```bash
43+
php artisan vendor:publish --provider=BeyondCode\Comments\CommentsServiceProvider --tag="config"
44+
```
45+
46+
## Usage
47+
48+
### Registering Models
49+
50+
To let your models be able to receive comments, add the `HasComments` trait to the model classes.
51+
52+
``` php
53+
namespace App\Models;
54+
55+
use Beyondcode\Comments\HasComments;
56+
use Illuminate\Database\Eloquent\Model;
57+
58+
class Post extends Model
59+
{
60+
use HasComments;
61+
...
62+
}
63+
```
64+
65+
### Creating Comments
66+
67+
To create a comment on your commentable models, you can use the `comment` method. It receives the string of the comment that you want to store.
68+
69+
```php
70+
$post = Post::find(1);
71+
72+
$comment = $post->comment('This is a comment from a user.');
73+
```
74+
75+
The comment method returns the newly created comment class.
76+
77+
Sometimes you also might want to create comments on behalf of other users. You can do this using the `commentAsUser` method and pass in your user model that should get associated
78+
with this comment:
79+
80+
```php
81+
$post = Post::find(1);
82+
83+
$comment = $post->commentAsUser($yourUser, 'This is a comment from someone else.');
84+
```
85+
86+
### Approving Comments
87+
88+
By default, all comments that you create are not approved - this is just a boolean flag called `is_approved` that you can use in your views/controllers to filter out comments that you might not yet want to display.
89+
90+
To approve a single comment, you may use the `approve` method on the Comment model like this:
91+
92+
```php
93+
$post = Post::find(1);
94+
$comment = $post->comments->first();
95+
96+
$comment->approve();
97+
```
98+
99+
### Auto Approve Comments
100+
101+
If you want to automatically approve a comment for a specific user (and optionally model) you can let your User model implement the following interface and method:
102+
103+
```php
104+
namespace App\Models;
105+
106+
use BeyondCode\Comments\Contracts\Commentator;
107+
use Illuminate\Foundation\Auth\User as Authenticatable;
108+
109+
class User extends Authenticatable implements Commentator
110+
{
111+
/**
112+
* Check if a comment for a specific model needs to be approved.
113+
* @param mixed $model
114+
* @return bool
115+
*/
116+
public function needsCommentApproval($model): bool
117+
{
118+
return false;
119+
}
120+
121+
}
122+
```
123+
124+
The `needsCommentApproval` method received the model instance that you want to add a comment to and you can either return `true` to mark the comment as **not** approved, or return `false` to mark the comment as **approved**.
125+
126+
### Retrieving Comments
127+
128+
The models that use the `HasComments` trait have access to it's comments using the `comments` relation:
129+
130+
```php
131+
132+
$post = Post::find(1);
133+
134+
// Retrieve all comments
135+
$comments = $post->comments;
136+
137+
// Retrieve only approved comments
138+
$approved = $post->comments()->approved()->get();
139+
140+
```
141+
142+
### Testing
143+
144+
``` bash
145+
composer test
146+
```
147+
148+
### Changelog
149+
150+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
151+
152+
## Contributing
153+
154+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
155+
156+
### Security
157+
158+
If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.
159+
160+
## Credits
161+
162+
- [Marcel Pociot](https://github.com/mpociot)
163+
- [All Contributors](../../contributors)
164+
165+
## License
166+
167+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"name": "beyondcode/laravel-comments",
3+
"description": "Add comments to your Laravel application",
4+
"keywords": [
5+
"beyondcode",
6+
"comments",
7+
"laravel-comments"
8+
],
9+
"homepage": "https://github.com/beyondcode/laravel-comments",
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Marcel Pociot",
14+
"email": "marcel@beyondco.de",
15+
"homepage": "https://beyondcode.de",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php": "^7.1"
21+
},
22+
"require-dev": {
23+
"orchestra/testbench": "^3.6",
24+
"phpunit/phpunit": "^7.0"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"BeyondCode\\Comments\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"BeyondCode\\Comments\\Tests\\": "tests"
34+
}
35+
},
36+
"scripts": {
37+
"test": "vendor/bin/phpunit",
38+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
39+
40+
},
41+
"config": {
42+
"sort-packages": true
43+
},
44+
"extra": {
45+
"laravel": {
46+
"providers": [
47+
"BeyondCode\\Comments\\CommentsServiceProvider"
48+
]
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)