Toolvana: production-ready media tools platform (CodeIgniter 4)
- 134-tool registry with programmatic SEO (unique titles/H1/descriptions, JSON-LD graphs, sitemap index, canonical 301 enforcement via required filter) - DB-backed job queue (SKIP LOCKED) with drivers: Ffmpeg, Images (GD), Pdf (qpdf/gs/poppler), Youtube (thumbnails), Qr (server-side PNG) - Security: SSRF guard, MIME validation, rate limits, API-key auth, bcrypt admin login, security headers - Admin panel: dashboard, tools/categories/guides CRUD, SEO audit, analytics, job inspector with retry, system health, feature flags - Docker deployment (nginx + web/api FPM pools + scalable workers), PHPUnit suite (19 tests / 1139 assertions), PWA manifest + service worker
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Libraries;
|
||||
|
||||
use App\Models\CategoryModel;
|
||||
use App\Models\ToolModel;
|
||||
|
||||
/**
|
||||
* Request-cached accessor for the tool registry. Every page, sitemap,
|
||||
* route and link block goes through here so the DB is hit at most once
|
||||
* per dataset per request.
|
||||
*/
|
||||
final class Finder
|
||||
{
|
||||
private ?array $bySlug = null;
|
||||
|
||||
/** @var array<int,array>|null */
|
||||
private ?array $categories = null;
|
||||
|
||||
public function tool(string $slug): ?array
|
||||
{
|
||||
if ($this->bySlug === null) {
|
||||
$tools = model(ToolModel::class)->allActive();
|
||||
$this->bySlug = [];
|
||||
foreach ($tools as $tool) {
|
||||
// decode JSON columns once per request
|
||||
foreach (['input_formats', 'output_formats', 'how_to', 'faqs', 'related_slugs', 'aliases', 'requires_binaries', 'accepts_mimes'] as $col) {
|
||||
if (isset($tool[$col]) && is_string($tool[$col])) {
|
||||
$decoded = json_decode($tool[$col], true);
|
||||
$tool[$col] = is_array($decoded) ? $decoded : [];
|
||||
} elseif (! isset($tool[$col])) {
|
||||
$tool[$col] = [];
|
||||
}
|
||||
}
|
||||
$this->bySlug[$tool['slug']] = $tool;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->bySlug[mb_strtolower($slug)] ?? null;
|
||||
}
|
||||
|
||||
public function categories(): array
|
||||
{
|
||||
if ($this->categories === null) {
|
||||
$this->categories = model(CategoryModel::class)->activeOrdered();
|
||||
}
|
||||
|
||||
return $this->categories;
|
||||
}
|
||||
|
||||
public function category(string $slug): ?array
|
||||
{
|
||||
foreach ($this->categories() as $category) {
|
||||
if ($category['slug'] === $slug) {
|
||||
foreach (['faqs'] as $col) {
|
||||
if (isset($category[$col]) && is_string($category[$col])) {
|
||||
$decoded = json_decode($category[$col], true);
|
||||
$category[$col] = is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
}
|
||||
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function categoryById(int $id): ?array
|
||||
{
|
||||
foreach ($this->categories() as $category) {
|
||||
if ((int) $category['id'] === $id) {
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toolsInCategory(string $slug): array
|
||||
{
|
||||
$category = $this->category($slug);
|
||||
if ($category === null) {
|
||||
return [];
|
||||
}
|
||||
$tools = model(ToolModel::class)->activeInCategory((int) $category['id']);
|
||||
foreach ($tools as &$t) {
|
||||
$this->decodeTool($t);
|
||||
}
|
||||
|
||||
return $tools;
|
||||
}
|
||||
|
||||
public function popular(int $limit = 8): array
|
||||
{
|
||||
$tools = model(ToolModel::class)->popular($limit);
|
||||
array_walk($tools, [$this, 'decodeTool']);
|
||||
|
||||
return $tools;
|
||||
}
|
||||
|
||||
public function featured(int $limit = 8): array
|
||||
{
|
||||
$tools = model(ToolModel::class)->featured($limit);
|
||||
array_walk($tools, [$this, 'decodeTool']);
|
||||
|
||||
return $tools;
|
||||
}
|
||||
|
||||
public function recent(int $limit = 6): array
|
||||
{
|
||||
$tools = model(ToolModel::class)->recent($limit);
|
||||
array_walk($tools, [$this, 'decodeTool']);
|
||||
|
||||
return $tools;
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
return array_values($this->index());
|
||||
}
|
||||
|
||||
public function countTools(): int
|
||||
{
|
||||
return count($this->index());
|
||||
}
|
||||
|
||||
private function index(): array
|
||||
{
|
||||
if ($this->bySlug === null) {
|
||||
$this->tool('__warm__'); // forces load
|
||||
}
|
||||
|
||||
return $this->bySlug ?? [];
|
||||
}
|
||||
|
||||
public static function decodeTool(array &$tool): void
|
||||
{
|
||||
foreach (['input_formats', 'output_formats', 'how_to', 'faqs', 'related_slugs', 'aliases', 'requires_binaries', 'accepts_mimes'] as $col) {
|
||||
if (isset($tool[$col]) && is_string($tool[$col])) {
|
||||
$decoded = json_decode($tool[$col], true);
|
||||
$tool[$col] = is_array($decoded) ? $decoded : [];
|
||||
} elseif (! isset($tool[$col])) {
|
||||
$tool[$col] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user