From e8ad5c43a59f761914401ab01e1e1a6eb6204097 Mon Sep 17 00:00:00 2001 From: Jasper Tey Date: Tue, 17 Jan 2023 16:06:57 -0500 Subject: [PATCH] Initial commit. --- .editorconfig | 15 ++++ .gitattributes | 19 +++++ .github/ISSUE_TEMPLATE/config.yml | 14 ++++ .github/dependabot.yml | 12 +++ .github/workflows/dependabot-auto-merge.yml | 32 ++++++++ .../workflows/fix-php-code-style-issues.yml | 24 ++++++ .github/workflows/phpstan.yml | 26 +++++++ .github/workflows/run-tests.yml | 51 +++++++++++++ .gitignore | 11 +++ CHANGELOG.md | 3 + LICENSE.md | 21 +++++ README.md | 76 +++++++++++++++++++ composer.json | 72 ++++++++++++++++++ config/ddd.php | 33 ++++++++ database/factories/ModelFactory.php | 19 +++++ phpstan-baseline.neon | 0 phpstan.neon.dist | 14 ++++ phpunit.xml.dist | 39 ++++++++++ .../stubs/components/base-model.php.stub | 22 ++++++ resources/stubs/components/dto.php.stub | 13 ++++ resources/stubs/components/model.php.stub | 20 +++++ resources/views/.gitkeep | 0 src/Commands/DomainGeneratorCommand.php | 46 +++++++++++ src/Commands/LaravelDDDCommand.php | 19 +++++ src/Commands/MakeDTO.php | 37 +++++++++ src/Commands/MakeModel.php | 37 +++++++++ src/Facades/LaravelDDD.php | 16 ++++ src/LaravelDDD.php | 7 ++ src/LaravelDDDServiceProvider.php | 27 +++++++ tests/ExampleTest.php | 5 ++ tests/Pest.php | 5 ++ tests/TestCase.php | 36 +++++++++ 32 files changed, 771 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/dependabot-auto-merge.yml create mode 100644 .github/workflows/fix-php-code-style-issues.yml create mode 100644 .github/workflows/phpstan.yml create mode 100644 .github/workflows/run-tests.yml create mode 100644 .gitignore create mode 100644 CHANGELOG.md create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 composer.json create mode 100644 config/ddd.php create mode 100644 database/factories/ModelFactory.php create mode 100644 phpstan-baseline.neon create mode 100644 phpstan.neon.dist create mode 100644 phpunit.xml.dist create mode 100644 resources/stubs/components/base-model.php.stub create mode 100644 resources/stubs/components/dto.php.stub create mode 100644 resources/stubs/components/model.php.stub create mode 100644 resources/views/.gitkeep create mode 100644 src/Commands/DomainGeneratorCommand.php create mode 100644 src/Commands/LaravelDDDCommand.php create mode 100644 src/Commands/MakeDTO.php create mode 100644 src/Commands/MakeModel.php create mode 100644 src/Facades/LaravelDDD.php create mode 100755 src/LaravelDDD.php create mode 100644 src/LaravelDDDServiceProvider.php create mode 100644 tests/ExampleTest.php create mode 100644 tests/Pest.php create mode 100644 tests/TestCase.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..dd9a2b5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +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..9e9519b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,19 @@ +# 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 +/art export-ignore +/docs export-ignore +/tests export-ignore +/.editorconfig export-ignore +/.php_cs.dist.php export-ignore +/psalm.xml export-ignore +/psalm.xml.dist export-ignore +/testbench.yaml export-ignore +/UPGRADING.md export-ignore +/phpstan.neon.dist export-ignore +/phpstan-baseline.neon export-ignore diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..7cd7d0f --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,14 @@ +blank_issues_enabled: false +contact_links: + - name: Ask a question + url: https://github.com/lunarstorm/laravel-ddd/discussions/new?category=q-a + about: Ask the community for help + - name: Request a feature + url: https://github.com/lunarstorm/laravel-ddd/discussions/new?category=ideas + about: Share ideas for new features + - name: Report a security issue + url: https://github.com/lunarstorm/laravel-ddd/security/policy + about: Learn how to notify us for sensitive bugs + - name: Report a bug + url: https://github.com/lunarstorm/laravel-ddd/issues/new + about: Report a reproducible bug diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..30c8a49 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,12 @@ +# Please see the documentation for all configuration options: +# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates + +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + labels: + - "dependencies" \ No newline at end of file diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml new file mode 100644 index 0000000..e7e28b9 --- /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.3.5 + 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/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml new file mode 100644 index 0000000..150750c --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -0,0 +1,24 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +jobs: + php-code-styling: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@1.0.0 + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..9d41c0c --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,26 @@ +name: PHPStan + +on: + push: + paths: + - '**.php' + - 'phpstan.neon.dist' + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + coverage: none + + - name: Install composer dependencies + uses: ramsey/composer-install@v2 + + - name: Run PHPStan + run: ./vendor/bin/phpstan --error-format=github diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..08cc9ab --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,51 @@ +name: run-tests + +on: + push: + branches: [main] + pull_request: + branches: [main] + +jobs: + test: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ubuntu-latest, windows-latest] + php: [8.2, 8.1] + laravel: [9.*] + stability: [prefer-lowest, prefer-stable] + include: + - laravel: 9.* + testbench: 7.* + carbon: ^2.63 + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - 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 }}" "nesbot/carbon:${{ matrix.carbon }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: List Installed Dependencies + run: composer show -D + + - name: Execute tests + run: vendor/bin/pest diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..83c9b9f --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +.idea +.phpunit.result.cache +build +composer.lock +coverage +docs +phpunit.xml +phpstan.neon +testbench.yaml +vendor +node_modules diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..fb01308 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# Changelog + +All notable changes to `laravel-ddd` will be documented in this file. diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..d0a48db --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) lunarstorm + +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..ce7c4c7 --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +# A Laravel toolkit for Domain Driven Design patterns + +[![Latest Version on Packagist](https://img.shields.io/packagist/v/lunarstorm/laravel-ddd.svg?style=flat-square)](https://packagist.org/packages/lunarstorm/laravel-ddd) +[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/lunarstorm/laravel-ddd/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/lunarstorm/laravel-ddd/actions?query=workflow%3Arun-tests+branch%3Amain) +[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/lunarstorm/laravel-ddd/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/lunarstorm/laravel-ddd/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) +[![Total Downloads](https://img.shields.io/packagist/dt/lunarstorm/laravel-ddd.svg?style=flat-square)](https://packagist.org/packages/lunarstorm/laravel-ddd) + +Laravel-DDD is a developer toolkit to support domain driven design (DDD) patterns in Laravel applications. More details to come. + +## Installation + +You can install the package via composer: + +```bash +composer require lunarstorm/laravel-ddd +``` + +You can publish and run the migrations with: + +```bash +php artisan vendor:publish --tag="laravel-ddd-migrations" +php artisan migrate +``` + +You can publish the config file with: + +```bash +php artisan vendor:publish --tag="laravel-ddd-config" +``` + +This is the contents of the published config file: + +```php +return [ +]; +``` + +Optionally, you can publish the views using + +```bash +php artisan vendor:publish --tag="laravel-ddd-views" +``` + +## Usage + +```php +$laravelDDD = new Lunarstorm\LaravelDDD(); +echo $laravelDDD->echoPhrase('Hello, Lunarstorm!'); +``` + +## Testing + +```bash +composer test +``` + +## Changelog + +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. + +## Security Vulnerabilities + +Please review [our security policy](../../security/policy) on how to report security vulnerabilities. + +## Credits + +- [Jasper Tey](https://github.com/JasperTey) +- [All Contributors](../../contributors) + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..1046bb9 --- /dev/null +++ b/composer.json @@ -0,0 +1,72 @@ +{ + "name": "lunarstorm/laravel-ddd", + "description": "A Laravel toolkit for Domain Driven Design patterns", + "keywords": [ + "lunarstorm", + "laravel", + "laravel-ddd" + ], + "homepage": "https://github.com/lunarstorm/laravel-ddd", + "license": "MIT", + "authors": [ + { + "name": "Jasper Tey", + "email": "jasper@jtey.com", + "role": "Developer" + } + ], + "require": { + "php": "^8.1", + "spatie/laravel-package-tools": "^1.13.0", + "illuminate/contracts": "^9.0" + }, + "require-dev": { + "laravel/pint": "^1.0", + "nunomaduro/collision": "^6.0", + "nunomaduro/larastan": "^2.0.1", + "orchestra/testbench": "^7.0", + "pestphp/pest": "^1.21", + "pestphp/pest-plugin-laravel": "^1.1", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^9.5" + }, + "autoload": { + "psr-4": { + "Lunarstorm\\LaravelDDD\\": "src", + "Lunarstorm\\LaravelDDD\\Database\\Factories\\": "database/factories" + } + }, + "autoload-dev": { + "psr-4": { + "Lunarstorm\\LaravelDDD\\Tests\\": "tests" + } + }, + "scripts": { + "post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi", + "analyse": "vendor/bin/phpstan analyse", + "test": "vendor/bin/pest", + "test-coverage": "vendor/bin/pest --coverage", + "format": "vendor/bin/pint" + }, + "config": { + "sort-packages": true, + "allow-plugins": { + "pestphp/pest-plugin": true, + "phpstan/extension-installer": true + } + }, + "extra": { + "laravel": { + "providers": [ + "Lunarstorm\\LaravelDDD\\LaravelDDDServiceProvider" + ], + "aliases": { + "LaravelDDD": "Lunarstorm\\LaravelDDD\\Facades\\LaravelDDD" + } + } + }, + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/config/ddd.php b/config/ddd.php new file mode 100644 index 0000000..2ca89b1 --- /dev/null +++ b/config/ddd.php @@ -0,0 +1,33 @@ + [ + // Path to the Application layer + 'application' => 'app/Modules', + + // Path to the Domain layer + 'domains' => 'src/Domains', + + // Relative paths of domain objects + 'dataTransferObjects' => 'Data', + 'models' => 'Models', + 'viewModels' => 'ViewModels', + + // Relative paths of domain-application objects + 'requests' => 'Requests', + 'resources' => 'Resources', + 'controllers' => 'Controllers', + ], + + 'namespaces' => [ + // Domain Layer + 'models' => 'Models', + 'data_transfer_objects' => 'Data', + 'view_models' => 'ViewModels', + + // Application Layer + 'requests' => 'Requests', + 'resources' => 'Resources', + 'controllers' => 'Controllers', + ] +]; diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php new file mode 100644 index 0000000..3ecd149 --- /dev/null +++ b/database/factories/ModelFactory.php @@ -0,0 +1,19 @@ + + + + + tests + + + + + ./src + + + + + + + + + + + diff --git a/resources/stubs/components/base-model.php.stub b/resources/stubs/components/base-model.php.stub new file mode 100644 index 0000000..dc6d209 --- /dev/null +++ b/resources/stubs/components/base-model.php.stub @@ -0,0 +1,22 @@ +explode('\\'); + $domain = $parts[1]; + $model = $parts->last(); + + return app("Database\\Factories\\{$domain}\\{$model}Factory"); + } +} diff --git a/resources/stubs/components/dto.php.stub b/resources/stubs/components/dto.php.stub new file mode 100644 index 0000000..c06a8bb --- /dev/null +++ b/resources/stubs/components/dto.php.stub @@ -0,0 +1,13 @@ +getDomain(); + + return $rootNamespace . '\\' . $domain . '\\Models'; + } + + protected function getDomainInput() + { + return $this->argument('domain'); + } + + protected function getDomain() + { + return str($this->getDomainInput()) + ->trim() + ->studly() + ->toString(); + } + + protected function getDomainBasePath() + { + return $this->laravel->basePath(config('ddd.paths.domains', 'src/Domains')); + } + + protected function getPath($name) + { + $name = Str::replaceFirst($this->rootNamespace(), '', $name); + + return $this->getDomainBasePath() . '/' . str_replace('\\', '/', $name) . '.php'; + } +} diff --git a/src/Commands/LaravelDDDCommand.php b/src/Commands/LaravelDDDCommand.php new file mode 100644 index 0000000..3f39ba2 --- /dev/null +++ b/src/Commands/LaravelDDDCommand.php @@ -0,0 +1,19 @@ +comment('All done'); + + return self::SUCCESS; + } +} diff --git a/src/Commands/MakeDTO.php b/src/Commands/MakeDTO.php new file mode 100644 index 0000000..60a51d2 --- /dev/null +++ b/src/Commands/MakeDTO.php @@ -0,0 +1,37 @@ +alreadyExists(); + } +} diff --git a/src/Commands/MakeModel.php b/src/Commands/MakeModel.php new file mode 100644 index 0000000..f8e3082 --- /dev/null +++ b/src/Commands/MakeModel.php @@ -0,0 +1,37 @@ +alreadyExists(); + } +} diff --git a/src/Facades/LaravelDDD.php b/src/Facades/LaravelDDD.php new file mode 100644 index 0000000..2ec6e6b --- /dev/null +++ b/src/Facades/LaravelDDD.php @@ -0,0 +1,16 @@ +name('laravel-ddd') + ->hasConfigFile('ddd') + ->hasCommands([ + LaravelDDDCommand::class, + MakeModel::class, + ]); + } +} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php new file mode 100644 index 0000000..5d36321 --- /dev/null +++ b/tests/ExampleTest.php @@ -0,0 +1,5 @@ +toBeTrue(); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 0000000..cc2017a --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,5 @@ +in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..dcbec82 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,36 @@ + 'Lunarstorm\\LaravelDDD\\Database\\Factories\\' . class_basename($modelName) . 'Factory' + ); + } + + protected function getPackageProviders($app) + { + return [ + LaravelDDDServiceProvider::class, + ]; + } + + public function getEnvironmentSetUp($app) + { + config()->set('database.default', 'testing'); + + /* + $migration = include __DIR__.'/../database/migrations/create_laravel-ddd_table.php.stub'; + $migration->up(); + */ + } +}