Skip to content

Commit

Permalink
Allow passing the repository directly to the updater/getter
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro authored and ifox committed Mar 29, 2021
1 parent bca6355 commit 3bdc81f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/Repositories/Behaviors/HandleRepeaters.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ public function updateRepeaterMorphMany($object, $fields, $relation, $morph = nu
*
* @return void
*/
public function updateRepeater($object, $fields, $relation, $model = null, $repeaterName = null)
public function updateRepeater($object, $fields, $relation, $modelOrRepository = null, $repeaterName = null)
{
if (!$repeaterName) {
$repeaterName = $relation;
}

$relationFields = $fields['repeaters'][$repeaterName] ?? [];

$relationRepository = $this->getModelRepository($relation, $model);
$relationRepository = $this->getModelRepository($relation, $modelOrRepository);

// if no relation field submitted, soft deletes all associated rows
if (!$relationFields) {
Expand Down Expand Up @@ -194,12 +194,12 @@ public function updateRepeater($object, $fields, $relation, $model = null, $repe
* @param object $object
* @param array $fields
* @param string $relation
* @param string $model
* @param string $modelOrRepository
* @param string $repeaterName
*
* @return array
*/
public function getFormFieldsForRepeater($object, $fields, $relation, $model = null, $repeaterName = null)
public function getFormFieldsForRepeater($object, $fields, $relation, $modelOrRepository = null, $repeaterName = null)
{
if (!$repeaterName) {
$repeaterName = $relation;
Expand All @@ -210,7 +210,7 @@ public function getFormFieldsForRepeater($object, $fields, $relation, $model = n
$repeatersBrowsers = [];
$repeatersMedias = [];
$repeatersFiles = [];
$relationRepository = $this->getModelRepository($relation, $model);
$relationRepository = $this->getModelRepository($relation, $modelOrRepository);
$repeatersList = app(BlockCollection::class)->getRepeaterList()->keyBy('name');

foreach ($object->$relation as $relationItem) {
Expand Down
44 changes: 32 additions & 12 deletions src/Repositories/ModuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@

namespace A17\Twill\Repositories;

use A17\Twill\Models\Model;
use A17\Twill\Models\Behaviors\Sortable;
use A17\Twill\Models\Model;
use A17\Twill\Repositories\Behaviors\HandleBrowsers;
use A17\Twill\Repositories\Behaviors\HandleDates;
use A17\Twill\Repositories\Behaviors\HandleFieldsGroups;
use A17\Twill\Repositories\Behaviors\HandleRepeaters;
use A17\Twill\Services\Capsules\HasCapsules;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Relations\Relation;

use PDO;

abstract class ModuleRepository
{
use HandleDates, HandleBrowsers, HandleRepeaters, HandleFieldsGroups;
use HandleDates, HandleBrowsers, HandleRepeaters, HandleFieldsGroups, HasCapsules;

/**
* @var \A17\Twill\Models\Model
Expand Down Expand Up @@ -361,7 +361,7 @@ public function duplicate($id, $titleColumnKey = 'title')
$baseInput = collect($revisionInput)->only([
$titleColumnKey,
'slug',
'languages'
'languages',
])->filter()->toArray();

$newObject = $this->create($baseInput);
Expand Down Expand Up @@ -841,25 +841,45 @@ public function getReservedFields()

/**
* @param string $relation
* @param \A17\Twill\Models\Model|null $model
* @param \A17\Twill\Models\Model|\A17\Twill\Repositories\ModuleRepository|null $modelOrRepository
* @return mixed
*/
protected function getModelRepository($relation, $model = null)
protected function getModelRepository($relation, $modelOrRepository = null)
{
if (!$model) {
if (!$modelOrRepository) {
if (class_exists($relation) && (new $relation) instanceof Model) {
$model = Str::afterLast($relation, '\\');
$modelOrRepository = Str::afterLast($relation, '\\');
} else {
$morphedModel = Relation::getMorphedModel($relation);
if (class_exists($morphedModel) && (new $morphedModel) instanceof Model) {
$model = (new \ReflectionClass($morphedModel))->getShortName();
$modelOrRepository = (new \ReflectionClass($morphedModel))->getShortName();
} else {
$model = ucfirst(Str::singular($relation));
$modelOrRepository = ucfirst(Str::singular($relation));
}
}
}

return App::make(Config::get('twill.namespace') . "\\Repositories\\" . ucfirst($model) . "Repository");
$repository = class_exists($modelOrRepository)
? App::make($modelOrRepository)
: $modelOrRepository;

if ($repository instanceof ModuleRepository) {
return $repository;
} else {
$class = Config::get('twill.namespace') . "\\Repositories\\" . ucfirst($modelOrRepository) . "Repository";
}

if (class_exists($class)) {
return App::make($class);
}

$capsule = $this->getCapsuleByModel($modelOrRepository);

if (blank($capsule)) {
throw new \Exception("Repository class not found for model '{$modelOrRepository}'");
}

return App::make($capsule['repository']);
}

/**
Expand Down

0 comments on commit 3bdc81f

Please sign in to comment.