⚠️ [ABANDONED] Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Lar
⚠️ [ABANDONED] Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Lar
⚠️ This package is abandoned and no longer maintained. No replacement package was suggested. ⚠️
Contact me if you are interested in maintaining it!
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
If you are looking for Laravel 5.5 support, use the dev-develop branch. It's stable but not tagged yet since test suites isn't complete.
The Rinvex\Repository\Repositories\BaseRepository is an abstract class with bare minimum that concrete implementations must extend.
The Rinvex\Repository\Repositories\EloquentRepository is currently the only available repository implementation (more to come in the future and you can develop your own), it makes it easy to create new eloquent model instances and to manipulate them easily. To use EloquentRepository your repository MUST extend it first:
namespace App\Repositories;
use Rinvex\Repository\Repositories\EloquentRepository;
class FooRepository extends EloquentRepository
{
protected $repositoryId = 'rinvex.repository.uniqueid';
protected $model = 'App\Models\User';
}That's it, you're done! Yes, it's that simple.
But if you'd like more control over the container instance, or would like to pass model name dynamically you can alternatively do as follow:
namespace App\Repositories;
use Illuminate\Contracts\Container\Container;
use Rinvex\Repository\Repositories\EloquentRepository;
class FooRepository extends EloquentRepository
{
// Instantiate repository object with required data
public function __construct(Container $container)
{
$this->setContainer($container)
->setModel(\App\Models\User::class)
->setRepositoryId('rinvex.repository.uniqueid');
}
}Now inside your controller, you can either instantiate the repository traditionally through $repository = new \App\Repositories\FooRepository(); or to use Laravel's awesome dependency injection and let the IoC do the magic:
namespace App\Http\Controllers;
use App\Repositories\FooRepository;
class BarController
{
// Inject `FooRepository` from the IoC
public function baz(FooRepository $repository)
{
// Find entity by primary key
$repository->find(1);
// Find all entities
$repository->findAll();
// Create a new entity
$repository->create(['name' => 'Example']);
}
}Rinvex Repository Workflow - Create Repository
Rinvex Repository Workflow - Use In Controller
Mission accomplished! You're good to use this package right now! :white_check_mark:
Unless you need to dig deeper & know some advanced stuff, you can skip the following steps! :wink:
setContainer(), getContainer()setConnection(), getConnection()setModel(), getModel()setRepositoryId(), getRepositoryId()setCacheLifetime(), getCacheLifetime()setCacheDriver(), getCacheDriver()enableCacheClear(), isCacheClearEnabled()createModel()forgetCache()with()where()whereIn()whereNotIn()whereHas()offset()limit()orderBy()find()findBy()findFirst()findAll()paginate()simplePaginate()findWhere()findWhereIn()findWhereNotIn()findWhereHas()create()update()store()delete()beginTransaction()commit()rollBack()The best and easiest way to install this package is through Composer.
This package fully compatible with Laravel 5.1.*, 5.2.*, and 5.3.*.
While this package tends to be framework-agnostic, it embraces Laravel culture and best practices to some extent. It's tested mainly with Laravel but you still can use it with other frameworks or even without any framework if you want.
Open your application's composer.json file and add the following line to the require array:
"rinvex/laravel-repositories": "3.0.*"Note: Make sure that after the required changes your
composer.jsonfile is valid by runningcomposer validate.
On your terminal run composer install or composer update command according to your application's status to install the new requirements.
Note: Checkout Composer's Basic Usage documentation for further details.
Rinvex Repository package is framework-agnostic and as such can be integrated easily natively or with your favorite framework.
Integrating the package outside of a framework is incredibly easy, just require the vendor/autoload.php file to autoload the package.
Note: Checkout Composer's Autoloading documentation for further details.
Run the following command on your terminal to publish config files:
php artisan vendor:publish --tag="rinvex-repository-config"Note: Checkout Laravel's Configuration documentation for further details.
You are good to go. Integration is done and you can now use all the available methods, proceed to the Usage section for an example.
If you followed the previous integration steps, then your published config file reside at config/rinvex.repository.php.
Config options are very expressive and self explanatory, as follows:
…setContainer(), getContainer()The setContainer method sets the IoC container instance, while getContainer returns it:
// Set the IoC container instance
$repository->setContainer(new \Illuminate\Container\Container());
// Get the IoC container instance
$container = $repository->getContainer();setConnection(), getConnection()The setConnection method sets the connection associated with the repository, while getConnection returns it:
// Set the connection associated with the repository
$repository->setConnection('mysql');
// Get the current connection for the repository
$connection = $repository->getConnection();Note: The name passed to the
setConnectionmethod should correspond to one of the connections listed in yourconfig/database.phpconfiguration file.
setModel(), getModel()The setModel method sets the repository model, while getModel returns it:
// Set the repository model
$repository->setModel(\App\Models\User::class);
// Get the repository model
$repositoryModel = $repository->getModel();setRepositoryId(), getRepositoryId()The setRepositoryId method sets the repository identifier, while getRepositoryId returns it (it could be anything you want, but must be unique per repository):
// Set the repository identifier
$repository->setRepositoryId('rinvex.repository.uniqueid');
// Get the repository identifier
$repositoryId = $repository->getRepositoryId();setCacheLifetime(), getCacheLifetime()The setCacheLifetime method sets the repository cache lifetime, while getCacheLifetime returns it:
// Set the repository cache lifetime
$repository->setCacheLifetime(123);
// Get the repository cache lifetime
$cacheLifetime = $repository->getCacheLifetime();setCacheDriver(), getCacheDriver()The setCacheDriver method sets the repository cache driver, while getCacheDriver returns it:
// Set the repository cache driver
$repository->setCacheDriver('redis');
// Get the repository cache driver
$cacheDriver = $repository->getCacheDriver();enableCacheClear(), isCacheClearEnabled()The enableCacheClear method enables repository cache clear, while isCacheClearEnabled determines it's state:
// Enable repository cache clear
$repository->enableCacheClear(true);
// Disable repository cache clear
$repository->enableCacheClear(false);
// Determine if repository cache clear is enabled
$cacheClearStatus = $repository->isCacheClearEnabled();createModel()The createModel() method creates a new repository model instance:
$repositoryModelInstance = $repository->createModel();forgetCache()The forgetCache() method forgets the repository cache:
$repository->forgetCache();with()The with method sets the relationships that should be eager loaded:
// Pass a string
$repository->with('relationship');
// Or an array
$repository->with(['relationship1', 'relationship2']);where()The where method adds a basic where clause to the query:
$repository->wherNo open issues yet, or sync has not completed.