Skip to content
Open
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
110 changes: 78 additions & 32 deletions packages/model/src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Model extends Eloquent\Model
public static array $schema = [];
protected static array $sqliteConnections = [];
public $incrementing = false;
protected $keyType = 'string';
protected $keyType = "string";

protected static function boot(): void
{
Expand All @@ -28,14 +28,12 @@ protected static function boot(): void
}

if (static::isCacheable()) {
static::cache([
Repository::getLocale(static::class),
]);
static::cache([Repository::getLocale(static::class)]);

return;
}

static::setSqliteConnection(':memory:');
static::setSqliteConnection(":memory:");

static::migrate();
}
Expand All @@ -58,25 +56,42 @@ protected static function getCachedAt($locale = null): int

protected static function getCachePath(?string $locale = null): string
{
return static::getCacheDirectory() . '/' . static::getCacheFileName($locale);
return static::getCacheDirectory() .
"/" .
static::getCacheFileName($locale);
}

protected static function getCacheDirectory(): string
{
return realpath(config('squire.cache-path', storage_path('framework/cache')));
return realpath(
config("squire.cache-path", storage_path("framework/cache")),
);
}

protected static function getCacheFileName(?string $locale = null): string
{
$kebabCaseLocale = strtolower(str_replace('_', '-', $locale ?? Repository::getLocale(static::class)));
$kebabCaseModelClassName = Str::kebab(str_replace('\\', '', static::class));

return config('squire.cache-prefix', 'squire') . '-' . $kebabCaseModelClassName . '-' . $kebabCaseLocale . '.sqlite';
$kebabCaseLocale = strtolower(
str_replace(
"_",
"-",
$locale ?? Repository::getLocale(static::class),
),
);
$kebabCaseModelClassName = Str::kebab(
str_replace("\\", "", static::class),
);

return config("squire.cache-prefix", "squire") .
"-" .
$kebabCaseModelClassName .
"-" .
$kebabCaseLocale .
".sqlite";
}

protected static function getModelUpdatedAt(): int
{
return filemtime((new ReflectionClass(static::class))->getFileName());
return filemtime(new ReflectionClass(static::class)->getFileName());
}

protected static function getSourceUpdatedAt(?string $locale = null): int
Expand All @@ -86,9 +101,11 @@ protected static function getSourceUpdatedAt(?string $locale = null): int

protected static function setSqliteConnection(string $database): void
{
static::$sqliteConnections[static::class] = app(ConnectionFactory::class)->make([
'driver' => 'sqlite',
'database' => $database,
static::$sqliteConnections[static::class] = app(
ConnectionFactory::class,
)->make([
"driver" => "sqlite",
"database" => $database,
]);
}

Expand All @@ -106,11 +123,16 @@ public static function cache(array $locales = [])
}

collect($locales)
->filter(fn (string $locale): bool => Repository::sourceIsRegistered(static::class, $locale))
->filter(
fn (string $locale): bool => Repository::sourceIsRegistered(
static::class,
$locale,
),
)
->each(function (string $locale): void {
$cachePath = static::getCachePath($locale);

file_put_contents($cachePath, '');
file_put_contents($cachePath, "");

static::setSqliteConnection($cachePath);

Expand All @@ -119,27 +141,37 @@ public static function cache(array $locales = [])
$modelUpdatedAt = static::getModelUpdatedAt();
$sourceUpdatedAt = static::getSourceUpdatedAt($locale);

touch($cachePath, $modelUpdatedAt >= $sourceUpdatedAt ? $modelUpdatedAt : $sourceUpdatedAt);
touch(
$cachePath,
$modelUpdatedAt >= $sourceUpdatedAt
? $modelUpdatedAt
: $sourceUpdatedAt,
);
});
}

public static function migrate(?string $locale = null): void
{
$tableName = static::getTableName();

static::resolveConnection()->getSchemaBuilder()->create($tableName, function (Blueprint $table): void {
foreach (static::$schema as $name => $type) {
$table->{$type}($name)->nullable();
}
});
static::resolveConnection()
->getSchemaBuilder()
->create($tableName, function (Blueprint $table): void {
foreach (static::$schema as $name => $type) {
$table->{$type}($name)->nullable();
}
});

$data = collect(Repository::fetchData(static::class, $locale));

$schema = collect(str_getcsv($data->first()));
$schema = collect(str_getcsv($data->first(), escape: "\\"));

$data->transform(function (string $line) use ($schema): Collection {
return $schema->combine(
array_map(fn ($value) => $value !== '' ? $value : null, str_getcsv($line, escape: '\\'))
array_map(
fn ($value) => $value !== "" ? $value : null,
str_getcsv($line, escape: "\\"),
),
);
});

Expand All @@ -150,23 +182,37 @@ public static function migrate(?string $locale = null): void
continue;
}

static::resolveConnection()->table($tableName)->insert($dataToInsert);
static::resolveConnection()
->table($tableName)
->insert($dataToInsert);
}
}

protected static function getTableName(): string
public function resolveCollectionFromAttribute(): ?string
{
$defaultProperties = (new ReflectionClass(static::class))->getDefaultProperties();
return null;
}

if (isset($defaultProperties['table']) && is_string($defaultProperties['table']) && $defaultProperties['table'] !== '') {
return $defaultProperties['table'];
protected static function getTableName(): string
{
$defaultProperties = new ReflectionClass(
static::class,
)->getDefaultProperties();

if (
isset($defaultProperties["table"]) &&
is_string($defaultProperties["table"]) &&
$defaultProperties["table"] !== ""
) {
return $defaultProperties["table"];
}

return Str::snake(Str::pluralStudly(class_basename(static::class)));
}

public static function resolveConnection($connection = null): SQLiteConnection
{
public static function resolveConnection(
$connection = null,
): SQLiteConnection {
return static::$sqliteConnections[static::class];
}

Expand Down
Loading