diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a7c44dd --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..99a9952 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,13 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.github export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/phpunit.xml.dist export-ignore +/tests export-ignore +/.editorconfig export-ignore +/.php_cs.dist export-ignore +/psalm.xml export-ignore +/psalm.xml.dist export-ignore diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..b4ae1c4 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,55 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +Please read and understand the contribution guide before creating an issue or pull request. + +## Etiquette + +This project is open source, and as such, the maintainers give their free time to build and maintain the source code +held within. They make the code freely available in the hope that it will be of use to other developers. It would be +extremely unfair for them to suffer abuse or anger for their hard work. + +Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the +world that developers are civilized and selfless people. + +It's the duty of the maintainer to ensure that all submissions to the project are of sufficient +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. + +## Viability + +When requesting or submitting new features, first consider whether it might be useful to others. Open +source projects are used by many developers, who may have entirely different needs to your own. Think about +whether or not your feature is likely to be used by other users of the project. + +## Procedure + +Before filing an issue: + +- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. +- Check to make sure your feature suggestion isn't already present within the project. +- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. +- Check the pull requests tab to ensure that the feature isn't already in progress. + +Before submitting a pull request: + +- Check the codebase to ensure that your feature doesn't already exist. +- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. + +## Requirements + +If the project maintainer has any additional requirements, you will find them listed here. + +- **[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). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **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. + +**Happy coding**! diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..6925ba6 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: maize-tech diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..f523c8a --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,11 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/maize-tech/laravel-markable/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/maize-tech/laravel-markable/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a bug + url: https://github.com/maize-tech/laravel-markable/issues/new + about: Report a reproducable bug diff --git a/.github/SECURITY.md b/.github/SECURITY.md new file mode 100644 index 0000000..1512509 --- /dev/null +++ b/.github/SECURITY.md @@ -0,0 +1,3 @@ +# Security Policy + +If you discover any security related issues, please email inn.security@h-farm.com instead of using the issue tracker. diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..399ef82 --- /dev/null +++ b/.github/workflows/dependabot-auto-merge.yml @@ -0,0 +1,32 @@ +name: dependabot-auto-merge +on: pull_request_target + +permissions: + pull-requests: write + contents: write + +jobs: + dependabot: + runs-on: ubuntu-latest + if: ${{ github.actor == 'dependabot[bot]' }} + steps: + + - name: Dependabot metadata + id: metadata + uses: dependabot/fetch-metadata@v1.1.1 + with: + github-token: "${{ secrets.GITHUB_TOKEN }}" + + - name: Auto-merge Dependabot PRs for semver-minor updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} + + - name: Auto-merge Dependabot PRs for semver-patch updates + if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} + run: gh pr merge --auto --merge "$PR_URL" + env: + PR_URL: ${{github.event.pull_request.html_url}} + GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml new file mode 100644 index 0000000..a83d708 --- /dev/null +++ b/.github/workflows/php-cs-fixer.yml @@ -0,0 +1,23 @@ +name: Check & fix styling + +on: [push] + +jobs: + php-cs-fixer: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: ${{ github.head_ref }} + + - name: Run PHP CS Fixer + uses: docker://oskarstark/php-cs-fixer-ga + with: + args: --config=.php-cs-fixer.dist.php --allow-risky=yes + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml new file mode 100644 index 0000000..99cf0d9 --- /dev/null +++ b/.github/workflows/psalm.yml @@ -0,0 +1,33 @@ +name: Psalm + +on: + push: + paths: + - '**.php' + - 'psalm.xml.dist' + +jobs: + psalm: + name: psalm + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick + coverage: none + + - name: Cache composer dependencies + uses: actions/cache@v2 + with: + path: vendor + key: composer-${{ hashFiles('composer.lock') }} + + - name: Run composer install + run: composer install -n --prefer-dist + + - name: Run psalm + run: ./vendor/bin/psalm --output-format=github diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..cc2203a --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,46 @@ +name: run-tests + +on: [push, pull_request] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest, windows-latest] + php: [8.1, 8.0, 7.4] + laravel: [9.*, 8.*] + stability: [prefer-lowest, prefer-stable] + include: + - laravel: 8.* + testbench: ^6.23 + - laravel: 9.* + testbench: 7.* + exclude: + - php: 7.4 + laravel: 9.* + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo + coverage: none + + - name: Setup problem matchers + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + - name: Install dependencies + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + - name: Execute tests + run: vendor/bin/phpunit diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml new file mode 100644 index 0000000..fa56639 --- /dev/null +++ b/.github/workflows/update-changelog.yml @@ -0,0 +1,28 @@ +name: "Update Changelog" + +on: + release: + types: [released] + +jobs: + update: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + ref: main + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + latest-version: ${{ github.event.release.name }} + release-notes: ${{ github.event.release.body }} + + - name: Commit updated CHANGELOG + uses: stefanzweifel/git-auto-commit-action@v4 + with: + branch: main + commit_message: Update CHANGELOG + file_pattern: CHANGELOG.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0a63bc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +.idea +.php_cs +.php_cs.cache +.phpunit.result.cache +build +composer.lock +coverage +docs +phpunit.xml +psalm.xml +testbench.yaml +vendor +node_modules +.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..d612a54 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,35 @@ +in([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->name('*.php') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PSR2' => true, + 'array_syntax' => ['syntax' => 'short'], + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => true, + 'trailing_comma_in_multiline' => true, + 'phpdoc_scalar' => true, + 'unary_operator_spaces' => true, + 'binary_operator_spaces' => true, + 'blank_line_before_statement' => [ + 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], + ], + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_var_without_name' => true, + 'method_argument_space' => [ + 'on_multiline' => 'ensure_fully_multiline', + 'keep_multiple_spaces_after_comma' => true, + ], + 'single_trait_insert_per_statement' => true, + ]) + ->setFinder($finder); diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ee9293e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Changelog + +All notable changes to `laravel-markable` will be documented in this file. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..cf7d937 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2022 MAIZE SRL + +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 100644 index 0000000..8ef9fd9 --- /dev/null +++ b/README.md @@ -0,0 +1,283 @@ +

Social Card of Laravel Markable

+ +# Laravel Markable + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/maize-tech/laravel-markable.svg?style=flat-square)](https://packagist.org/packages/maize-tech/laravel-markable) +[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/maize-tech/laravel-markable/run-tests?label=tests)](https://github.com/maize-tech/laravel-markable/actions?query=workflow%3ATests+branch%3Amaster) +[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/maize-tech/laravel-markable/Check%20&%20fix%20styling?label=code%20style)](https://github.com/maize-tech/laravel-markable/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amaster) +[![Total Downloads](https://img.shields.io/packagist/dt/maize-tech/laravel-markable.svg?style=flat-square)](https://packagist.org/packages/maize-tech/laravel-markable) + +This package allows you to easily add the markable feature to your application, as for example likes, bookmarks, favorites and so on. + +## Installation + +You can install the package via composer: + +```bash +composer require maize-tech/laravel-markable +``` + +You can publish and run the migrations with: + +```bash +php artisan vendor:publish --tag="markable-migration-bookmark" # publishes bookmark migration +php artisan vendor:publish --tag="markable-migration-favorite" # publishes favorite migration +php artisan vendor:publish --tag="markable-migration-like" # publishes like migration +php artisan vendor:publish --tag="markable-migration-reaction" # publishes reaction migration + +php artisan migrate +``` + +You can publish the config file with: +```bash +php artisan vendor:publish --tag="markable-config" +``` + +This is the content of the published config file: + +```php + App\Models\User::class, + + /* + |-------------------------------------------------------------------------- + | Table prefix + |-------------------------------------------------------------------------- + | + | Here you may specify the prefix for all mark tables. + | If set, all migrations should be named with the given prefix and + | the mark's class name. + | + */ + + 'table_prefix' => 'markable_', + + /* + |-------------------------------------------------------------------------- + | Allowed values + |-------------------------------------------------------------------------- + | + | Here you may specify the list of allowed values for each mark type. + | If a specific mark should not accept any values, you can avoid adding it + | to the list. + | The array key name should match the mark's class name in lower case. + | + */ + + 'allowed_values' => [ + 'reaction' => [], + ], +]; +``` + +## Usage + +### Basic + +To use the package, add the `Maize\Markable\Markable` trait to the model where you want to have marks. + +Once done, you can define the list of possible marks for the given model implementing the `$marks` array with the list of mark classes' namespace. + +Here's an example model including the `Markable` trait and implementing the `Like` mark: + +``` php +user(); + +Like::add($course, $user); // marks the course liked for the given user + +Like::remove($course, $user); // unmarks the course liked for the given user + +Like::toggle($course, $user); // toggles the course like for the given user + +Like::has($course, $user); // returns whether the given user has marked as liked the course or not + +Like::count($course); // returns the amount of like marks for the given course +``` + +### Custom mark model + +The package allows you to define custom marks. + +First thing you need to do is create a migration which defines the new mark model. +The package works with separate tables for each mark in order to increase the performances when executing related queries. + +The migration table name should contain the prefix defined in `table_prefix` attribute under `config/markable.php`. +Default prefix is set to `markable_`. + +Here's an example migration for bookmarks: + +``` php +class CreateBookmarksTable extends Migration +{ + public function up() + { + Schema::create('markable_bookmarks', function (Blueprint $table) { + $table->id(); + $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); + $table->morphs('markable'); + $table->string('value')->nullable(); + $table->json('metadata')->nullable(); + $table->timestamps(); + }); + } +} +``` + +Once done, you can create a new class which extends the abstract `Mark` class and implement the `markableRelationName` method, which defines the name of the relation. + +Here's an example model for bookmarks: + +``` php + [ + 'reaction' => [ + 'person_raising_hand', + 'heart', + 'kissing_heart', + ], +], +``` + +You can then use the custom mark with values: + +``` php +use App\Models\Post; +use Maize\Markable\Models\Reaction; + +$post = Post::firstOrFail(); +$user = auth()->user(); + +Reaction::add($post, $user, 'kissing_heart'); // adds the 'kissing_heart' reaction to the post for the given user + +Reaction::remove($post, $user, 'kissing_heart'); // removes the 'kissing_heart' reaction to the post for the given user + +Reaction::toggle($post, $user, 'heart'); // toggles the 'heart' reaction to the post for the given user + +Reaction::has($post, $user, 'heart'); // returns whether the user has reacted with the 'heart' reaction to the given post or not + +Reaction::count($post, 'person_raising_hand'); // returns the amount of 'person_raising_hand' reactions for the given post +``` + +### Retrieve mark relation with eloquent + +``` php +use App\Models\Course; +use App\Models\Post; + +Course::firstOrFail()->likers; // returns the collection of likes to the given course with their user + +Post::firstOrFail()->reacters; // returns the collection of reactions to the given post with their user and value +``` + +### Filter marked models with eloquent + +``` php +use App\Models\Course; +use App\Models\Post; + +Course::whereHasLike( + auth()->user() +)->get(); // returns all course models with a like from the given user + +Post::whereHasReaction( + auth()->user(), + 'heart' +)->get(); // returns all post models with a 'heart' reaction from the given user +``` + +## Testing + +```bash +composer test +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. + +## Credits + +- [Enrico De Lazzari](https://github.com/enricodelazzari) +- [Riccardo Dalla Via](https://github.com/riccardodallavia) +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/art/socialcard.png b/art/socialcard.png new file mode 100644 index 0000000..3759280 Binary files /dev/null and b/art/socialcard.png differ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ad20b31 --- /dev/null +++ b/composer.json @@ -0,0 +1,70 @@ +{ + "name": "maize-tech/laravel-markable", + "description": "Laravel Markable", + "keywords": [ + "maize-tech", + "laravel", + "markable", + "likes", + "bookmarks", + "favorites" + ], + "homepage": "https://github.com/maize-tech/laravel-markable", + "license": "MIT", + "authors": [ + { + "name": "Enrico De Lazzari", + "email": "enrico.delazzari@h-farm.com", + "homepage": "https://innovation.h-farm.com", + "role": "Developer" + }, + { + "name": "Riccardo Dalla Via", + "email": "riccardo.dallavia@h-farm.com", + "homepage": "https://innovation.h-farm.com", + "role": "Developer" + } + ], + "require": { + "php": "^7.4|^8.0", + "illuminate/database": "^8.0|^9.0", + "illuminate/support": "^8.0|^9.0", + "spatie/laravel-package-tools": "^1.11.3" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.7", + "orchestra/testbench": "^6.23|^7.0", + "phpunit/phpunit": "^9.5", + "spatie/laravel-ray": "^1.29", + "vimeo/psalm": "^4.22" + }, + "autoload": { + "psr-4": { + "Maize\\Markable\\": "src", + "Maize\\Markable\\Database\\Factories\\": "database/factories" + } + }, + "autoload-dev": { + "psr-4": { + "Maize\\Markable\\Tests\\": "tests" + } + }, + "scripts": { + "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes", + "psalm": "vendor/bin/psalm", + "test": "vendor/bin/phpunit --colors=always", + "test-coverage": "vendor/bin/phpunit --coverage-html coverage" + }, + "config": { + "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "Maize\\Markable\\MarkableServiceProvider" + ] + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/config/markable.php b/config/markable.php new file mode 100644 index 0000000..4ff4333 --- /dev/null +++ b/config/markable.php @@ -0,0 +1,43 @@ + App\Models\User::class, + + /* + |-------------------------------------------------------------------------- + | Table prefix + |-------------------------------------------------------------------------- + | + | Here you may specify the prefix for all mark tables. + | If set, all migrations should be named with the given prefix and + | the mark's class name. + | + */ + + 'table_prefix' => 'markable_', + + /* + |-------------------------------------------------------------------------- + | Allowed values + |-------------------------------------------------------------------------- + | + | Here you may specify the list of allowed values for each mark type. + | If a specific mark should not accept any values, you can avoid adding it + | to the list. + | The array key name should match the mark's class name in lower case. + | + */ + + 'allowed_values' => [ + 'reaction' => [], + ], +]; diff --git a/database/factories/ArticleFactory.php b/database/factories/ArticleFactory.php new file mode 100644 index 0000000..ec6b4d9 --- /dev/null +++ b/database/factories/ArticleFactory.php @@ -0,0 +1,18 @@ +id(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('articles'); + } +} diff --git a/database/migrations/create_bookmarks_table.php.stub b/database/migrations/create_bookmarks_table.php.stub new file mode 100644 index 0000000..f1ab6d4 --- /dev/null +++ b/database/migrations/create_bookmarks_table.php.stub @@ -0,0 +1,25 @@ +id(); + $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); + $table->morphs('markable'); + $table->string('value')->nullable(); + $table->json('metadata')->nullable(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('markable_bookmarks'); + } +} diff --git a/database/migrations/create_favorites_table.php.stub b/database/migrations/create_favorites_table.php.stub new file mode 100644 index 0000000..8506bef --- /dev/null +++ b/database/migrations/create_favorites_table.php.stub @@ -0,0 +1,25 @@ +id(); + $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); + $table->morphs('markable'); + $table->string('value')->nullable(); + $table->json('metadata')->nullable(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('markable_favorites'); + } +} diff --git a/database/migrations/create_likes_table.php.stub b/database/migrations/create_likes_table.php.stub new file mode 100644 index 0000000..022f262 --- /dev/null +++ b/database/migrations/create_likes_table.php.stub @@ -0,0 +1,25 @@ +id(); + $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); + $table->morphs('markable'); + $table->string('value')->nullable(); + $table->json('metadata')->nullable(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('markable_likes'); + } +} diff --git a/database/migrations/create_posts_table.php.stub b/database/migrations/create_posts_table.php.stub new file mode 100644 index 0000000..5aadd3e --- /dev/null +++ b/database/migrations/create_posts_table.php.stub @@ -0,0 +1,21 @@ +id(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('posts'); + } +} diff --git a/database/migrations/create_reactions_table.php.stub b/database/migrations/create_reactions_table.php.stub new file mode 100644 index 0000000..c8ce0ed --- /dev/null +++ b/database/migrations/create_reactions_table.php.stub @@ -0,0 +1,25 @@ +id(); + $table->foreignId('user_id')->constrained()->cascadeOnUpdate()->cascadeOnDelete(); + $table->morphs('markable'); + $table->string('value')->nullable(); + $table->json('metadata')->nullable(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('markable_reactions'); + } +} diff --git a/database/migrations/create_users_table.php.stub b/database/migrations/create_users_table.php.stub new file mode 100644 index 0000000..bc67b6e --- /dev/null +++ b/database/migrations/create_users_table.php.stub @@ -0,0 +1,21 @@ +id(); + $table->timestamps(); + }); + } + + public function down() + { + Schema::dropIfExists('users'); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..290f954 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,39 @@ + + + + + tests + + + + + ./src + + + + + + + + + + + diff --git a/psalm.xml.dist b/psalm.xml.dist new file mode 100644 index 0000000..c6df33e --- /dev/null +++ b/psalm.xml.dist @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/src/Exceptions/InvalidMarkInstanceException.php b/src/Exceptions/InvalidMarkInstanceException.php new file mode 100644 index 0000000..3e89170 --- /dev/null +++ b/src/Exceptions/InvalidMarkInstanceException.php @@ -0,0 +1,19 @@ +__toString(); + } + + public static function add(Model $markable, Model $user, ?string $value = null): self + { + static::validMarkable($markable); + + if (! static::hasAllowedValues($value)) { + throw InvalidMarkValueException::create(); + } + + $attributes = [ + app(static::class)->getUserIdColumn() => $user->getKey(), + 'markable_id' => $markable->getKey(), + 'markable_type' => $markable->getMorphClass(), + 'value' => $value, + ]; + $values = static::forceSingleValuePerUser() + ? [Arr::pull($attributes, 'value')] + : []; + + return static::firstOrCreate($attributes, $values); + } + + public static function remove(Model $markable, Model $user, ?string $value = null) + { + static::validMarkable($markable); + + return static::where([ + app(static::class)->getUserIdColumn() => $user->getKey(), + 'markable_id' => $markable->getKey(), + 'markable_type' => $markable->getMorphClass(), + 'value' => $value, + ])->get()->each->delete(); + } + + public static function count(Model $markable, ?string $value = null): int + { + static::validMarkable($markable); + + return static::where([ + 'markable_id' => $markable->getKey(), + 'markable_type' => $markable->getMorphClass(), + 'value' => $value, + ])->count(); + } + + public static function has(Model $markable, Model $user, ?string $value = null): bool + { + return static::where([ + app(static::class)->getUserIdColumn() => $user->getKey(), + 'markable_id' => $markable->getKey(), + 'markable_type' => $markable->getMorphClass(), + 'value' => $value, + ])->exists(); + } + + public static function toggle(Model $markable, Model $user, ?string $value = null) + { + return static::has($markable, $user, $value) + ? static::remove($markable, $user, $value) + : static::add($markable, $user, $value); + } + + public static function hasAllowedValues(?string $value): bool + { + return in_array($value, static::allowedValues() ?? [null]); + } + + public function user(): BelongsTo + { + return $this->belongsTo(config('markable.user_model')); + } + + public function markable(): MorphTo + { + return $this->morphTo(); + } + + public function getUserIdColumn(): string + { + return defined('static::USER_ID') ? static::USER_ID : 'user_id'; + } + + public function getQualifiedUserIdColumn(): string + { + return $this->qualifyColumn($this->getUserIdColumn()); + } + + public function getTable(): string + { + if (is_null($this->table)) { + $this->setTable( + config('markable.table_prefix', 'markable_'). + Str::snake(Str::pluralStudly(class_basename($this))) + ); + } + + return $this->table; + } + + protected static function forceSingleValuePerUser(): bool + { + return false; + } + + protected static function validMarkable(Model $markable): bool + { + if (! static::usesMarkableTrait($markable)) { + throw InvalidMarkableInstanceException::create(); + } + + if (! in_array(static::class, $markable::marks())) { + throw InvalidMarkableInstanceException::create(); + } + + return true; + } + + protected static function usesMarkableTrait(Model $markable): bool + { + return in_array( + Markable::class, + trait_uses_recursive(get_class($markable)) + ); + } +} diff --git a/src/Markable.php b/src/Markable.php new file mode 100644 index 0000000..df00fbd --- /dev/null +++ b/src/Markable.php @@ -0,0 +1,63 @@ +whereHas( + $mark->markableRelationName(), + fn (Builder $b) => $b->where([ + $mark->getQualifiedUserIdColumn() => $user->getKey(), + 'value' => $value, + ]) + ); + } + + protected static function registerMarks(): void + { + foreach (static::marks() as $mark) { + static::registerMark($mark); + } + } + + protected static function registerMark(string $mark) + { + $instance = new $mark; + + if (! $instance instanceof Mark) { + throw InvalidMarkInstanceException::create(); + } + + static::addMarkableRelation($instance); + } + + protected static function addMarkableRelation(Mark $mark) + { + static::resolveRelationUsing( + $mark->markableRelationName(), + fn ($markable) => $markable + ->morphToMany(config('markable.user_model'), 'markable', $mark->getTable()) + ->using($mark->getMorphClass()) + ->withPivot('value') + ->withTimestamps() + ); + } +} diff --git a/src/MarkableServiceProvider.php b/src/MarkableServiceProvider.php new file mode 100644 index 0000000..6999e36 --- /dev/null +++ b/src/MarkableServiceProvider.php @@ -0,0 +1,40 @@ + 'bookmark', + 'create_favorites_table' => 'favorite', + 'create_likes_table' => 'like', + 'create_reactions_table' => 'reaction', + ]; + + public function configurePackage(Package $package): void + { + $package + ->name('laravel-markable') + ->hasConfigFile(); + } + + public function bootingPackage() + { + if (! $this->app->runningInConsole()) { + return; + } + + $now = now(); + + foreach (static::MIGRATIONS as $migrationFileName => $name) { + $this->publishes([ + $this->package->basePath("/../database/migrations/{$migrationFileName}.php.stub") => $this->generateMigrationName( + $migrationFileName, + $now->addSecond() + ), ], "{$this->package->shortName()}-migration-{$name}"); + } + } +} diff --git a/src/Models/Bookmark.php b/src/Models/Bookmark.php new file mode 100644 index 0000000..41a991a --- /dev/null +++ b/src/Models/Bookmark.php @@ -0,0 +1,13 @@ +getModel()::marks(); + + foreach ($marks as $mark) { + $this->addWhereHasMark($builder, new $mark); + } + } + + protected function addWhereHasMark(Builder $builder, Mark $mark): void + { + $builder->macro( + "whereHas{$mark::getMarkClassName()}", + fn (Builder $b, $user, ?string $value = null) => $b->whereHasMark($mark, $user, $value) + ); + } +} diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php new file mode 100644 index 0000000..e223c64 --- /dev/null +++ b/tests/BookmarkTest.php @@ -0,0 +1,38 @@ +create(); + $posts = Post::factory(2)->create(); + $users = User::factory(2)->create(); + + $this->assertCount(0, Article::whereHasBookmark($users[0])->get()); + $this->assertCount(0, Article::whereHasBookmark($users[1])->get()); + + Bookmark::add($articles[0], $users[0]); + $this->assertCount(1, Article::whereHasBookmark($users[0])->get()); + $this->assertCount(0, Article::whereHasBookmark($users[1])->get()); + + Bookmark::add($articles[2], $users[0]); + $this->assertCount(2, Article::whereHasBookmark($users[0])->get()); + $this->assertCount(0, Article::whereHasBookmark($users[1])->get()); + + Bookmark::add($articles[2], $users[1]); + $this->assertCount(2, Article::whereHasBookmark($users[0])->get()); + $this->assertCount(1, Article::whereHasBookmark($users[1])->get()); + + $this->expectException(InvalidMarkableInstanceException::class); + Bookmark::add($posts[0], $users[0]); + } +} diff --git a/tests/FavoriteTest.php b/tests/FavoriteTest.php new file mode 100644 index 0000000..7aee046 --- /dev/null +++ b/tests/FavoriteTest.php @@ -0,0 +1,38 @@ +create(); + $posts = Post::factory(2)->create(); + $users = User::factory(2)->create(); + + $this->assertCount(0, Article::whereHasFavorite($users[0])->get()); + $this->assertCount(0, Article::whereHasFavorite($users[1])->get()); + + Favorite::add($articles[0], $users[0]); + $this->assertCount(1, Article::whereHasFavorite($users[0])->get()); + $this->assertCount(0, Article::whereHasFavorite($users[1])->get()); + + Favorite::add($articles[2], $users[0]); + $this->assertCount(2, Article::whereHasFavorite($users[0])->get()); + $this->assertCount(0, Article::whereHasFavorite($users[1])->get()); + + Favorite::add($articles[2], $users[1]); + $this->assertCount(2, Article::whereHasFavorite($users[0])->get()); + $this->assertCount(1, Article::whereHasFavorite($users[1])->get()); + + $this->expectException(InvalidMarkableInstanceException::class); + Favorite::add($posts[0], $users[0]); + } +} diff --git a/tests/LikeTest.php b/tests/LikeTest.php new file mode 100644 index 0000000..5ec445a --- /dev/null +++ b/tests/LikeTest.php @@ -0,0 +1,38 @@ +create(); + $posts = Post::factory(2)->create(); + $users = User::factory(2)->create(); + + $this->assertCount(0, Article::whereHasLike($users[0])->get()); + $this->assertCount(0, Article::whereHasLike($users[1])->get()); + + Like::add($articles[0], $users[0]); + $this->assertCount(1, Article::whereHasLike($users[0])->get()); + $this->assertCount(0, Article::whereHasLike($users[1])->get()); + + Like::add($articles[2], $users[0]); + $this->assertCount(2, Article::whereHasLike($users[0])->get()); + $this->assertCount(0, Article::whereHasLike($users[1])->get()); + + Like::add($articles[2], $users[1]); + $this->assertCount(2, Article::whereHasLike($users[0])->get()); + $this->assertCount(1, Article::whereHasLike($users[1])->get()); + + Like::add($posts[0], $users[0]); + $this->assertCount(2, Article::whereHasLike($users[0])->get()); + $this->assertCount(1, Article::whereHasLike($users[1])->get()); + } +} diff --git a/tests/MarkTest.php b/tests/MarkTest.php new file mode 100644 index 0000000..d165f84 --- /dev/null +++ b/tests/MarkTest.php @@ -0,0 +1,170 @@ +create(); + $user = User::factory()->create(); + + Like::add($article, $user); + + $this->assertDatabaseHas((new Like)->getTable(), [ + 'user_id' => $user->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => null, + ]); + } + + /** @test */ + public function cannot_remove_with_an_invalid_markable_type_fail() + { + $user = User::factory()->create(); + + $this->expectException(InvalidMarkableInstanceException::class); + Like::remove($user, $user); + } + + /** @test */ + public function cannot_remove_an_unregistered_mark_fail() + { + $user = User::factory()->create(); + $post = Post::factory()->create(); + + $this->expectException(InvalidMarkableInstanceException::class); + Bookmark::remove($post, $user); + } + + /** @test */ + public function can_remove_a_mark() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + + Like::add($article, $user); + Like::remove($article, $user); + + $this->assertDatabaseMissing((new Like)->getTable(), [ + 'user_id' => $user->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + ]); + } + + /** @test */ + public function can_count_marks() + { + $articles = Article::factory(2)->create(); + $post = Post::factory()->create(); + $users = User::factory(2)->create(); + + $this->assertEquals(0, Like::count($articles[0])); + $this->assertEquals(0, Like::count($articles[1])); + $this->assertEquals(0, Like::count($post)); + + Like::add($articles[0], $users[0]); + $this->assertEquals(1, Like::count($articles[0])); + $this->assertEquals(0, Like::count($articles[1])); + $this->assertEquals(0, Like::count($post)); + + Like::add($articles[0], $users[0]); + $this->assertEquals(1, Like::count($articles[0])); + $this->assertEquals(0, Like::count($articles[1])); + $this->assertEquals(0, Like::count($post)); + + Like::add($articles[0], $users[1]); + $this->assertEquals(2, Like::count($articles[0])); + $this->assertEquals(0, Like::count($articles[1])); + $this->assertEquals(0, Like::count($post)); + + Like::add($articles[1], $users[0]); + $this->assertEquals(2, Like::count($articles[0])); + $this->assertEquals(1, Like::count($articles[1])); + $this->assertEquals(0, Like::count($post)); + + Like::add($post, $users[0]); + $this->assertEquals(2, Like::count($articles[0])); + $this->assertEquals(1, Like::count($articles[1])); + $this->assertEquals(1, Like::count($post)); + } + + /** @test */ + public function can_check_if_user_has_mark() + { + $articles = Article::factory(2)->create(); + $post = Post::factory()->create(); + $users = User::factory(2)->create(); + + $this->assertFalse(Like::has($articles[0], $users[0])); + $this->assertFalse(Like::has($articles[0], $users[1])); + $this->assertFalse(Like::has($articles[1], $users[0])); + $this->assertFalse(Like::has($articles[1], $users[1])); + + Like::add($articles[0], $users[0]); + $this->assertTrue(Like::has($articles[0], $users[0])); + $this->assertFalse(Like::has($articles[0], $users[1])); + $this->assertFalse(Like::has($articles[1], $users[0])); + $this->assertFalse(Like::has($articles[1], $users[1])); + } + + /** @test */ + public function can_toggle_a_mark() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + + Like::toggle($article, $user); + + $this->assertDatabaseHas((new Like)->getTable(), [ + 'user_id' => $user->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => null, + ]); + + Like::toggle($article, $user); + + $this->assertDatabaseMissing((new Like)->getTable(), [ + 'user_id' => $user->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + ]); + } + + /** @test */ + public function can_retrieve_markable_morph() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + + $like = Like::add($article, $user); + + $this->assertInstanceOf(Article::class, $like->markable); + + $this->assertEquals($article->getKey(), $like->markable->getKey()); + } + + /** @test */ + public function can_retrieve_user_relation() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + + $like = Like::add($article, $user); + + $this->assertInstanceOf(User::class, $like->user); + + $this->assertEquals($user->getKey(), $like->user->getKey()); + } +} diff --git a/tests/MarkableTest.php b/tests/MarkableTest.php new file mode 100644 index 0000000..6b3cd73 --- /dev/null +++ b/tests/MarkableTest.php @@ -0,0 +1,59 @@ +expectException(InvalidMarkInstanceException::class); + + $markable = new ModelWithInvalidMark; + } + + /** @test */ + public function can_register_a_mark_in_markable_model() + { + $markable = new Article; + + $this->assertInstanceOf(Article::class, $markable); + + $this->assertContains(Markable::class, trait_uses_recursive($markable)); + } + + /** @test */ + public function can_filter_marked_models() + { + $articles = Article::factory(5)->create(); + $posts = Post::factory(2)->create(); + $users = User::factory(2)->create(); + + $this->assertCount(0, Article::whereHasMark(app(Like::class), $users[0])->get()); + $this->assertCount(0, Article::whereHasMark(app(Like::class), $users[1])->get()); + + Like::add($articles[0], $users[0]); + $this->assertCount(1, Article::whereHasMark(app(Like::class), $users[0])->get()); + $this->assertCount(0, Article::whereHasMark(app(Like::class), $users[1])->get()); + + Like::add($articles[2], $users[0]); + $this->assertCount(2, Article::whereHasMark(app(Like::class), $users[0])->get()); + $this->assertCount(0, Article::whereHasMark(app(Like::class), $users[1])->get()); + + Like::add($articles[2], $users[1]); + $this->assertCount(2, Article::whereHasMark(app(Like::class), $users[0])->get()); + $this->assertCount(1, Article::whereHasMark(app(Like::class), $users[1])->get()); + + Like::add($posts[0], $users[0]); + $this->assertCount(2, Article::whereHasMark(app(Like::class), $users[0])->get()); + $this->assertCount(1, Article::whereHasMark(app(Like::class), $users[1])->get()); + } +} diff --git a/tests/Models/Article.php b/tests/Models/Article.php new file mode 100644 index 0000000..adc47e7 --- /dev/null +++ b/tests/Models/Article.php @@ -0,0 +1,24 @@ +create(); + $user = User::factory()->create(); + + $this->expectException(InvalidMarkValueException::class); + Reaction::add($article, $user); + } + + /** @test */ + public function cannot_add_an_invalid_reaction_value_fails() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + + $this->expectException(InvalidMarkValueException::class); + Reaction::add($article, $user, 'not_valid_value'); + } + + /** @test */ + public function can_add_a_reaction() + { + $article = Article::factory()->create(); + $users = User::factory(2)->create(); + $table = (new Reaction)->getTable(); + + Reaction::add($article, $users[0], 'reaction_1'); + $this->assertDatabaseCount($table, 1); + $this->assertDatabaseHas($table, [ + 'user_id' => $users[0]->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => 'reaction_1', + ]); + + Reaction::add($article, $users[0], 'reaction_2'); + $this->assertDatabaseCount($table, 2); + $this->assertDatabaseHas($table, [ + 'user_id' => $users[0]->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => 'reaction_2', + ]); + + Reaction::add($article, $users[0], 'reaction_2'); + $this->assertDatabaseCount($table, 2); + + Reaction::add($article, $users[1], 'reaction_2'); + $this->assertDatabaseCount($table, 3); + $this->assertDatabaseHas($table, [ + 'user_id' => $users[1]->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => 'reaction_2', + ]); + } + + /** @test */ + public function can_remove_a_reaction() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + $table = (new Reaction)->getTable(); + + Reaction::add($article, $user, 'reaction_1'); + $this->assertDatabaseCount($table, 1); + + Reaction::remove($article, $user, 'not_valid_value'); + $this->assertDatabaseCount($table, 1); + + Reaction::remove($article, $user, 'reaction_1'); + $this->assertDatabaseCount($table, 0); + } + + /** @test */ + public function can_count_reactions() + { + $article = Article::factory()->create(); + $users = User::factory(2)->create(); + $table = (new Reaction)->getTable(); + + $this->assertEquals(0, Reaction::count($article, 'reaction_1')); + + Reaction::add($article, $users[0], 'reaction_1'); + $this->assertEquals(1, Reaction::count($article, 'reaction_1')); + $this->assertEquals(0, Reaction::count($article, 'reaction_2')); + + Reaction::add($article, $users[0], 'reaction_1'); + Reaction::add($article, $users[1], 'reaction_1'); + $this->assertEquals(2, Reaction::count($article, 'reaction_1')); + $this->assertEquals(0, Reaction::count($article, 'reaction_2')); + } + + /** @test */ + public function can_check_if_user_has_reaction() + { + $article = Article::factory()->create(); + $users = User::factory(2)->create(); + $table = (new Reaction)->getTable(); + + $this->assertFalse(Reaction::has($article, $users[0], 'reaction_1')); + $this->assertFalse(Reaction::has($article, $users[1], 'reaction_1')); + + Reaction::add($article, $users[0], 'reaction_1'); + $this->assertTrue(Reaction::has($article, $users[0], 'reaction_1')); + $this->assertFalse(Reaction::has($article, $users[1], 'reaction_1')); + } + + /** @test */ + public function cannot_toggle_an_invalid_reaction_value_null_fails() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + + $this->expectException(InvalidMarkValueException::class); + Reaction::toggle($article, $user); + } + + /** @test */ + public function cannot_toggle_an_invalid_reaction_value_fails() + { + $article = Article::factory()->create(); + $user = User::factory()->create(); + + $this->expectException(InvalidMarkValueException::class); + Reaction::toggle($article, $user, 'not_valid_value'); + } + + /** @test */ + public function can_toggle_a_reaction() + { + $article = Article::factory()->create(); + $users = User::factory(2)->create(); + $table = (new Reaction)->getTable(); + + Reaction::toggle($article, $users[0], 'reaction_1'); + $this->assertDatabaseCount($table, 1); + $this->assertDatabaseHas($table, [ + 'user_id' => $users[0]->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => 'reaction_1', + ]); + + Reaction::toggle($article, $users[0], 'reaction_2'); + $this->assertDatabaseCount($table, 2); + $this->assertDatabaseHas($table, [ + 'user_id' => $users[0]->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => 'reaction_2', + ]); + + Reaction::toggle($article, $users[0], 'reaction_2'); + $this->assertDatabaseCount($table, 1); + $this->assertDatabaseMissing($table, [ + 'user_id' => $users[0]->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => 'reaction_2', + ]); + + Reaction::toggle($article, $users[1], 'reaction_3'); + $this->assertDatabaseCount($table, 2); + $this->assertDatabaseHas($table, [ + 'user_id' => $users[1]->getKey(), + 'markable_id' => $article->getKey(), + 'markable_type' => $article->getMorphClass(), + 'value' => 'reaction_3', + ]); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..65e708e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,66 @@ + 'Maize\\Markable\\Database\\Factories\\'.class_basename($modelName).'Factory' + ); + } + + protected function getPackageProviders($app) + { + return [ + MarkableServiceProvider::class, + ]; + } + + public function getEnvironmentSetUp($app) + { + $app['config']->set('database.default', 'sqlite'); + $app['config']->set('database.connections.sqlite', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + + $app['config']->set('markable.user_model', User::class); + $app['config']->set('markable.allowed_values.reaction', [ + 'reaction_1', + 'reaction_2', + 'reaction_3', + 'reaction_4', + ]); + + include_once __DIR__.'/../database/migrations/create_users_table.php.stub'; + (new \CreateUsersTable())->up(); + + include_once __DIR__.'/../database/migrations/create_articles_table.php.stub'; + (new \CreateArticlesTable())->up(); + + include_once __DIR__.'/../database/migrations/create_posts_table.php.stub'; + (new \CreatePostsTable())->up(); + + include_once __DIR__.'/../database/migrations/create_likes_table.php.stub'; + (new \CreateLikesTable())->up(); + + include_once __DIR__.'/../database/migrations/create_favorites_table.php.stub'; + (new \CreateFavoritesTable())->up(); + + include_once __DIR__.'/../database/migrations/create_bookmarks_table.php.stub'; + (new \CreateBookmarksTable())->up(); + + include_once __DIR__.'/../database/migrations/create_reactions_table.php.stub'; + (new \CreateReactionsTable())->up(); + } +}