Skip to content

Laravel 5.5 #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 17, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -74,7 +74,7 @@ Please observe and respect all aspects of the included Code of Conduct <https://

### Reporting Issues
When reporting issues, please fill out the included template as completely as
possible. Incomplete issues may be ignore or closed if there is not enough
possible. Incomplete issues may be ignored or closed if there is not enough
information included to be actionable.

### Submitting Pull Requests
141 changes: 141 additions & 0 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php namespace GeneaLabs\LaravelModelCaching;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;

class CacheKey
{
protected $eagerLoad;
protected $model;
protected $query;

public function __construct(array $eagerLoad, Model $model, Builder $query)
{
$this->eagerLoad = $eagerLoad;
$this->model = $model;
$this->query = $query;
}

public function make(array $columns = ['*'], $idColumn = null) : string
{
$key = $this->getModelSlug();
$key .= $this->getIdColumn($idColumn ?: '');
$key .= $this->getQueryColumns($columns);
$key .= $this->getWhereClauses();
$key .= $this->getWithModels();
$key .= $this->getOrderByClauses();
$key .= $this->getOffsetClause();
$key .= $this->getLimitClause();

return $key;
}

protected function getIdColumn(string $idColumn) : string
{
return $idColumn ? "_{$idColumn}" : '';
}

protected function getLimitClause() : string
{
if (! $this->query->limit) {
return '';
}

return "-limit_{$this->query->limit}";
}

protected function getModelSlug() : string
{
return str_slug(get_class($this->model));
}

protected function getOffsetClause() : string
{
if (! $this->query->offset) {
return '';
}

return "-offset_{$this->query->offset}";
}

protected function getOrderByClauses() : string
{
$orders = collect($this->query->orders);

return $orders->reduce(function($carry, $order){
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
})
?: '';
}

protected function getQueryColumns(array $columns) : string
{
if ($columns === ['*'] || $columns === []) {
return '';
}

return '_' . implode('_', $columns);
}

protected function getTypeClause($where) : string
{
return in_array($where['type'], ['In', 'Null', 'NotNull'])
? strtolower($where['type'])
: '';
}

protected function getValuesClause(array $where = null) : string
{
return is_array(array_get($where, 'values'))
? '_' . implode('_', $where['values'])
: '';
}

protected function getWhereClauses(array $wheres = []) : string
{
return $this->getWheres($wheres)
->reduce(function ($carry, $where) {
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
}

if ($where['type'] === 'Column') {
return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
}

if ($where['type'] === 'raw') {
return "_{$where['boolean']}_" . str_slug($where['sql']);
}

$value = array_get($where, 'value');
$value .= $this->getTypeClause($where);
$value .= $this->getValuesClause($where);

return "{$carry}-{$where['column']}_{$value}";
})
. '';
}

protected function getWheres(array $wheres) : Collection
{
$wheres = collect($wheres);

if ($wheres->isEmpty()) {
$wheres = collect($this->query->wheres);
}

return $wheres;
}

protected function getWithModels() : string
{
$eagerLoads = collect($this->eagerLoad);

if ($eagerLoads->isEmpty()) {
return '';
}

return '-' . implode('-', $eagerLoads->keys()->toArray());
}
}
42 changes: 42 additions & 0 deletions src/CacheTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php namespace GeneaLabs\LaravelModelCaching;

use GeneaLabs\LaravelModelCaching\CachedBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

class CacheTags
{
protected $eagerLoad;
protected $model;

public function __construct(array $eagerLoad, Model $model)
{
$this->eagerLoad = $eagerLoad;
$this->model = $model;
}

public function make() : array
{
return collect($this->eagerLoad)
->keys()
->map(function ($relationName) {
$relation = collect(explode('.', $relationName))
->reduce(function ($carry, $name) {
if (! $carry) {
$carry = $this->model;
}

if ($carry instanceof Relation) {
$carry = $carry->getQuery()->getModel();
}

return $carry->{$name}();
});

return str_slug(get_class($relation->getQuery()->getModel()));
})
->prepend(str_slug(get_class($this->model)))
->values()
->toArray();
}
}
Loading