Skip to content

jkstudio99/open-genetics

Repository files navigation

OpenGenetics Logo

OpenGenetics Framework

Enterprise PHP Micro-Framework v2.2 β€” Production-ready features built-in. No config bloat. Just PHP.

License: MIT PHP 8.1+ MySQL v2.3.0


✨ What's New in v2.3

Feature Description
πŸ—ƒοΈ Query Builder DB::table('users')->where('active',1)->paginate(20) β€” fluent builder on PDO with prepared statements
βœ… Guard::check() Soft auth check β€” returns bool instead of throwing, perfect for optional-auth endpoints
🏷️ Cache::namespace() Isolate cache key spaces between modules β€” Cache::namespace('shop')->get('products')
πŸ§ͺ Testing: seed() $this->seed(['users' => [...]]) β€” seed test data within transaction rollback
⏩ Pipeline::after() Post-response hooks for async side effects like audit logging
🚫 #[SkipMiddleware] #[SkipMiddleware(CorsMiddleware::class)] β€” exclude routes from global middleware
πŸ“ LogMiddleware Built-in request/response logging middleware
πŸ”Œ ErrorHandler::reporter() ErrorHandler::reporter(callable) β€” hook for Sentry/Bugsnag integration

✨ What's in v2.0

# Feature Description
1 ⚑️ Middleware Pipeline #[Middleware('auth', 'rate:10,60')] declarative middleware with Chain of Responsibility
2 πŸ—„οΈ DB Migrations Versioned, batch-rollback migrations with migrate, migrate:rollback, migrate:status
3 πŸ§ͺ Testing Framework GeneticTestCase with HTTP client, actingAsAdmin(), assertOk(), assertPaginated()
4 πŸš€ Caching Layer File-based Cache::remember(), tag-based flush, zero dependencies
5 πŸ” Field Selector ?fields=id,name,price sparse fieldsets β€” GraphQL-lite without the server
6 πŸ“‘ Genetic Pulse Server-Sent Events real-time push β€” no WebSocket server needed
7 🧩 Genetic Modules GeneticModule plugin system with auto-discovery and lifecycle hooks
8 πŸ› οΈ Admin Generator make:admin <table> β€” full CRUD admin endpoint from DB schema in seconds
9 πŸ€– Endpoint AI make:endpoint-ai products "CRUD with auth and cache" β€” NLP scaffold
10 πŸͺ Marketplace market:install og/notifications β€” community packages in one command

Plus all v1.0 features: JWT Auth, RBAC, i18n, Audit Trail, Dual SDK, File-based Routing, CLI, OpenAPI


πŸš€ Quick Start

# 1. Clone or create project
composer create-project open-genetics/framework my-api && cd my-api

# 2. Configure environment
cp .env.example .env
# Edit: DB_NAME, DB_USER, DB_PASS, JWT_SECRET

# 3. Bootstrap (tables + RBAC + admin user)
php genetics mutate

# 4. Start dev server
php genetics serve
# β†’ http://127.0.0.1:8080

Default admin: [email protected] / password (change immediately in production)


πŸ’» Building an API Endpoint

Drop a file in api/ β€” it's instantly a route. No registration needed.

// api/products.php  β†’  GET/POST /api/products

use OpenGenetics\Core\{Response, Cache};
use OpenGenetics\Core\DB;

#[\OpenGenetics\Core\Middleware('auth', 'rate:60,60')]
class Products
{
    public static function get(array $body): void
    {
        // Query Builder with caching
        $products = Cache::remember('products:all', 300, fn() =>
            DB::table('products')->where('active', 1)->paginate(20)
        );
        Response::success($products);
    }

    public static function post(array $body): void
    {
        // AI-assisted scaffold: php genetics make:endpoint-ai products "with auth and audit"
        Response::success(null, 'Created', 201);
    }
}

Or scaffold in seconds:

php genetics make:endpoint-ai products "CRUD with auth, search, pagination and cache"

πŸ–₯️ CLI β€” 25+ Commands

Database:    mutate, seed, status, serve
Migrations:  migrate, migrate:rollback, migrate:status
Scaffold:    make:endpoint, make:middleware, make:migration,
             make:test, make:admin, make:endpoint-ai
Cache:       cache:clear, cache:stats, cache:gc
Marketplace: market:list, market:search, market:install
Modules:     modules:list
Docs:        docs:generate
Info:        help, --version, new

πŸ—οΈ Project Structure

my-api/
β”œβ”€β”€ api/                    # File-based routing (1 file = 1 endpoint)
β”‚   └── auth/               # Auth routes (login, register, logout)
β”œβ”€β”€ src/Core/               # Framework core (PSR-4, PHP 8.1+)
β”‚   β”œβ”€β”€ QueryBuilder.php    # Fluent Query Builder (v2.2)
β”‚   β”œβ”€β”€ Cache.php           # Caching layer with namespace support (v2.2)
β”‚   β”œβ”€β”€ FieldSelector.php   # GraphQL-lite sparse fieldsets
β”‚   β”œβ”€β”€ Pulse.php           # Server-Sent Events
β”‚   β”œβ”€β”€ ModuleLoader.php    # Plugin system
β”‚   β”œβ”€β”€ AdminGenerator.php  # Admin endpoint scaffolder
β”‚   β”œβ”€β”€ EndpointAI.php      # AI endpoint generator
β”‚   β”œβ”€β”€ Marketplace.php     # Package registry
β”‚   β”œβ”€β”€ Pipeline.php        # Middleware pipeline with Pipeline::after()
β”‚   β”œβ”€β”€ Migrator.php        # DB migrations
β”‚   β”œβ”€β”€ Database.php        # PDO Singleton
β”‚   β”œβ”€β”€ Router.php          # File-based router
β”‚   └── Response.php        # JSON response helpers
β”œβ”€β”€ src/Auth/               # JWT + Guard RBAC (Guard::check() v2.2)
β”œβ”€β”€ src/Testing/            # GeneticTestCase + TestResponse + seed()
β”œβ”€β”€ src/Middleware/         # Auth, CORS, RateLimit, LogMiddleware
β”œβ”€β”€ database/migrations/    # Versioned migration files
β”œβ”€β”€ modules/                # Genetic Modules (plugins)
β”œβ”€β”€ storage/cache/          # File cache store
β”œβ”€β”€ sdk/                    # Frontend SDK (React + Vanilla JS)
β”œβ”€β”€ locales/                # i18n dictionaries (en.json, th.json)
β”œβ”€β”€ genetics                # CLI tool
β”œβ”€β”€ public/                 # Web root (index.php)
└── .env                    # Environment config

πŸ”₯ Feature Highlights

Query Builder (v2.2)

$users = DB::table('users')
    ->select(['id', 'email', 'role_name'])
    ->where('is_active', 1)
    ->where('tenant_id', $tenantId)
    ->orderBy('created_at', 'DESC')
    ->paginate(20);

Middleware Pipeline

#[Middleware('auth:ADMIN', 'rate:10,60')]
class AdminProducts { ... }

Guard::check() β€” Optional Auth (v2.2)

if (Guard::check()) {
    $user = Guard::user(); // personalized response
} else {
    // public response
}

Database Migrations

php genetics migrate            # Run pending
php genetics migrate:rollback   # Undo last batch
php genetics make:migration add_image_to_products

Testing Framework

class ProductsTest extends GeneticTestCase {
    public function testList(): void {
        $this->actingAsAdmin();
        $this->seed(['products' => [['name' => 'Test', 'active' => 1]]]);
        $this->get('/api/products')->assertOk()->assertPaginated();
    }
}

Field Selector (GraphQL-lite)

GET /api/products?fields=id,name,price,category.name

Real-time SSE

Pulse::broadcast('orders', ['id' => 123, 'status' => 'new']);
// Client: const es = new EventSource('/api/events');

Marketplace

php genetics market:search notifications
php genetics market:install og/notifications

πŸ“š Documentation

Full documentation: open docs/site/overview.html or browse the 26-page docs site.

# Regenerate docs
cd docs/_tools && python3 _gen_pages.py

πŸ›‘οΈ Security

Report vulnerabilities to [email protected] β€” do not use the issue tracker.

πŸ“„ License

MIT License β€” free and open-source. See LICENSE.