Repository URL to install this package:
|
Version:
1.0.0 ▾
|
<?php
namespace Modules\Core\Repositories;
use Modules\Core\Contracts\Repositories\CoreRepositoryInterface;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Helpers\CacheKeys;
use Prettus\Repository\Traits\CacheableRepository;
/**
* Class CoursesRepositoryEloquent
* @package namespace App\Repositories\Eloquent;
*/
abstract class CoreRepository extends BaseRepository implements CoreRepositoryInterface, CacheableInterface
{
use CacheableRepository;
/**
* @var \Eloquent;
*/
protected $model;
/**
* Boot up the repository, pushing criteria
*/
public function boot()
{
$this->pushCriteria(app(RequestCriteria::class));
}
public function select(array $select)
{
if (!$this->allowedCache('select') || $this->isSkippedCache()) {
return $this->_select($select);
}
$key = $this->getCacheKeyWithoutUrl('select', func_get_args());
$minutes = $this->getCacheMinutes();
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($select) {
return $this->_select($select);
});
return $value;
}
private function _select(array $select)
{
$this->applyCriteria();
$this->applyScope();
$result = $this->model->select($select)->get();
$this->resetModel();
return $result;
}
/**
* Get Cache key for the method
*
* @param $method
* @param $args
* @return string
*/
public function getCacheKeyWithoutUrl($method, $args = null)
{
$request = app('Illuminate\Http\Request');
$args = serialize($args);
$key = sprintf(
'%s@%s-%s',
get_called_class(),
$method,
md5($args . $request->url())
);
CacheKeys::putKey(get_called_class(), $key);
return $key;
}
public function findFirst($field, $value = null, $columns = array('*'))
{
$this->applyCriteria();
$this->applyScope();
if (!is_null($value)) {
$field = [$field => $value];
}
$model = $this->model->where($field)->first($columns);
$this->resetModel();
return $this->parserResult($model);
}
public function count(array $where = null)
{
if (!$this->allowedCache('count') || $this->isSkippedCache()) {
return $this->_count($where);
}
$key = $this->getCacheKey('count', func_get_args());
$minutes = $this->getCacheMinutes();
$value = $this->getCacheRepository()->remember($key, $minutes, function () use ($where) {
return $this->_count($where);
});
return $value;
}
public function _count(array $where = null)
{
$this->applyCriteria();
$this->applyScope();
if (!is_null($where)) {
$this->model->where($where);
}
$result = $this->model->count();
$this->resetModel();
return $result;
}
public function getModel()
{
return $this->model;
}
/**
* Retrieve all data of repository, paginated
* @param null $limit
* @param array $columns
* @param string $method
* @return mixed
*/
public function paginate($limit = null, $columns = array('*'), $method = "paginate")
{
$this->applyCriteria();
$this->applyScope();
$limit = (int)request()->get('perPage', is_null($limit) ? config('repository.pagination.limit', 15) : $limit);
/** @var \Illuminate\Pagination\AbstractPaginator $results */
$results = $this->model->{$method}($limit, $columns);
$results->appends(request()->query());
$this->resetModel();
return $this->parserResult($results);
}
}