Skip to content

Commit

Permalink
Add support for Globals
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Aug 29, 2020
1 parent 04ced7b commit f447275
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 58 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

![Statamic](https://flat.badgen.net/badge/Statamic/3.0+/FF269E)

> This addon provides an easy way to quickly whip up fake `Collection Entries` and `Taxonomy Terms` using [Faker](https://github.com/fzaninotto/Faker).
> This addon provides an easy way to quickly whip up fake data for your `Collection Entries`, `Taxonomy Terms` and `Globals` using [Faker](https://github.com/fzaninotto/Faker).
## Documentation

Expand Down
84 changes: 32 additions & 52 deletions src/Commands/RunFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function handle(): void
{
$contentType = $this->choice(
'Choose the type of content you want to create',
['Collection Entry', 'Taxonomy Term']
['Collection Entry', 'Taxonomy Term', 'Global']
);

// if ($contentType === 'Asset' && $this->hasAssetContainers()) {
Expand All @@ -81,11 +81,11 @@ public function handle(): void
);
}

// if ($contentType === 'Global' && $this->hasGlobals()) {
// $contentHandle = $this->choice('Choose a global set', $this->globals());
// $blueprintHandle = $this->globalBlueprint($contentHandle);
// $amount = 1;
// }
if ($contentType === 'Global') {
$contentHandle = $this->choice('Choose a global set', $this->globals());
$blueprintHandle = collect($this->blueprints('globals'))->search($contentHandle);
$amount = 1;
}

if ($contentType === 'Taxonomy Term') {
$contentHandle = $this->choice('Choose a taxonomy', $this->taxonomies());
Expand Down Expand Up @@ -124,12 +124,12 @@ protected function runFactory(string $contentType, string $contentHandle, string
*
* @return array
*/
protected function assetContainers(): array
{
return AssetContainer::all()->map(function ($container) {
return $container->handle();
})->toArray();
}
// protected function assetContainers(): array
// {
// return AssetContainer::all()->map(function ($container) {
// return $container->handle();
// })->toArray();
// }

/**
* Get the available collection handles.
Expand All @@ -154,9 +154,15 @@ protected function collections(): array
*/
protected function globals(): array
{
return GlobalSet::all()->map(function ($container) {
$globals = GlobalSet::all()->map(function ($container) {
return $container->handle();
})->toArray();
})->all();

if (empty($globals)) {
$this->error('You have no globals. Create at least one global set to use the factory.');
}

return $globals;
}

/**
Expand All @@ -180,10 +186,10 @@ protected function taxonomies(): array
*
* @return string
*/
protected function assetBlueprint(string $contentHandle): string
{
return AssetContainer::find($contentHandle)->blueprint();
}
// protected function assetBlueprint(string $contentHandle): string
// {
// return AssetContainer::find($contentHandle)->blueprint();
// }

/**
* Get blueprint handles
Expand All @@ -201,47 +207,21 @@ protected function blueprints(string $path): array
return $blueprints;
}

/**
* Get the blueprint handle of a global set.
*
* @return string
*/
protected function globalBlueprint(string $contentHandle): string
{
return GlobalSet::find($contentHandle)->blueprint();
}

/**
* Check if there's any asset containers.
*
* @return bool
*/
protected function hasAssetContainers(): bool
{
if (empty($this->assetContainers())) {
$this->error('You have no asset containers. Create at least one asset container to use the factory.');

return false;
}

return true;
}

/**
* Check if there's any global sets.
*
* @return bool
*/
protected function hasGlobals(): bool
{
if (empty($this->globals())) {
$this->error('You have no globals. Create at least one global set to use the factory.');
// protected function hasAssetContainers(): bool
// {
// if (empty($this->assetContainers())) {
// $this->error('You have no asset containers. Create at least one asset container to use the factory.');

return false;
}
// return false;
// }

return true;
}
// return true;
// }

/**
* Validate the answer of a question.
Expand Down
13 changes: 8 additions & 5 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ protected function blueprint(): \Statamic\Fields\Blueprint
if ($this->contentType === 'Taxonomy Term') {
return Blueprint::find("taxonomies/{$this->contentHandle}/{$this->blueprintHandle}");
}

if ($this->contentType === 'Global') {
return Blueprint::find("globals/{$this->contentHandle}");
}
}

/**
Expand Down Expand Up @@ -270,17 +274,16 @@ protected function makeEntry(int $amount): void
}

/**
* TODO: Figure out how to save data to a Global Set.
*
* Fill the global set with fake data.
*
* @return void
*/
protected function makeGlobal(): void
{
$fakeData = $this->fakeData();

dd(GlobalSet::find($this->contentHandle)->fileData());
GlobalSet::findByHandle($this->contentHandle)
->inDefaultSite()
->data($this->fakeData())
->save();
}

/**
Expand Down

0 comments on commit f447275

Please sign in to comment.