Skip to content

Commit

Permalink
feat: Initial re-release
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Jul 7, 2020
1 parent dd97111 commit fe85ff2
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 69 deletions.
13 changes: 3 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.1.0] - 2020-06-28
### Fixed
- Change `orchestra/testbench` version to match Laravel version.
### Added
- Added a `IdentityManager::flushIdentities()` method to clear the identity map (#2).

## [1.0.0] - 2020-06-24
## [1.0.0] - 2020-07-07
- Initial release

[Unreleased]: https://github.com/sprocketbox/eloquence/compare/v1.1.0...develop
[1.0.0]: https://github.com/sprocketbox/eloquence/releases/tag/v1.0.0
[1.1.0]: https://github.com/sprocketbox/eloquence/releases/tag/v1.1.0
[Unreleased]: https://github.com/sprocketbox/eloquent-identity/compare/v1.0.0...develop
[1.0.0]: https://github.com/sprocketbox/eloquent-identity/releases/tag/v1.0.0
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# Eloquence
[![Latest Stable Version](https://poser.pugx.org/sprocketbox/eloquence/v/stable.png)](https://packagist.org/packages/sprocketbox/eloquence)
[![Latest Unstable Version](https://poser.pugx.org/sprocketbox/eloquence/v/unstable.png)](https://packagist.org/packages/sprocketbox/eloquence)
[![License](https://poser.pugx.org/sprocketbox/eloquence/license.png)](https://packagist.org/packages/sprocketbox/eloquence)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sprocketbox/eloquence/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sprocketbox/eloquence/?branch=master)
# Eloquent Identity
[![Latest Stable Version](https://poser.pugx.org/sprocketbox/eloquent-identity/v/stable.png)](https://packagist.org/packages/sprocketbox/eloquent-identity)
[![Latest Unstable Version](https://poser.pugx.org/sprocketbox/eloquent-identity/v/unstable.png)](https://packagist.org/packages/sprocketbox/eloquent-identity)
[![License](https://poser.pugx.org/sprocketbox/eloquent-identity/license.png)](https://packagist.org/packages/sprocketbox/eloquent-identity)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/sprocketbox/eloquent-identity/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/sprocketbox/eloquent-identity/?branch=master)

- **Laravel**: 7
- **PHP**: 7.4+
- **License**: MIT
- **Author**: Ollie Read
- **Author Homepage**: https://sprocketbox.io

Eloquence provides a cache on top of Eloquent that prevents multiple models being created for a single database row
using the Identity Map design pattern ([P of EAA](https://martinfowler.com/eaaCatalog/identityMap.html) [Wikipedia](https://en.wikipedia.org/wiki/Identity_map_pattern)).
Eloquent identity provides a cache on top of Eloquent preventing multiple models being created for a single database row
using the Identity Map design pattern ([P of EAA](https://martinfowler.com/eaaCatalog/identityMap.html) & [Wikipedia](https://en.wikipedia.org/wiki/Identity_map_pattern)).

#### Table of Contents

Expand All @@ -28,23 +28,23 @@ using the Identity Map design pattern ([P of EAA](https://martinfowler.com/eaaCa
To install this package simply run the following command.

```
composer require sprocketbox/eloquence
composer require sprocketbox/eloquent-identity
```

This package uses auto-discovery to register the service provider but if you'd rather do it manually,
the service provider is:

```
Sprocketbox\Eloquence\ServiceProvider
Sprocketbox\Eloquent\Identity\ServiceProvider
```

There is no configuration required.

## Usage
To make use of Eloquence on your models, add the following trait.
To make use of the Eloquent identity map on your models, add the following trait.

```
Sprocketbox\Eloquence\Concerns\MapsIdentity
Sprocketbox\Eloquent\Identity\Concerns\MapsIdentity
```

### Finding
Expand All @@ -70,7 +70,7 @@ If a belongs to relationship is loaded (not belongs to many) without constraints
called, the query will skip any model instances that already exist.

### Flushing
If you wish to flush the cached models, call `flushIdentities()` on an instance of `IdentityManager`, or on the `Eloquence`
If you wish to flush the cached models, call `flushIdentities()` on an instance of `IdentityManager`, or on the `Identity`
facade.

## How
Expand All @@ -79,7 +79,7 @@ The `IdentityManager` stores an array containing all existing model instances an
The identities for models are stored as string, created using the following class.

```
Sprocketbox\Eloquence\ModelIdentity
Sprocketbox\Eloquent\Identity\ModelIdentity
```

This contains a key, the model class name, and the connection name. The string version of these looks like so:
Expand All @@ -92,6 +92,6 @@ connection:class:key
It's very easy to end up with multiple versions of the same model, meaning that updates on one aren't persisted
to others.

Eloquence was created to reduce the number of models created, help limit unnecessary queries, and allow for consistent
Eloquent identity was created to reduce the number of models created, help limit unnecessary queries, and allow for consistent
model interaction. It doesn't matter where in your code you're dealing with user 1, any changes made during a request
will persist across all instances.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "sprocketbox/eloquence",
"name": "sprocketbox/eloquent-identity",
"description": "An identity map implementation for Eloquent",
"keywords": [
"laravel",
Expand All @@ -17,12 +17,12 @@
],
"autoload": {
"psr-4": {
"Sprocketbox\\Eloquence\\": "src/"
"Sprocketbox\\Eloquent\\Identity\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Sprocketbox\\Eloquence\\Tests\\": "tests/"
"Sprocketbox\\Eloquent\\Identity\\Tests\\": "tests/"
}
},
"require": {
Expand All @@ -35,10 +35,10 @@
"extra": {
"laravel": {
"providers": [
"Sprocketbox\\Eloquence\\ServiceProvider"
"Sprocketbox\\Eloquent\\Identity\\ServiceProvider"
],
"facades": [
"Sprocketbox\\Eloquence\\Facades\\Eloquence"
"Identity"
]
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/Concerns/MapsIdentity.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php /** @noinspection PhpUndefinedMethodInspection */

namespace Sprocketbox\Eloquence\Concerns;
namespace Sprocketbox\Eloquent\Identity\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Date;
use Laravel\Nova\Fields\BelongsToMany;
use LogicException;
use Sprocketbox\Eloquence\Facades\Eloquence;
use Sprocketbox\Eloquence\ModelIdentity;
use Sprocketbox\Eloquence\Query\Builder;
use Sprocketbox\Eloquent\Identity\Facades\Identity;
use Sprocketbox\Eloquent\Identity\ModelIdentity;
use Sprocketbox\Eloquent\Identity\Query\Builder;

/**
* MapsIdentity
*
* This trait provides identity map functionality for Eloquent models.
*
* @package Sprocketbox\Eloquence\Concerns
* @package Sprocketbox\Eloquent\Identity\Concerns
*/
trait MapsIdentity
{
Expand All @@ -27,9 +27,9 @@ trait MapsIdentity
public static function bootMapsIdentity(): void
{
// Add a deleted event so the identity is removed from the map
static::deleted(fn(Model $model) => Eloquence::removeIdentity($model->getModelIdentity()));
static::deleted(fn(Model $model) => Identity::removeIdentity($model->getModelIdentity()));
// Add a created event so newly created models are stored
static::created(fn(Model $model) => Eloquence::storeIdentity($model->getModelIdentity(), $model));
static::created(fn(Model $model) => Identity::storeIdentity($model->getModelIdentity(), $model));
}

/**
Expand All @@ -52,8 +52,8 @@ public function newFromBuilder($attributes = [], $connection = null): Model
if ($key !== null) {
$identity = $this->getModelIdentity($key, $connection);

if (Eloquence::hasIdentity($identity)) {
$model = Eloquence::getIdentity($identity);
if (Identity::hasIdentity($identity)) {
$model = Identity::getIdentity($identity);
/** @noinspection NullPointerExceptionInspection */
$this->updateModelAttributes($model, $attributes);

Expand All @@ -64,7 +64,7 @@ public function newFromBuilder($attributes = [], $connection = null): Model
$model = parent::newFromBuilder($attributes, $connection);

if ($identity !== null) {
Eloquence::storeIdentity($model->getModelIdentity(), $model);
Identity::storeIdentity($model->getModelIdentity(), $model);
}

return $model;
Expand All @@ -76,7 +76,7 @@ public function newFromBuilder($attributes = [], $connection = null): Model
* @param null $id
* @param string|null $connection
*
* @return \Sprocketbox\Eloquence\ModelIdentity
* @return \Sprocketbox\Eloquent\Identity\ModelIdentity
*/
public function getModelIdentity($id = null, ?string $connection = null): ModelIdentity
{
Expand All @@ -90,7 +90,7 @@ public function getModelIdentity($id = null, ?string $connection = null): ModelI
*
* @param \Illuminate\Database\Query\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder|\Sprocketbox\Eloquence\Query\Builder|static
* @return \Illuminate\Database\Eloquent\Builder|\Sprocketbox\Eloquent\Identity\Query\Builder|static
*/
public function newEloquentBuilder($query)
{
Expand Down Expand Up @@ -187,8 +187,8 @@ protected function getRelationshipResults(Relation $relation)
if (method_exists($related, 'getModelIdentity')) {
$identity = $related->getModelIdentity($this->getAttribute($relation->getForeignKeyName()));

if (Eloquence::hasIdentity($identity)) {
return Eloquence::getIdentity($identity);
if (Identity::hasIdentity($identity)) {
return Identity::getIdentity($identity);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/Facades/Eloquence.php → src/Facades/Identity.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace Sprocketbox\Eloquence\Facades;
namespace Sprocketbox\Eloquent\Identity\Facades;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Facade;
use Sprocketbox\Eloquence\IdentityManager;
use Sprocketbox\Eloquence\ModelIdentity;
use Sprocketbox\Eloquent\Identity\IdentityManager;
use Sprocketbox\Eloquent\Identity\ModelIdentity;

/**
* Eloquence Facade
* Identity Facade
*
* @method static IdentityManager getInstance()
* @method static bool hasIdentity(ModelIdentity $identity)
Expand All @@ -17,12 +17,12 @@
* @method static IdentityManager removeIdentity(ModelIdentity $identity)
* @method static array allIdentities()
*
* @package Sprocketbox\Eloquence\Facades
* @package Sprocketbox\Eloquent\Identity\Facades
*/
class Eloquence extends Facade
class Identity extends Facade
{
protected static function getFacadeAccessor(): string
{
return 'eloquence';
return 'eloquent.identity';
}
}
20 changes: 10 additions & 10 deletions src/IdentityManager.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php

namespace Sprocketbox\Eloquence;
namespace Sprocketbox\Eloquent\Identity;

use Illuminate\Database\Eloquent\Model;

/**
* Eloquence
*
* @package Sprocketbox\Eloquence
* @package Sprocketbox\Eloquent\Identity
*/
class IdentityManager
{
/**
* The current instance.
*
* @var \Sprocketbox\Eloquence\IdentityManager
* @var \Sprocketbox\Eloquent\Identity\IdentityManager
*/
protected static IdentityManager $instance;

/**
* Get the current Eloquence instance.
*
* @return \Sprocketbox\Eloquence\IdentityManager
* @return \Sprocketbox\Eloquent\Identity\IdentityManager
*/
public static function getInstance(): IdentityManager
{
Expand All @@ -42,7 +42,7 @@ public static function getInstance(): IdentityManager
/**
* Check if the identity is already stored.
*
* @param \Sprocketbox\Eloquence\ModelIdentity $identity
* @param \Sprocketbox\Eloquent\Identity\ModelIdentity $identity
*
* @return bool
*/
Expand All @@ -54,7 +54,7 @@ public function hasIdentity(ModelIdentity $identity): bool
/**
* Get the stored model for the given identity.
*
* @param \Sprocketbox\Eloquence\ModelIdentity $identity
* @param \Sprocketbox\Eloquent\Identity\ModelIdentity $identity
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
Expand All @@ -66,8 +66,8 @@ public function getIdentity(ModelIdentity $identity): ?Model
/**
* Set the model for the current identity.
*
* @param \Sprocketbox\Eloquence\ModelIdentity $identity
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Sprocketbox\Eloquent\Identity\ModelIdentity $identity
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return $this
*/
Expand All @@ -81,7 +81,7 @@ public function storeIdentity(ModelIdentity $identity, Model $model): IdentityMa
/**
* Remove a model from the identity map.
*
* @param \Sprocketbox\Eloquence\ModelIdentity $identity
* @param \Sprocketbox\Eloquent\Identity\ModelIdentity $identity
*
* @return $this
*/
Expand Down Expand Up @@ -119,7 +119,7 @@ public function allIdentities(): array
/**
* Turn the model identity into a string.
*
* @param \Sprocketbox\Eloquence\ModelIdentity $identity
* @param \Sprocketbox\Eloquent\Identity\ModelIdentity $identity
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ModelIdentity.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sprocketbox\Eloquence;
namespace Sprocketbox\Eloquent\Identity;

class ModelIdentity
{
Expand Down
12 changes: 6 additions & 6 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sprocketbox\Eloquence\Query;
namespace Sprocketbox\Eloquent\Identity\Query;

use Closure;
use Illuminate\Contracts\Support\Arrayable;
Expand All @@ -10,9 +10,9 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Str;
use Sprocketbox\Eloquence\Concerns\MapsIdentity;
use Sprocketbox\Eloquence\Facades\Eloquence;
use Sprocketbox\Eloquence\ModelIdentity;
use Sprocketbox\Eloquent\Identity\Concerns\MapsIdentity;
use Sprocketbox\Eloquent\Identity\Facades\Identity;
use Sprocketbox\Eloquent\Identity\ModelIdentity;

class Builder extends EloquentBuilder
{
Expand Down Expand Up @@ -130,8 +130,8 @@ protected function identifyModel($id): ?Model
$identity = $this->model->getModelIdentity($id, $this->getConnection()->getName());
}

if (Eloquence::hasIdentity($identity)) {
return Eloquence::getIdentity($identity);
if (Identity::hasIdentity($identity)) {
return Identity::getIdentity($identity);
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sprocketbox\Eloquence;
namespace Sprocketbox\Eloquent\Identity;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;

Expand All @@ -9,6 +9,6 @@ class ServiceProvider extends BaseServiceProvider
public function register(): void
{
$this->app->bind(IdentityManager::class, fn() => IdentityManager::getInstance(), true);
$this->app->alias(IdentityManager::class, 'eloquence');
$this->app->alias(IdentityManager::class, 'eloquent.identity');
}
}

0 comments on commit fe85ff2

Please sign in to comment.