Skip to content

Commit

Permalink
Use different approach for localization
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Sep 2, 2024
1 parent 4ae3d1b commit 7be8fdf
Showing 1 changed file with 38 additions and 49 deletions.
87 changes: 38 additions & 49 deletions src/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ abstract class Factory

protected Generator $faker;

protected ?Sequence $siteSequence = null;

public function __construct(
protected ?int $count = null,
protected ?string $site = null,
protected ?Collection $states = null,
protected ?Collection $afterMaking = null,
protected ?Collection $afterCreating = null,
protected ?Collection $recycle = null,
protected bool $randomSite = false,
) {
$this->site ??= $this->getDefaultSiteFromContentModel();
$this->states ??= new Collection;
$this->afterMaking ??= new Collection;
$this->afterCreating ??= new Collection;
Expand Down Expand Up @@ -64,11 +67,11 @@ public function configure(): self
public function raw($attributes = []): array
{
if ($this->count === null) {
return $this->state($attributes)->getExpandedAttributes();
return $this->evaluateSite()->state($attributes)->getExpandedAttributes();
}

return array_map(function () use ($attributes) {
return $this->state($attributes)->getExpandedAttributes();
return $this->evaluateSite()->state($attributes)->getExpandedAttributes();
}, range(1, $this->count));
}

Expand All @@ -84,7 +87,7 @@ public function make(array $attributes = []): Collection|Entry|Term
}

if ($this->count === null) {
return tap($this->state($attributes)->makeInstance(), function ($instance) {
return tap($this->evaluateSite()->state($attributes)->makeInstance(), function ($instance) {
$this->callAfterMaking(collect([$instance]));
});
}
Expand All @@ -94,7 +97,7 @@ public function make(array $attributes = []): Collection|Entry|Term
}

$instances = collect(array_map(function () use ($attributes) {
return $this->state($attributes)->makeInstance();
return $this->evaluateSite()->state($attributes)->makeInstance();
}, range(1, $this->count)));

$this->callAfterMaking($instances);
Expand Down Expand Up @@ -165,7 +168,6 @@ protected function getExpandedAttributes(): array
protected function getRawAttributes(): array
{
return $this->states
->pipe($this->getSiteFromStatesAndSetFakerLocaleAccordingly(...))
->reduce(function (array $carry, $state) {
if ($state instanceof Closure) {
$state = $state->bindTo($this);
Expand All @@ -175,31 +177,6 @@ protected function getRawAttributes(): array
}, $this->definition());
}

protected function getSiteFromStatesAndSetFakerLocaleAccordingly(Collection $states): Collection
{
$evaluatedClosure = $states
->map(fn ($state) => (clone $state)()) /* Clone the closure so that we don't run into issues when evaluating the same closure later. Needed for sequences to work correctly. */
->filter(fn ($state) => isset($state['site']))
->flatMap(fn ($state, $index) => array_merge(['index' => $index], $state));

$site = $evaluatedClosure->get('site');

/**
* This should only be applied when using "random" as the site.
* Replace the unevaluated site closure with one that contains the evaluated site.
* This ensures that we'll save the content in the same site that we are using for Faker.
*/
if ($this->randomSite) {
$states = $states->put($evaluatedClosure->get('index'), fn () => ['site' => $site]);
}

$locale = Site::get($site)?->locale() ?? Site::default()->locale();

$this->faker = $this->withFaker($locale);

return $states;
}

protected function expandAttributes(array $definition)
{
return collect($definition)
Expand Down Expand Up @@ -263,17 +240,18 @@ public function count(?int $count): self

public function site(string $site): self
{
if ($site === 'random') {
$this->randomSite = true;
}
return $this->newInstance(['site' => $site]);
}

return match ($site) {
'sequence' => $this->state(new Sequence(
... $this->getSitesFromContentModel()->map(fn ($site) => ['site' => $site])->all()
)),
'random' => $this->sequence(fn (Sequence $sequence) => ['site' => $this->getSitesFromContentModel()->random()]),
default => $this->set('site', $site),
};
// TODO: Ensure we are applying this correctly wherever necessary.
protected function evaluateSite(): self
{
return $this->site(match (true) {
$this->getSitesFromContentModel()->contains($this->site) => $this->site,
$this->site === 'random' => $this->getSitesFromContentModel()->random(),
$this->site === 'sequence' => once(fn () => new Sequence(... $this->getSitesFromContentModel()))(),
default => $this->getDefaultSiteFromContentModel(),
});
}

public function published(bool|string $published): self
Expand All @@ -295,6 +273,16 @@ protected function getSitesFromContentModel(): Collection
};
}

protected function getDefaultSiteFromContentModel(): string
{
$contentModel = $this->newContentModel();

return match (true) {
$contentModel instanceof Entry => $contentModel->locale(),
$contentModel instanceof Term => $contentModel->defaultLocale(),
};
}

public function recycle($model)
{
return $this->newInstance([
Expand Down Expand Up @@ -345,11 +333,11 @@ protected function newInstance(array $arguments = []): self
{
return new static(...array_values(array_merge([
'count' => $this->count,
'site' => $this->site,
'states' => $this->states,
'afterMaking' => $this->afterMaking,
'afterCreating' => $this->afterCreating,
'recycle' => $this->recycle,
'randomSite' => $this->randomSite,
], $arguments)));
}

Expand All @@ -372,8 +360,8 @@ protected function newEntry(array $attributes = []): Entry
$entry->slug($slug);
}

if (($site = Arr::pull($attributes, 'site')) && $entry->sites()->contains($site)) {
$entry->locale($site);
if ($entry->sites()->contains($this->site)) {
$entry->locale($this->site);
}

$entry->published(Arr::pull($attributes, 'published', true));
Expand All @@ -387,15 +375,14 @@ protected function newTerm(array $attributes = []): Term
->taxonomy($this->contentModelHandle())
->blueprint($this->contentModelBlueprint());

$site = Arr::pull($attributes, 'site');
$published = Arr::pull($attributes, 'published', true);
$slug = Arr::pull($attributes, 'slug');

/**
* If the term is *not* being created in the default site, we'll copy all the
* appropriate values into the default localization since it needs to exist.
*/
if ($site !== $term->defaultLocale()) {
if ($this->site !== $term->defaultLocale()) {
$term
->inDefaultLocale()
->published($published)
Expand All @@ -404,9 +391,9 @@ protected function newTerm(array $attributes = []): Term
}

/* Ensure we only create localizations for sites that are configured on the taxonomy. */
if ($term->taxonomy()->sites()->contains($site)) {
if ($term->taxonomy()->sites()->contains($this->site)) {
$term
->in($site)
->in($this->site)
->published($published)
->slug($slug)
->data($attributes);
Expand All @@ -415,8 +402,10 @@ protected function newTerm(array $attributes = []): Term
return $term;
}

protected function withFaker(?string $locale = null): Generator
protected function withFaker(): Generator
{
$locale = Site::get($this->site)?->locale() ?? Site::default()->locale();

return Container::getInstance()->makeWith(Generator::class, ['locale' => $locale]);
}

Expand Down

0 comments on commit 7be8fdf

Please sign in to comment.