diff --git a/packages/model/src/Model.php b/packages/model/src/Model.php index f920dea..e9c7e5b 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, ]); } @@ -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); @@ -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,19 +154,24 @@ 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: "\\"), + ), ); }); @@ -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]; }