Skip to content
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

Allow behaviors to load classes without being the main model of a capsule #2423

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
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
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);
}
}
21 changes: 3 additions & 18 deletions src/Models/Behaviors/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,17 @@
*
* @return object
*/
public function getSlugClass(): string
public function getSlugClass(): Model

Check warning on line 40 in src/Models/Behaviors/HasSlug.php

View check run for this annotation

Codecov / codecov/patch

src/Models/Behaviors/HasSlug.php#L40

Added line #L40 was not covered by tests
{
return new $this->getSlugModelClass();
return new ($this->getSlugModelClass());

Check warning on line 42 in src/Models/Behaviors/HasSlug.php

View check run for this annotation

Codecov / codecov/patch

src/Models/Behaviors/HasSlug.php#L42

Added line #L42 was not covered by tests
}

/**
* Returns the fully qualified slug class name for this model.
*/
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
26 changes: 26 additions & 0 deletions src/TwillCapsules.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use A17\Twill\Exceptions\NoCapsuleFoundException;
use A17\Twill\Helpers\Capsule;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use A17\Twill\Models\Contracts\TwillModelContract;
Expand Down Expand Up @@ -161,4 +162,29 @@
? app('autoloader')
: require base_path('vendor/autoload.php');
}

/** @return class-string<Model> */
public function guessRelatedModelClass(string $related, Model $model): string
{
$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.
$namespace . "\\{$related}s\\" . $relatedClass,
] as $possibleClass
) {
if (@class_exists($possibleClass)) {
return $possibleClass;
}
}

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

Check warning on line 188 in src/TwillCapsules.php

View check run for this annotation

Codecov / codecov/patch

src/TwillCapsules.php#L188

Added line #L188 was not covered by tests
}
}
Loading