Skip to content

Commit

Permalink
Prevent unnecessary query call when model exists (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 authored Feb 23, 2022
1 parent 4a0e1d8 commit 8c12c70
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/TranslationLoaderManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\TranslationLoader;

use Illuminate\Database\QueryException;
use Illuminate\Translation\FileLoader;
use Illuminate\Support\Facades\Schema;
use Spatie\TranslationLoader\TranslationLoaders\TranslationLoader;
Expand All @@ -19,23 +20,27 @@ class TranslationLoaderManager extends FileLoader
*/
public function load($locale, $group, $namespace = null): array
{
$modelClass = config('translation-loader.model');
$model = new $modelClass;
if (is_a($model, LanguageLine::class)) {
if (! Schema::hasTable($model->getTable())) {
return parent::load($locale, $group, $namespace);
}
}
try {
$fileTranslations = parent::load($locale, $group, $namespace);

$fileTranslations = parent::load($locale, $group, $namespace);
if (!is_null($namespace) && $namespace !== '*') {
return $fileTranslations;
}

if (! is_null($namespace) && $namespace !== '*') {
return $fileTranslations;
}
$loaderTranslations = $this->getTranslationsForTranslationLoaders($locale, $group, $namespace);

$loaderTranslations = $this->getTranslationsForTranslationLoaders($locale, $group, $namespace);
return array_replace_recursive($fileTranslations, $loaderTranslations);
} catch (QueryException $e) {
$modelClass = config('translation-loader.model');
$model = new $modelClass;
if (is_a($model, LanguageLine::class)) {
if (! Schema::hasTable($model->getTable())) {
return parent::load($locale, $group, $namespace);
}
}

return array_replace_recursive($fileTranslations, $loaderTranslations);
throw $e;
}
}

protected function getTranslationsForTranslationLoaders(
Expand Down

0 comments on commit 8c12c70

Please sign in to comment.