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 committed Sep 1, 2020
1 parent e7dde4e commit 64e6658
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 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
35 changes: 28 additions & 7 deletions src/Repositories/ModuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use A17\Twill\Models\Model;
use A17\Twill\Models\Behaviors\Sortable;
use A17\Twill\Services\Capsules\HasCapsules;
use A17\Twill\Repositories\Behaviors\HandleBrowsers;
use A17\Twill\Repositories\Behaviors\HandleDates;
use A17\Twill\Repositories\Behaviors\HandleFieldsGroups;
Expand All @@ -19,7 +20,7 @@

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

/**
* @var \A17\Twill\Models\Model
Expand Down Expand Up @@ -839,20 +840,40 @@ 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 {
$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 64e6658

Please sign in to comment.