Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
manchenkoff committed Sep 24, 2023
0 parents commit 3bc2348
Show file tree
Hide file tree
Showing 28 changed files with 1,342 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.fleet
/.idea
/.vscode

/vendor
composer.lock

*.cache
.*.cache

/tests/cache

.DS_Store
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Artem Manchenkov

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.
132 changes: 132 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Laravel Repositories

Package provides a basic implementation of Repository pattern with `artisan` command to generate classes.

Features:

- `Repository` class with basic methods like `all`, `find`, `create`, `update`, `delete`
- Generic type comments to pass `PHPStan` checks
- Artisan `make:repository` command to generate repository class with model and interface

## Installation

To install this package, you need to install [Composer](https://getcomposer.org/) first, and then run:

```bash
composer require manchenkoff/laravel-repositories
```

or add this line to `composer.json`:

```json
"manchenkoff/laravel-repositories": "*"
```

and run `composer update` in the terminal.

Package should automatically register its service provider in your application, but you can do it manually in `config/app.php`:

```php
'providers' => ServiceProvider::defaultProviders()
->merge([
// Package Service Providers
\Manchenkov\Laravel\Repositories\ServiceProvider::class,

// Application Service Providers
// ...
])
->toArray(),
```

## Usage

First of all, you need to create a model class for your repository. You can do it manually or use `artisan` command:

```bash
php artisan make:model Post
```

Then you can create a repository class for your model:

```bash
# repository name - PostRepository
# model name - Post
php artisan make:repository PostRepository Post
```

This command will create a repository class in `app/Repositories` directory and `PostRepositoryInterface` contract class in `app/Contracts/Repositories`.

Now you can use existing methods in your services or extend with custom functionality:

```php
<?php

namespace App\Services;

use Illuminate\Database\Eloquent\Collection;
use App\Contracts\Repositories\PostRepositoryInterface;
use App\Contracts\Services\PostServiceInterface;

final class PostService implements PostServiceInterface
{
private readonly PostRepositoryInterface $repository;

public function __construct(PostRepositoryInterface $repository)
{
$this->repository = $repository;
}

public function getAllPosts(): Collection
{
return $this->repository->all();
}
}
```

## Implementation

All repository methods use protected `query()` method to get `Eloquent\Builder` instance. You can override this method in your repository class to add custom logic, e.g. when you always need some relations to be loaded or custom sorting applied.

```php
protected function query(): Builder
{
return parent::query()->with('comments')->orderBy('created_at', 'desc');
}
```

Here is a list of available methods with a quick description:

| Method | Description |
| ------------------------------------------- | ------------------------------------------------------- |
| `paginated(): LengthAwarePaginator` | returns paginated collection |
| `all(): Collection` | returns all entities |
| `find(mixed $id): ?Model` | returns entity by id or null |
| `get(mixed $id): Model` | returns entity by id or throws `ModelNotFoundException` |
| `create(array $data): Model` | creates new entity with given data |
| `update(Model $entity, array $data): Model` | updates existing entity with given data |
| `updateMany(array $ids, array $data): void` | updates many entities with given data by ids |
| `delete(Model $entity): Model` | deletes existing entity |
| `deleteMany(array $ids): void` | deletes many entities by ids |

## Development

This package is completely open-source, so any contributions are welcome!

Clone this repository to your local machine, install dependencies and run tests:

```bash
git clone https://github.com/manchenkoff/laravel-repositories
cd laravel-repositories
composer install
composer test
```

There are some useful `composer` scripts:

| Script | Description |
| --------------------- | ---------------------------------------------- |
| `composer fmt` | Apply Laravel Pint code style rules |
| `composer test` | Run tests with Testbench package |
| `composer lint` | Run PHP Stan analysis against package codebase |
| `composer rector` | Run Rector analysis against package codebase |
| `composer rector:fix` | Apply available Rector suggestions |
80 changes: 80 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "manchenkoff/laravel-repositories",
"description": "Repository pattern implementation for your Laravel application",
"type": "library",
"license": "MIT",
"keywords": [
"laravel",
"repository",
"eloquent"
],
"authors": [
{
"name": "manchenkoff",
"email": "[email protected]"
}
],
"scripts": {
"fmt": "vendor/bin/pint",
"test": "vendor/bin/testbench package:test",
"insights": "vendor/bin/phpinsights",
"lint": "vendor/bin/phpstan analyse --memory-limit=256M",
"rector": "vendor/bin/rector process src --dry-run",
"rector:fix": "vendor/bin/rector process src",
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"@build",
"@php vendor/bin/testbench serve"
]
},
"autoload": {
"psr-4": {
"Manchenkoff\\Laravel\\Repositories\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Manchenkoff\\Laravel\\Repositories\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"minimum-stability": "stable",
"require": {
"php": "^8.2",
"illuminate/console": "^10.0",
"illuminate/support": "^10.0",
"illuminate/database": "^10.0",
"illuminate/contracts": "^10.0"
},
"require-dev": {
"icanhazstring/composer-unused": "^0.8.10",
"laravel/pint": "^1.13",
"phpunit/phpunit": "^10",
"phpstan/phpstan": "^1.10",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^8.11",
"rector/rector": "^0.18.3",
"driftingly/rector-laravel": "^0.26.0",
"nunomaduro/collision": "^7.8"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
"laravel": {
"providers": [
"Manchenkoff\\Laravel\\Repositories\\ServiceProvider"
]
}
}
}
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon
parameters:
level: 9
checkGenericClassInNonGenericObjectType: false
paths:
- src
- tests
ignoreErrors: []
12 changes: 12 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<phpunit bootstrap="vendor/autoload.php" colors="true" cacheDirectory="tests/cache">
<testsuites>
<testsuite name="Repository Tests">
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
14 changes: 14 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"preset": "laravel",
"rules": {
"final_class": true,
"phpdoc_separation": true,
"declare_strict_types": true,
"no_superfluous_phpdoc_tags": false,
"not_operator_with_successor_space": false,
"phpdoc_var_annotation_correct_order": true,
"concat_space": {
"spacing": "one"
}
}
}
18 changes: 18 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use RectorLaravel\Set\LaravelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/app',
__DIR__ . '/tests',
]);

// define sets of rules
$rectorConfig->sets([
LaravelSetList::LARAVEL_100,
]);
};
Loading

0 comments on commit 3bc2348

Please sign in to comment.