Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor
.php_cs.cache
.nova/*
.phpunit.cache
.idea
8 changes: 6 additions & 2 deletions config/eloquent-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

'blueprints' => [
'driver' => 'file',
'blueprint_model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
'fieldset_model' => \Statamic\Eloquent\Fields\FieldsetModel::class,
'model' => \Statamic\Eloquent\Fields\BlueprintModel::class,
'namespaces' => 'all',
],

Expand All @@ -42,6 +41,11 @@
'entry' => \Statamic\Eloquent\Entries\Entry::class,
],

'fieldsets' => [
'driver' => 'file',
'model' => \Statamic\Eloquent\Fields\FieldsetModel::class,
],

'forms' => [
'driver' => 'file',
'model' => \Statamic\Eloquent\Forms\FormModel::class,
Expand Down
31 changes: 28 additions & 3 deletions src/Commands/ImportBlueprints.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class ImportBlueprints extends Command
*
* @var string
*/
protected $signature = 'statamic:eloquent:import-blueprints';
protected $signature = 'statamic:eloquent:import-blueprints
{--force : Force the import to run, with all prompts answered "yes"}
{--only-blueprints : Only import blueprints}
{--only-fieldsets : Only import fieldsets}';

/**
* The console command description.
Expand Down Expand Up @@ -62,6 +65,10 @@ private function useDefaultRepositories(): void

private function importBlueprints(): void
{
if (! $this->shouldImportBlueprints()) {
return;
}

$directory = resource_path('blueprints');

$files = File::withAbsolutePaths()
Expand Down Expand Up @@ -105,7 +112,7 @@ private function importBlueprints(): void

$lastModified = Carbon::createFromTimestamp(File::lastModified($path));

app('statamic.eloquent.blueprints.blueprint_model')::firstOrNew([
app('statamic.eloquent.blueprints.model')::firstOrNew([
'handle' => $blueprint->handle(),
'namespace' => $blueprint->namespace() ?? null,
])
Expand All @@ -118,6 +125,10 @@ private function importBlueprints(): void

private function importFieldsets(): void
{
if (! $this->shouldImportFieldsets()) {
return;
}

$directory = resource_path('fieldsets');

$files = File::withAbsolutePaths()
Expand All @@ -132,7 +143,7 @@ private function importFieldsets(): void

$lastModified = Carbon::createFromTimestamp(File::lastModified($path));

app('statamic.eloquent.blueprints.fieldset_model')::firstOrNew([
app('statamic.eloquent.fieldsets.model')::firstOrNew([
'handle' => $fieldset->handle(),
])
->fill(['data' => $fieldset->contents(), 'created_at' => $lastModified, 'updated_at' => $lastModified])
Expand All @@ -152,4 +163,18 @@ private function getNamespaceAndHandle(string $blueprint): array

return [$namespace, $handle];
}

private function shouldImportBlueprints(): bool
{
return $this->option('only-blueprints')
|| ! $this->option('only-fieldsets')
&& ($this->option('force') || $this->confirm('Do you want to import blueprints?'));
}

private function shouldImportFieldsets(): bool
{
return $this->option('only-fieldsets')
|| ! $this->option('only-blueprints')
&& ($this->option('force') || $this->confirm('Do you want to import fieldsets?'));
}
}
14 changes: 9 additions & 5 deletions src/Fields/BlueprintRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public function find($blueprint): ?Blueprint
return parent::find($blueprint);
}

$blueprintModel = ($namespace ? $this->filesIn($namespace) : app('statamic.eloquent.blueprints.blueprint_model')::whereNull('namespace'))
$blueprintModel = ($namespace ? $this->filesIn($namespace) : app('statamic.eloquent.blueprints.model')::whereNull('namespace'))
->where('handle', $handle)
->first();

if (! $blueprintModel) {
throw_if(
$namespace === null && $handle === 'default',
Exception::class,
\Exception::class,
'Default Blueprint is required but not found. '
);

Expand Down Expand Up @@ -96,10 +96,14 @@ public function in(string $namespace)

protected function filesIn($namespace)
{
if (! $this->isEloquentDrivenNamespace($namespace)) {
return parent::filesIn($namespace);
}

return Blink::store(self::BLINK_NAMESPACE_PATHS)->once($namespace ?? 'none', function () use ($namespace) {
$namespace = str_replace('/', '.', $namespace);

if (count($blueprintModels = app('statamic.eloquent.blueprints.blueprint_model')::where('namespace', $namespace)->get()) == 0) {
if (count($blueprintModels = app('statamic.eloquent.blueprints.model')::where('namespace', $namespace)->get()) == 0) {
return collect();
}

Expand Down Expand Up @@ -138,14 +142,14 @@ protected function getNamespaceAndHandle($blueprint)

public function getModel($blueprint)
{
return app('statamic.eloquent.blueprints.blueprint_model')::where('namespace', $blueprint->namespace() ?? null)
return app('statamic.eloquent.blueprints.model')::where('namespace', $blueprint->namespace() ?? null)
->where('handle', $blueprint->handle())
->first();
}

public function updateModel($blueprint)
{
$model = app('statamic.eloquent.blueprints.blueprint_model')::firstOrNew([
$model = app('statamic.eloquent.blueprints.model')::firstOrNew([
'handle' => $blueprint->handle(),
'namespace' => $blueprint->namespace() ?? null,
]);
Expand Down
6 changes: 3 additions & 3 deletions src/Fields/FieldsetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class FieldsetRepository extends StacheRepository
public function all(): Collection
{
return Blink::once('eloquent-fieldsets', function () {
if (count($models = app('statamic.eloquent.blueprints.fieldset_model')::get() ?? collect()) === 0) {
if (count($models = app('statamic.eloquent.fieldsets.model')::get() ?? collect()) === 0) {
return collect();
}

Expand Down Expand Up @@ -47,7 +47,7 @@ public function delete(Fieldset $fieldset)

public function updateModel($fieldset)
{
$model = app('statamic.eloquent.blueprints.fieldset_model')::firstOrNew([
$model = app('statamic.eloquent.fieldsets.model')::firstOrNew([
'handle' => $fieldset->handle(),
]);

Expand All @@ -59,7 +59,7 @@ public function updateModel($fieldset)

public function deleteModel($fieldset)
{
$model = app('statamic.eloquent.blueprints.fieldset_model')::where('handle', $fieldset->handle())->first();
$model = app('statamic.eloquent.fieldsets.model')::where('handle', $fieldset->handle())->first();

if ($model) {
$model->delete();
Expand Down
44 changes: 34 additions & 10 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,12 @@ private function publishMigrations(): void

$this->publishes($blueprintMigrations = [
__DIR__.'/../database/migrations/2024_03_07_100000_create_blueprints_table.php' => database_path('migrations/2024_03_07_100000_create_blueprints_table.php'),
__DIR__.'/../database/migrations/2024_03_07_100000_create_fieldsets_table.php' => database_path('migrations/2024_03_07_100000_create_fieldsets_table.php'),
], 'statamic-eloquent-blueprint-migrations');

$this->publishes($fieldsetMigrations = [
__DIR__.'/../database/migrations/2024_03_07_100000_create_fieldsets_table.php' => database_path('migrations/2024_03_07_100000_create_fieldsets_table.php'),
], 'statamic-eloquent-fieldset-migrations');

$this->publishes($formMigrations = [
__DIR__.'/../database/migrations/2024_03_07_100000_create_forms_table.php' => database_path('migrations/2024_03_07_100000_create_forms_table.php'),
], 'statamic-eloquent-form-migrations');
Expand Down Expand Up @@ -176,6 +179,7 @@ private function publishMigrations(): void
$navigationTreeMigrations,
$collectionMigrations,
$blueprintMigrations,
$fieldsetMigrations,
$formMigrations,
$formSubmissionMigrations,
$assetContainerMigrations,
Expand Down Expand Up @@ -203,6 +207,7 @@ public function register()
$this->registerCollections();
$this->registerCollectionTrees();
$this->registerEntries();
$this->registerFieldsets();
$this->registerForms();
$this->registerFormSubmissions();
$this->registerGlobals();
Expand Down Expand Up @@ -267,23 +272,19 @@ private function registerBlueprints()
return;
}

$this->app->bind('statamic.eloquent.blueprints.blueprint_model', function () {
return config('statamic.eloquent-driver.blueprints.blueprint_model');
$this->app->bind('statamic.eloquent.blueprints.model', function () {
return config('statamic.eloquent-driver.blueprints.model', config('statamic.eloquent-driver.blueprints.blueprint_model'));
});

$this->app->bind('statamic.eloquent.blueprints.fieldset_model', function () {
return config('statamic.eloquent-driver.blueprints.fieldset_model');
// @deprecated
$this->app->bind('statamic.eloquent.blueprints.blueprints_model', function () {
return config('statamic.eloquent-driver.blueprints.model', config('statamic.eloquent-driver.blueprints.blueprint_model'));
});

$this->app->singleton(\Statamic\Fields\BlueprintRepository::class, function () {
return (new \Statamic\Eloquent\Fields\BlueprintRepository)
->setDirectory(resource_path('blueprints'));
});

$this->app->singleton(
'Statamic\Fields\FieldsetRepository',
'Statamic\Eloquent\Fields\FieldsetRepository'
);
}

private function registerCollections()
Expand Down Expand Up @@ -345,6 +346,29 @@ private function registerEntries()
Statamic::repository(EntryRepositoryContract::class, EntryRepository::class);
}

private function registerFieldsets()
{
$usingOldConfigKeys = config()->has('statamic.eloquent-driver.blueprints.fieldset_model');

if (config($usingOldConfigKeys ? 'statamic.eloquent-driver.blueprints.driver' : 'statamic.eloquent-driver.fieldsets.driver', 'file') != 'eloquent') {
return;
}

$this->app->bind('statamic.eloquent.fieldsets.model', function () {
return config('statamic.eloquent-driver.fieldsets.model', config('statamic.eloquent-driver.blueprints.fieldset_model'));
});

// @deprecated
$this->app->bind('statamic.eloquent.blueprints.fieldset_model', function () {
return config('statamic.eloquent-driver.fieldsets.model', config('statamic.eloquent-driver.blueprints.fieldset_model'));
});

$this->app->singleton(
'Statamic\Fields\FieldsetRepository',
'Statamic\Eloquent\Fields\FieldsetRepository'
);
}

private function registerForms()
{
if (config('statamic.eloquent-driver.forms.driver', 'file') != 'eloquent') {
Expand Down
Loading