Skip to content

Commit

Permalink
Improved menu
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Nov 21, 2024
1 parent 881777f commit 95097a0
Show file tree
Hide file tree
Showing 13 changed files with 2,729 additions and 352 deletions.
20 changes: 20 additions & 0 deletions app/Enums/Continent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Enums;

enum Continent: string
{
case Africa = 'Africa';

case Asia = 'Asia';

case Europe = 'Europe';

case NorthAmerica = 'North America';

case SouthAmerica = 'South America';

case Antarctica = 'Antarctica';

case Australia = 'Australia';
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/World/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected function points()
->map(fn(Group|ExternalGroup $row) => [
'lat' => $row->latitude,
'lng' => $row->longitude,
'name' => $row->label(),
'name' => $row->label,
])
->reject(fn($data) => empty($data['lat']) || empty($data['lat']))
->values();
Expand Down
10 changes: 8 additions & 2 deletions app/Models/ExternalGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
use App\Enums\GroupStatus;
use Exception;
use Glhd\Bits\Database\HasSnowflakes;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ExternalGroup extends Model
{
use HasSnowflakes;
use SoftDeletes;
use HasDomain;

protected $appends = [
'label',
];

protected static function booted()
{
Expand All @@ -30,8 +36,8 @@ protected function casts(): array
];
}

public function label(): string
protected function label(): Attribute
{
return $this->region ?? str($this->name)->afterLast('×')->trim()->toString();
return Attribute::get(fn() => $this->region ?? str($this->name)->afterLast('×')->trim()->toString());
}
}
30 changes: 8 additions & 22 deletions app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,25 @@ class Group extends Model
use SoftDeletes;
use HasFactory;
use HasSnowflakes;
use HasDomain;

protected $visible = [
'id',
'domain',
'name',
'region',
'continent',
'description',
'timezone',
'frequency',
'status',
'created_at',
];

protected $appends = [
'label',
];

protected function casts(): array
{
return [
Expand All @@ -55,29 +61,9 @@ protected static function booted()
static::saved(fn() => Cache::forget('phpx-network'));
}

public static function findByDomain(string $domain): ?static
{
if (App::isLocal()) {
$domain = str($domain)->replaceEnd('.test', '.com')->toString();
}

$container = Container::getInstance();
$id = "group:{$domain}";

if (! $container->has($id)) {
if (! $group = Group::firstWhere('domain', $domain)) {
return null;
}

$container->instance($id, $group);
}

return $container->get($id);
}

public function label(): string
protected function label(): Attribute
{
return $this->region ?? str($this->name)->afterLast('×')->trim()->toString();
return Attribute::get(fn() => $this->region ?? str($this->name)->afterLast('×')->trim()->toString());
}

public function isActive()
Expand Down
29 changes: 29 additions & 0 deletions app/Models/HasDomain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

use Illuminate\Container\Container;
use Illuminate\Support\Facades\App;

trait HasDomain
{
public static function findByDomain(string $domain): ?static
{
if (App::isLocal()) {
$domain = str($domain)->replaceEnd('.test', '.com')->toString();
}

$container = Container::getInstance();
$id = class_basename(static::class).":{$domain}";

if (! $container->has($id)) {
if (! $group = static::firstWhere('domain', $domain)) {
return null;
}

$container->instance($id, $group);
}

return $container->get($id);
}
}
41 changes: 17 additions & 24 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,29 @@ public function boot(): void
$this->sharePhpxNetwork();
}

protected function sharePhpxNetwork()
protected function sharePhpxNetwork(): void
{
$this->callAfterResolving(Factory::class, function(Factory $view) {
$network = Cache::remember('phpx-network', now()->addWeek(), function() {
try {
return Group::query()
->select('domain', 'name', 'region')
->where('domain_status', DomainStatus::Confirmed)
->get()
->mapWithKeys(fn(Group $group) => [$group->domain => $group->label()])
->toArray();
} catch (Throwable) {
return [];
}
$data = Cache::remember('phpx-network', now()->addWeek(), function() {
$groups = Group::query()
->where('domain_status', DomainStatus::Confirmed)
->get()
->map(fn(Group $group) => [$group::class, $group->attributesToArray()]);

$external = ExternalGroup::query()->get()
->map(fn(ExternalGroup $group) => [$group::class, $group->attributesToArray()]);

return $groups->merge($external)->values()->toArray();
});

$external = Cache::remember('phpx-network-external', now()->addWeek(), function() {
try {
return ExternalGroup::query()
->select('domain', 'name', 'region')
->get()
->mapWithKeys(fn(ExternalGroup $g) => [$g->domain => $g->label()])
->toArray();
} catch (Throwable) {
return [];
}
});
/** @var \Illuminate\Support\Collection<string, Group|ExternalGroup> $network */
$network = collect($data)
->map(function (array $record) {
[$fqcn, $attributes] = $record;
return (new $fqcn)->newFromBuilder($attributes);
});

$view->share('phpx_network', $network);
$view->share('phpx_external', $external);
});
}
}
36 changes: 36 additions & 0 deletions database/migrations/2024_11_21_145425_add_continent_to_groups.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use App\Enums\Continent;
use App\Models\ExternalGroup;
use App\Models\Group;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('groups', function(Blueprint $table) {
$table->string('continent', 20)->after('region')->default(Continent::NorthAmerica);
});

Schema::table('external_groups', function(Blueprint $table) {
$table->string('continent', 20)->after('region')->default(Continent::NorthAmerica);
});

Group::findByDomain('phpxvie.com')?->update(['continent' => Continent::Europe]);
Group::findByDomain('phpxpnq.com')?->update(['continent' => Continent::Asia]);
Group::findByDomain('phpxcebu.com')?->update(['continent' => Continent::Asia]);
Group::findByDomain('phpxadl.com')?->update(['continent' => Continent::Australia]);

ExternalGroup::findByDomain('phpstoke.co.uk')?->update(['continent' => Continent::Europe]);
ExternalGroup::findByDomain('laravel.swiss')?->update(['continent' => Continent::Europe]);
}

public function down(): void
{
Schema::table('groups', function(Blueprint $table) {
$table->dropColumn('continent');
});
}
};
Loading

0 comments on commit 95097a0

Please sign in to comment.