Skip to content
Closed
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
11 changes: 2 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@ jobs:

strategy:
matrix:
php: [8.1, 8.2, 8.3, 8.4]
laravel: [10.*, 11.*, 12.*]
php: [8.2, 8.3, 8.4]
laravel: [11.*, 12.*]
stability: [prefer-lowest, prefer-stable]
exclude:
- php: 8.1
laravel: 11.*
- php: 8.1
laravel: 12.*
- php: 8.4
laravel: 10.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }}

Expand Down
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The command will also give you the opportunity to indicate whether you'd like ex

If you originally opt-out of importing existing content, then later change your mind, you can import existing content by running the relevant commands:

- Addon Settings: `php please eloquent:import-addon-settings`
- Assets: `php please eloquent:import-assets`
- Blueprints and Fieldsets: `php please eloquent:import-blueprints`
- Collections: `php please eloquent:import-collections`
Expand All @@ -43,6 +44,7 @@ If your assets are being driven by the Eloquent Driver and you're managing your

If you wish to move back to flat-files, you may use the following commands to export your content out of the database:

- Addon Settings: `php please eloquent:export-addon-settings`
- Assets: `php please eloquent:export-assets`
- Blueprints and Fieldsets: `php please eloquent:export-blueprints`
- Collections: `php please eloquent:export-collections`
Expand Down Expand Up @@ -114,15 +116,18 @@ By default, the Eloquent Driver stores all data in a single `data` column. Howev

class Entry extends \Statamic\Eloquent\Entries\EntryModel
{
protected $casts = [
// The casts from Statamic's base model...
'date' => 'datetime',
'data' => 'json',
'published' => 'boolean',

// Your custom casts...
'featured_images' => 'json',
];
protected function casts(): array
{
return [
// The casts from Statamic's base model...
'date' => 'datetime',
'data' => 'json',
'published' => 'boolean',

// Your custom casts...
'featured_images' => 'json',
];
}
}
```

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
}
},
"require": {
"php": "^8.1",
"statamic/cms": "^5.48"
"php": "^8.2",
"statamic/cms": "dev-master"
},
"require-dev": {
"doctrine/dbal": "^3.8",
"laravel/pint": "^1.0",
"orchestra/testbench": "^8.28 || ^9.6.1 || ^10.0",
"phpunit/phpunit": "^10.5.35 || ^11.0"
"orchestra/testbench": "^9.6.1 || ^10.0",
"phpunit/phpunit": "^11.0"
},
"scripts": {
"test": "phpunit"
Expand Down
5 changes: 5 additions & 0 deletions config/eloquent-driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
'connection' => env('STATAMIC_ELOQUENT_CONNECTION', ''),
'table_prefix' => env('STATAMIC_ELOQUENT_PREFIX', ''),

'addon_settings' => [
'driver' => 'file',
'model' => \Statamic\Eloquent\AddonSettings\AddonSettingsModel::class,
],

'asset_containers' => [
'driver' => 'file',
'model' => \Statamic\Eloquent\Assets\AssetContainerModel::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function up()
$table->id();
$table->string('handle')->index();
$table->string('locale')->nullable();
$table->string('origin')->nullable();
$table->jsonb('data');
$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Statamic\Eloquent\Database\BaseMigration as Migration;

return new class extends Migration
{
public function up()
{
Schema::create($this->prefix('addon_settings'), function (Blueprint $table) {
$table->string('addon')->index()->primary();
$table->json('settings')->nullable();
});
}

public function down()
{
Schema::dropIfExists($this->prefix('addon_settings'));
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Statamic\Eloquent\Database\BaseMigration as Migration;

return new class extends Migration {
public function up()
{
Schema::table($this->prefix('global_set_variables'), function (Blueprint $table) {
$table->dropColumn('origin');
});
}

public function down()
{
Schema::table($this->prefix('global_set_variables'), function (Blueprint $table) {
$table->string('origin')->nullable();
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ return new class extends Migration {
}

Schema::table($this->prefix('form_submissions'), function (Blueprint $table) {
$table->string('form', 30)->nullable()->index()->after('id');
$table->string('form')->nullable()->index()->after('id');
});

$forms = FormModel::all()->pluck('handle', 'id');
Expand Down
44 changes: 44 additions & 0 deletions src/AddonSettings/AddonSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Statamic\Eloquent\AddonSettings;

use Illuminate\Database\Eloquent\Model;
use Statamic\Addons\Settings as AbstractSettings;
use Statamic\Facades\Addon;

class AddonSettings extends AbstractSettings
{
protected $model;

public static function fromModel(Model $model)
{
$addon = Addon::get($model->addon);

return (new static($addon, $model->settings))->model($model);
}

public function toModel()
{
return self::makeModelFromContract($this);
}

public static function makeModelFromContract(AbstractSettings $settings)
{
$class = app('statamic.eloquent.addon_settings.model');

return $class::firstOrNew(['addon' => $settings->addon()->id()])->fill([
'settings' => array_filter($settings->raw()),
]);
}

public function model($model = null)
{
if (func_num_args() === 0) {
return $this->model;
}

$this->model = $model;

return $this;
}
}
25 changes: 25 additions & 0 deletions src/AddonSettings/AddonSettingsModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Statamic\Eloquent\AddonSettings;

use Statamic\Eloquent\Database\BaseModel;

class AddonSettingsModel extends BaseModel
{
protected $guarded = [];

protected $table = 'addon_settings';

protected $primaryKey = 'addon';

protected $keyType = 'string';

public $timestamps = false;

protected function casts(): array
{
return [
'settings' => 'array',
];
}
}
37 changes: 37 additions & 0 deletions src/AddonSettings/AddonSettingsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Statamic\Eloquent\AddonSettings;

use Statamic\Addons\SettingsRepository as FileSettingsRepository;
use Statamic\Contracts\Addons\Settings as AddonSettingsContract;

class AddonSettingsRepository extends FileSettingsRepository
{
public function find(string $addon): ?AddonSettingsContract
{
$model = app('statamic.eloquent.addon_settings.model')::find($addon);

if (! $model) {
return null;
}

return AddonSettings::fromModel($model);
}

public function save(AddonSettingsContract $settings): bool
{
return $settings->toModel()->save();
}

public function delete(AddonSettingsContract $settings): bool
{
return $settings->toModel()->delete();
}

public static function bindings(): array
{
return [
AddonSettingsContract::class => AddonSettings::class,
];
}
}
9 changes: 6 additions & 3 deletions src/Assets/AssetContainerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ class AssetContainerModel extends BaseModel

protected $table = 'asset_containers';

protected $casts = [
'settings' => 'json',
];
protected function casts(): array
{
return [
'settings' => 'json',
];
}

public function getAttribute($key)
{
Expand Down
11 changes: 7 additions & 4 deletions src/Assets/AssetModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ class AssetModel extends BaseModel

protected $table = 'assets_meta';

protected $casts = [
'data' => 'json',
'meta' => 'json',
];
protected function casts(): array
{
return [
'data' => 'json',
'meta' => 'json',
];
}
}
25 changes: 14 additions & 11 deletions src/Collections/CollectionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ class CollectionModel extends BaseModel

protected $table = 'collections';

protected $casts = [
'settings' => 'json',
'settings.routes' => 'array',
'settings.inject' => 'array',
'settings.taxonomies' => 'array',
'settings.structure' => 'array',
'settings.sites' => 'array',
'settings.revisions' => 'boolean',
'settings.dated' => 'boolean',
'settings.default_publish_state' => 'boolean',
];
protected function casts(): array
{
return [
'settings' => 'json',
'settings.routes' => 'array',
'settings.inject' => 'array',
'settings.taxonomies' => 'array',
'settings.structure' => 'array',
'settings.sites' => 'array',
'settings.revisions' => 'boolean',
'settings.dated' => 'boolean',
'settings.default_publish_state' => 'boolean',
];
}
}
49 changes: 49 additions & 0 deletions src/Commands/ExportAddonSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Statamic\Eloquent\Commands;

use Illuminate\Console\Command;
use Statamic\Addons\FileSettingsRepository;
use Statamic\Console\RunsInPlease;
use Statamic\Contracts\Addons\SettingsRepository as SettingsRepositoryContract;
use Statamic\Eloquent\AddonSettings\AddonSettingsModel;
use Statamic\Facades\Addon;
use Statamic\Statamic;

class ExportAddonSettings extends Command
{
use RunsInPlease;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'statamic:eloquent:export-addon-settings';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Exports eloquent addon settings to flat files.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
Statamic::repository(SettingsRepositoryContract::class, FileSettingsRepository::class);

AddonSettingsModel::all()->each(function ($model) {
Addon::get($model->addon)?->settings()->set($model->settings)->save();
});

$this->newLine();
$this->info('Addon settings exported');

return 0;
}
}
Loading
Loading