Skip to content

Commit

Permalink
Allow behaviors to load classes without being the main model of a cap…
Browse files Browse the repository at this point in the history
…sule
  • Loading branch information
Tofandel committed Feb 6, 2024
1 parent 8a5503d commit 53bf593
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/Helpers/Capsule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use A17\Twill\Http\Controllers\Admin\SingletonModuleController;
use A17\Twill\View\Components\Navigation\NavigationLink;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
Expand Down
8 changes: 1 addition & 7 deletions src/Models/Behaviors/HasRevisions.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ public function deleteSpecificRevisions(int $maxRevisions): void

protected function getRevisionModel(): string
{
$revision = config('twill.namespace') . "\Models\Revisions\\" . class_basename($this) . "Revision";

if (@class_exists($revision)) {
return $revision;
}

return TwillCapsules::getCapsuleForModel(class_basename($this))->getRevisionModel();
return TwillCapsules::guessRelatedModelClass('Revision', $this);
}
}
17 changes: 1 addition & 16 deletions src/Models/Behaviors/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,7 @@ public function getSlugClass(): string
*/
public function getSlugModelClass(): string
{
// First load it from the base directory.
$slug = config('twill.namespace') . "\\Models\\Slugs\\" . $this->getSlugClassName();

if (@class_exists($slug)) {
return $slug;
}

// Alternatively try to get it from the same directory as the model resides in (nested directory models).
$slug = $this->getNamespace() . "\Slugs\\" . $this->getSlugClassName();

if (@class_exists($slug)) {
return $slug;
}

// Finally try to get it from capsules.
return TwillCapsules::getCapsuleForModel(class_basename($this))->getSlugModel();
return TwillCapsules::guessRelatedModelClass('Slug', $this);
}

protected function getSlugClassName(): string
Expand Down
12 changes: 2 additions & 10 deletions src/Models/Behaviors/HasTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,10 @@ trait HasTranslation

/**
* Returns the fully qualified translation class name for this model.
*
* @return string|null
*/
public function getTranslationModelNameDefault()
public function getTranslationModelNameDefault(): string
{
$repository = config('twill.namespace') . "\Models\Translations\\" . class_basename($this) . 'Translation';

if (@class_exists($repository)) {
return $repository;
}

return TwillCapsules::getCapsuleForModel(class_basename($this))->getTranslationModel();
return TwillCapsules::guessRelatedModelClass('Translation', $this);
}

public function scopeWithActiveTranslations(Builder $query, ?string $locale = null): Builder
Expand Down
23 changes: 23 additions & 0 deletions src/TwillCapsules.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,27 @@ public function getAutoloader()
? app('autoloader')
: require base_path('vendor/autoload.php');
}

/** @return class-string<Model> */
public function guessRelatedModelClass(string $related, Model $model): string

Check failure on line 166 in src/TwillCapsules.php

View workflow job for this annotation

GitHub Actions / lint

Parameter $model of method A17\Twill\TwillCapsules::guessRelatedModelClass() has invalid type A17\Twill\Model.
{
$rc = new \ReflectionClass($model);
$namespace = $rc->getNamespaceName();
$capsule = $rc->getShortName();;
$relatedClass = $capsule . $related;
foreach ([
// First load it from the base directory.
config('twill.namespace') . "\\Models\\{$related}s\\" . $relatedClass,
// Alternatively try to get it from the same directory as the model resides
$rc->getName().$related,
// Or in nested directory models.
$model->getNamespace() . "\\{$related}s\\" . $relatedClass
] as $possibleClass) {
if (@class_exists($possibleClass)) {
return $possibleClass;
}
}

return call_user_func([$this->getCapsuleForModel($capsule), 'get'.$related.'Model']);
}
}

0 comments on commit 53bf593

Please sign in to comment.