Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AnourValar committed Sep 21, 2024
1 parent 0b038e3 commit 8a70133
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Http/Middleware/Api/Locale.php
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);
}
}
20 changes: 20 additions & 0 deletions src/Providers/LaravelAtomServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ public function register()
{
// config
$this->mergeConfigFrom(__DIR__.'/../resources/config/atom.php', 'atom');
$this->mergeConfigFrom(__DIR__.'/../resources/config/bindings.php', 'bindings');

// bindings
foreach (config('bindings') as $interface => $implementation) {
if (! isset($implementation['bind'])) {
continue;
}

$this->app->singleton($interface, function ($app, $arguments = []) use ($interface, $implementation) {
$object = new $implementation['bind'](...$arguments);

if (interface_exists($interface) && ! $object instanceof $interface) {
throw new \LogicException("{$implementation['bind']} is not instance of {$interface}");
}

return $object;
});
}

// sc
$this->app->singleton(\AnourValar\LaravelAtom\Service::class, function ($app) {
return new \AnourValar\LaravelAtom\Service(new \AnourValar\LaravelAtom\Registry());
});
Expand All @@ -43,6 +62,7 @@ public function boot()
{
// config
$this->publishes([ __DIR__.'/../resources/config/atom.php' => config_path('atom.php')], 'config');
$this->publishes([ __DIR__.'/../resources/config/bindings.php' => config_path('bindings.php')], 'config');

// langs
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'laravel-atom');
Expand Down
63 changes: 63 additions & 0 deletions src/Traits/OptimizeCheckerTrait.php
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).']'
);
}
}

});
}
}
106 changes: 106 additions & 0 deletions src/Traits/PostgresTrait.php
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);
}
}
19 changes: 19 additions & 0 deletions src/resources/config/bindings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Adapters
|--------------------------------------------------------------------------
*/

// ...

/*
|--------------------------------------------------------------------------
| Other implementations
|--------------------------------------------------------------------------
*/

// ...
];

0 comments on commit 8a70133

Please sign in to comment.