From d1dd6db7d48827e9b72a310a16405b171266ace1 Mon Sep 17 00:00:00 2001 From: iRaziul Date: Fri, 3 Apr 2026 12:46:01 +0600 Subject: [PATCH 1/3] fix: resolve collection from attribute --- packages/model/src/Model.php | 110 +++++++++++++++++++++++++---------- 1 file changed, 78 insertions(+), 32 deletions(-) diff --git a/packages/model/src/Model.php b/packages/model/src/Model.php index f920dea..e818023 100644 --- a/packages/model/src/Model.php +++ b/packages/model/src/Model.php @@ -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 { @@ -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(); } @@ -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 @@ -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, ]); } @@ -101,16 +118,21 @@ protected static function isCacheable(): bool public static function cache(array $locales = []) { - if (! count($locales)) { + if (!count($locales)) { $locales = array_keys(Repository::getSources(static::class)); } 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); @@ -119,7 +141,12 @@ 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, + ); }); } @@ -127,11 +154,13 @@ 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)); @@ -139,7 +168,10 @@ public static function migrate(?string $locale = null): void $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: "\\"), + ), ); }); @@ -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]; } From e24d8906be7ee48fa542cffb5d48670f99f94160 Mon Sep 17 00:00:00 2001 From: iRaziul Date: Fri, 3 Apr 2026 12:49:18 +0600 Subject: [PATCH 2/3] Fix: Specify escape character for str_getcsv --- packages/model/src/Model.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/model/src/Model.php b/packages/model/src/Model.php index e818023..8112969 100644 --- a/packages/model/src/Model.php +++ b/packages/model/src/Model.php @@ -164,7 +164,7 @@ public static function migrate(?string $locale = null): void $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( From d067dbc358cc7c461716900dc85e022ba286177d Mon Sep 17 00:00:00 2001 From: iRaziul Date: Fri, 3 Apr 2026 06:49:37 +0000 Subject: [PATCH 3/3] chore: styling --- packages/model/src/Model.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/model/src/Model.php b/packages/model/src/Model.php index 8112969..e9c7e5b 100644 --- a/packages/model/src/Model.php +++ b/packages/model/src/Model.php @@ -118,13 +118,13 @@ protected static function isCacheable(): bool public static function cache(array $locales = []) { - if (!count($locales)) { + if (! count($locales)) { $locales = array_keys(Repository::getSources(static::class)); } collect($locales) ->filter( - fn(string $locale): bool => Repository::sourceIsRegistered( + fn (string $locale): bool => Repository::sourceIsRegistered( static::class, $locale, ), @@ -169,7 +169,7 @@ public static function migrate(?string $locale = null): void $data->transform(function (string $line) use ($schema): Collection { return $schema->combine( array_map( - fn($value) => $value !== "" ? $value : null, + fn ($value) => $value !== "" ? $value : null, str_getcsv($line, escape: "\\"), ), );