-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b038e3
commit 8a70133
Showing
5 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace AnourValar\LaravelAtom\Http\Middleware\Api; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class Locale | ||
{ | ||
protected $header = 'Accept-Language'; | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next | ||
* @param ...$supportedLocales | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
*/ | ||
public function handle(Request $request, Closure $next, ...$supportedLocales): Response | ||
{ | ||
$locale = mb_substr((string) $request->header($this->header), 0, 2); | ||
$locale = mb_strtolower($locale); | ||
|
||
if (in_array($locale, $supportedLocales)) { | ||
\App::setLocale($locale); | ||
\Date::setLocale($locale); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace AnourValar\LaravelAtom\Traits; | ||
|
||
trait OptimizeCheckerTrait | ||
{ | ||
/** | ||
* Boot the testing helper traits. | ||
* | ||
* @return array | ||
*/ | ||
protected function setUpTraits() | ||
{ | ||
$uses = parent::setUpTraits(); | ||
|
||
$this->registerChecker(); | ||
|
||
\Queue::after(function ($job) { | ||
\App::make(\Illuminate\Contracts\Cache\Repository::class)->getStore()->locks = []; | ||
}); | ||
|
||
return $uses; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function registerChecker(): void | ||
{ | ||
if (\DB::connection()->getPdo()->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql') { | ||
\DB::update('SET enable_seqscan = 0'); | ||
//$this->assertEquals('off', \DB::select("SHOW enable_seqscan")[0]->enable_seqscan); | ||
} | ||
|
||
\DB::listen(function ($query) { | ||
$sql = $query->sql; | ||
|
||
if (stripos($sql, 'select ') !== 0) { | ||
return; | ||
} | ||
|
||
if (! stripos($sql, ' where ')) { | ||
return; | ||
} | ||
|
||
$sql = preg_replace('|\s+limit\s+\d+\s*$|iu', '', $sql); | ||
|
||
foreach (\DB::select("EXPLAIN {$sql}", $query->bindings) as $item) { | ||
$item = (array) $item; | ||
$item = $item['QUERY PLAN']; | ||
|
||
if (mb_stripos($item, 'Seq Scan') !== false) { | ||
$this->assertStringNotContainsString( | ||
'Seq Scan', | ||
$item, | ||
$sql . ' ['.implode(', ', $query->bindings).']' | ||
); | ||
} | ||
} | ||
|
||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace AnourValar\LaravelAtom\Traits; | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
trait PostgresTrait | ||
{ | ||
/** | ||
* Create a GIN index | ||
* | ||
* @param string $tableName | ||
* @param string $column | ||
* @param string $option | ||
* @return void | ||
*/ | ||
protected function addGinIndex(string $tableName, string $column, string $option = 'gin_trgm_ops'): void | ||
{ | ||
Schema::table($tableName, function (Blueprint $table) use ($tableName, $column, $option) { | ||
if (Schema::getConnection()->getPdo()->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql') { | ||
Schema::getConnection()->statement( | ||
"CREATE INDEX {$tableName}_{$column}_index ON {$tableName} USING gin ({$column} {$option});" | ||
); | ||
} else { | ||
$table->index($column); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Remove default value from the column | ||
* | ||
* @param string $tableName | ||
* @param string $column | ||
* @return void | ||
*/ | ||
protected function removeDefault(string $tableName, string $column): void | ||
{ | ||
\DB::statement("ALTER TABLE {$tableName} ALTER COLUMN {$column} DROP DEFAULT;"); | ||
} | ||
|
||
/** | ||
* Sync the auto-increment state | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
*/ | ||
protected function syncAutoIncrement(\Illuminate\Database\Eloquent\Model $model): void | ||
{ | ||
if ($model->getConnection() instanceof \Illuminate\Database\PostgresConnection) { | ||
$table = $model->getTable(); | ||
$key = $model->getKeyName(); | ||
|
||
\DB::connection($model->getConnectionName()) | ||
->select("SELECT setval('{$table}_id_seq', max({$key})) FROM {$table}"); | ||
} | ||
} | ||
|
||
/** | ||
* Enable pg_trgm extension | ||
* | ||
* @return void | ||
*/ | ||
protected function installPgTrgm(): void | ||
{ | ||
if (\DB::connection()->getPdo()->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql') { | ||
\DB::statement('create extension if not exists pg_trgm;'); | ||
} | ||
} | ||
|
||
/** | ||
* Create a conditional (not null) b-tree index | ||
* | ||
* @param string $tableName | ||
* @param string $column | ||
*/ | ||
protected function conditionalIndexNotNull(string $tableName, string $column) | ||
{ | ||
\DB::statement(<<< HERE | ||
CREATE INDEX {$tableName}_{$column}_index ON {$tableName} | ||
USING btree ({$column}) WHERE ({$column} IS NOT NULL); | ||
HERE); | ||
} | ||
|
||
/** | ||
* create a fulltext search (tsvector) column with RUM index | ||
* | ||
* @param string $tableName | ||
* @param string $columnName | ||
* @param string $locale | ||
* @return void | ||
*/ | ||
protected function addRumFullText(string $tableName, string $columnName = 'search_fulltext', string $locale = null): void | ||
{ | ||
if (! $locale) { | ||
$locale = config('app.fallback_locale'); | ||
} | ||
|
||
\DB::statement("ALTER TABLE {$tableName} ADD COLUMN {$columnName} TSVECTOR"); | ||
|
||
\DB::statement(<<<HERE | ||
CREATE INDEX {$tableName}_{$columnName}_rum ON {$tableName} USING rum ({$columnName} rum_tsvector_ops); | ||
HERE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
return [ | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Adapters | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
|
||
// ... | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Other implementations | ||
|-------------------------------------------------------------------------- | ||
*/ | ||
|
||
// ... | ||
]; |