Skip to content

Commit

Permalink
Add Importer command and implement getBlueprints method
Browse files Browse the repository at this point in the history
  • Loading branch information
tdwesten committed Feb 7, 2024
1 parent 118c4e4 commit cba2dab
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 8 deletions.
28 changes: 28 additions & 0 deletions src/Console/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,35 @@
namespace Tdwesten\StatamicBuilder\Console;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Statamic\Filesystem\FlysystemAdapter;

class Importer extends Command
{
protected $signature = 'statamic-builder:import';

protected $description = 'Import YAML files to Statamic blueprints and fieldsets';

protected $name = 'Statamic Builder Importer';

public function handle()
{
$this->info('Importing blueprints and fieldsets...');

$this->getBlueprints();
}

private function getBlueprints()
{
$filesystem = Storage::build([
'driver' => 'local',
'root' => base_path('resources/blueprints'),
]);

$fs = new FlysystemAdapter($filesystem);

$files = $fs->getFilesRecursively('/');

dd($files);
}
}
92 changes: 92 additions & 0 deletions src/Contracts/ConditionalLogic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Tdwesten\StatamicBuilder\Contracts;

use Tdwesten\StatamicBuilder\Enums\OperatorOption;

trait ConditionalLogic
{
protected $if;

protected $if_any;

protected $unless;

protected $unless_any;

public function if(string $key, OperatorOption $oparator, $value)
{
if ($this->if === null) {
$this->if = collect();
}

$this->if->put($key, "{$oparator->value} {$value}");

return $this;
}

public function ifCustom(string $custom)
{
if ($this->if === null) {
$this->if = collect();
}

$this->if->put('custom', $custom);

return $this;
}

public function ifAny(string $key, OperatorOption $oparator, $value)
{
if ($this->if_any === null) {
$this->if_any = collect();
}

$this->if_any->put($key, "{$oparator->value} {$value}");

return $this;
}

public function ifAnyCustom(string $custom)
{
if ($this->if_any === null) {
$this->if_any = collect();
}

$this->if_any->put('custom', $custom);

return $this;
}

public function unless(string $key, OperatorOption $oparator, $value)
{
if ($this->unless === null) {
$this->unless = collect();
}

$this->unless->put($key, "{$oparator->value} {$value}");

return $this;
}

public function unlessCustom(string $custom)
{
if ($this->unless === null) {
$this->unless = collect();
}

$this->unless->put('custom', $custom);

return $this;
}

public function conditionalLogicToArray()
{
return [
'if' => $this->if?->toArray(),
'if_any' => $this->if_any?->toArray(),
'unless' => $this->unless?->toArray(),
'unless_any' => $this->unless_any?->toArray(),
];
}
}
18 changes: 18 additions & 0 deletions src/Enums/OperatorOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Tdwesten\StatamicBuilder\Enums;

enum OperatorOption: string
{
case Equals = 'equals';
case Not = 'not';
case Contains = 'contains';
case ContainsAny = 'contains_any';
case IsIdentical = '===';
case IsNotIdentical = '!==';
case GreaterThan = '>';
case GreaterThanOrEqual = '>=';
case LessThan = '<';
case LessThanOrEqual = '<=';
case Custom = 'custom';
}
3 changes: 3 additions & 0 deletions src/FieldTypes/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Tdwesten\StatamicBuilder\FieldTypes;

use Illuminate\Support\Collection;
use Tdwesten\StatamicBuilder\Contracts\ConditionalLogic;
use Tdwesten\StatamicBuilder\Contracts\Makeble;
use Tdwesten\StatamicBuilder\Contracts\Renderable;
use Tdwesten\StatamicBuilder\Enums\VisibilityOption;

class Field implements Renderable
{
use ConditionalLogic;
use Makeble;

protected $handle;
Expand Down Expand Up @@ -63,6 +65,7 @@ public function toArray()

$field = $fieldDefaults
->merge($this->fieldToArray())
->merge($this->conditionalLogicToArray())
->merge($this->customAttributes ?? []);

$content = collect([
Expand Down
8 changes: 0 additions & 8 deletions src/Importer.php
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
<?php

namespace Tdwesten\StatamicBuilder;

class Importer
{

}
1 change: 1 addition & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function boot()
$this->commands([
Console\MakeBlueprint::class,
Console\MakeFieldset::class,
Console\Importer::class,
]);
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Unit/ConditionalLogicTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Tdwesten\StatamicBuilder\Enums\OperatorOption;

test('Can set conditional logic if', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field
->type('text')
->if('title', OperatorOption::Equals, 'red');

expect($field->toArray()['field']['if'])->toBe([
'title' => 'equals red',
]);
});

test('Can set conditional logic if with multiple conditions', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field
->type('text')
->if('title', OperatorOption::Equals, 'red')
->if('short_title', OperatorOption::IsIdentical, 'blue');

expect($field->toArray()['field']['if'])->toBe([
'title' => 'equals red',
'short_title' => '=== blue',
]);
});

test('Can set conditional logic if any', function () {
$field = new \Tdwesten\StatamicBuilder\FieldTypes\Field('title');
$field
->type('text')
->ifAny('title', OperatorOption::Equals, 'red');

expect($field->toArray()['field']['if_any'])->toBe([
'title' => 'equals red',
]);
});

0 comments on commit cba2dab

Please sign in to comment.