maatify / data-adapters
Unified data connectivity and diagnostics layer for Redis, MySQL, and MongoDB with automatic fallback recovery, telemetry-ready metrics, and environment auto-detection. Forms the foundation of the Maatify Data Infrastructure.
Installs: 396
Dependents: 2
Suggesters: 1
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/maatify/data-adapters
Requires
- php: >=8.4
- maatify/bootstrap: ^1.0
- maatify/common: ^1.0
Requires (Dev)
- doctrine/dbal: ^4.3
- friendsofphp/php-cs-fixer: ^3.89
- maatify/rate-limiter: dev-main
- mongodb/mongodb: ^2.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^10.0
- predis/predis: ^2.3
- vlucas/phpdotenv: ^5.6
Suggests
- ext-mongodb: Required for MongoAdapter
- ext-pdo: Required for MySQLAdapter (PDO driver)
- ext-redis: or high-performance native RedisAdapter
- doctrine/dbal: Optional advanced SQL driver for MySQLDbalAdapter
- mongodb/mongodb: Required only if you plan to use MongoAdapter for MongoDB integration.
- predis/predis: Used as fallback when ext-redis is unavailable
README
π¦ maatify/data-adapters
Unified Data Connectivity & Diagnostics Layer
π§ Overview
maatify/data-adapters is a unified, framework-agnostic layer for managing Redis, MongoDB,
and MySQL connections with centralized diagnostics and auto-detection.
It serves as the core data layer of the Maatify Ecosystem.
βοΈ Installation
composer require maatify/data-adapters
Requirements:
β’ PHP β₯ 8.4
β’ Redis (phpredis recommended β Predis auto-fallback)
β’ MongoDB extension (optional)
β’ PDO MySQL required (DBAL optional)
β¨ Features
- Unified configuration engine (Phase 13)
- Registry β DSN β Legacy priority system
- Fully unified config builders for MySQL / MongoDB / Redis
- Multi-profile MySQL & MongoDB (unlimited profiles)
- Redis unified builder (future multi-profile ready)
- Centralized diagnostics & health checks
- Unified raw driver layer (
getDriver()) for PDO / DBAL / MongoDB / Redis - Full DSN stabilization (PDO-style + Doctrine URL-style)
π Why Choose maatify/data-adapters?
maatify/data-adapters is not just another database wrapper β
it is a unified, production-ready connectivity engine powering the entire Maatify Ecosystem.
Hereβs why developers and teams prefer it:
π 1. One Resolver for All Data Sources
Connect to MySQL (PDO/DBAL), MongoDB, Redis, and Predis using the same API:
$adapter = $resolver->resolve('mysql.main');
No manual wiring, no duplicate logic β one resolver handles everything.
π§ 2. DSN-First Architecture (PDO + Doctrine + Mongo + Redis)
Phase 13 introduced a complete DSN engine:
mysql://user:pass@host:3306/dbmongodb://host:27017/adminredis://:password@host:6379/2
This ensures clean configuration, multi-profile support, and zero boilerplate.
π 3. Multi-Profile Support (Unlimited Profiles)
mysql.main mysql.logs mysql.analytics mongo.activity redis.cache
Run isolated databases per module with effortless switching.
π§© 4. Raw Driver Access (Phase 15)
Need native database power?
$pdo = $mysql->getDriver(); // PDO $db = $mongo->getDriver(); // MongoDB\Database $rd = $redis->getDriver(); // Redis or Predis\Client
Perfect for advanced queries, analytics, or legacy migrations.
π©Ί 5. Built-In Diagnostics & Telemetry
Each adapter includes:
healthCheck()- latency metrics
- Prometheus-ready telemetry
- structured log context
- fallback logging
Ideal for microservices & modern cloud infrastructure.
π‘ 6. Bulletproof Configuration Resolution
A 3-layer priority model:
REGISTRY β DSN β Legacy environment variables
This gives the library:
- β Predictable behavior
- β Zero config duplication
- β Clean environment files
- β Dynamic overrides in production
π§ͺ 7. Fully Tested β 93% Coverage
Test suite includes:
- DSN parsing
- Registry merging
- Multi-profile routing
- Raw driver access
- Diagnostics & telemetry
- Redis/Mongo/MySQL integration
CI is stable and GitHub Actions validated.
π₯ 8. Framework-Agnostic
Works with:
- maatify/bootstrap
- maatify/security-guard
- maatify/rate-limiter
- maatify/mongo-activity
- Laravel, Symfony, Slim, custom frameworks
You own the stack β the library adapts to you.
π 9. Enterprise-Grade Production Stability
Phase 15β17 implemented:
- DSN stabilization
- Doctrine URL normalization
- strict typing (PHPStan MAX)
- DBAL safety
- CI stability patch
- cross-platform reliability (Linux/macOS/CI)
This is not hobby code β itβs ecosystem infrastructure.
π 10. Designed for Multi-Service Architectures
Supports:
- microservices
- containers & Docker
- Kubernetes
- cloud deployments
- serverless adapters
- distributed logging
- cross-service caches
Your data layer becomes scalable, testable, and predictable.
π₯ In shortβ¦
If you want Redis, MongoDB, and MySQL to behave like a single unified system β this is the library.
π₯ New in Phase 13 β Unified Configuration Architecture
Phase 13 finalizes the unified configuration engine across all adapters.
β Global Configuration Priority
Registry β DSN β Legacy (Deprecated)
This applies consistently to MySQL, MongoDB, and Redis.
β Unified Builder Behavior (Final)
All builders now:
- Return fully normalized configuration objects
- Use identical DSN parsing rules
- Support unlimited profiles (
mysql.main,mongo.logs, β¦) - Merge configuration with the same priority logic
- Expose driver + profile metadata
β Registry JSON Support
A new registry.json file allows runtime overrides:
{
"mysql": {
"main": { "user": "override_user" }
},
"redis": {
"cache": { "host": "10.0.0.1", "port": 6380 }
}
}
This overrides DSN & legacy variables automatically.
β Redis Builder Unified
The Redis builder has been rewritten to match MySQL/Mongo logic and is now future-ready for multi-profile support.
π₯ New in Phase 15 β Raw Driver Layer + DSN Stabilization
Phase 15 introduces a unified low-level driver access layer and fully stabilizes DSN parsing across all adapters (PDO, Doctrine, Mongo, Redis).
This phase ensures every adapter exposes its native underlying driver safely:
| Adapter | raw driver (getDriver()) |
|---|---|
| MySQL (PDO) | PDO |
| MySQL (DBAL) | Doctrine\DBAL\Connection |
| MongoDB | MongoDB\Database |
| Redis | Redis or Predis\Client |
β Unified Raw Driver Access (getDriver())
Every adapter now supports:
$pdo = $mysql->getDriver(); // PDO $dbal = $mysqlDbal->getDriver(); // Doctrine Connection $mongo = $mongoMain->getDriver(); // MongoDB Database $redis = $redisCache->getDriver(); // Redis or Predis
Perfect for:
- Building your own query layers
- Passing native connections into other libraries
- High-performance custom operations
β Full DSN Stabilization (PDO + Doctrine)
Phase 15 completes the DSN architecture:
PDO-style DSNs
mysql:host=127.0.0.1;port=3306;dbname=demo
Doctrine URL DSNs
mysql://user:P%40ss%3B@10.0.0.5:3306/mydb
β Special characters now parsed correctly
β Passwords preserved safely (encoded/decoded)
β Unified parser for both formats
β No more parse_url() issues
β Accurate Driver Routing (driver=pdo / driver=dbal)
Profiles can now explicitly choose driver:
MYSQL_MAIN_DRIVER=pdo
MYSQL_LOGS_DRIVER=dbal
MYSQL_REPORTING_DRIVER=dbal
Or DSN auto-detects driver automatically.
β Real MySQL Dual-Driver Test (Local + CI)
Phase 15 upgrades the integration tests so they:
- Load real
.envvalues - Support DSN override via
putenv() - Work in both CI and local development
- Auto-detect PDO and DBAL correctness
- Skip only when connection fails intentionally
β Improved Config Normalization
MySqlConfigBuilder / MongoConfigBuilder / RedisConfigBuilder now guarantee:
- No null database issues
- No partial DSN parsing
- No empty user/pass fallback bugs
- No malformed DSN from registry merge
β Raw Driver Tests Added
RawDriverRoutingTestRawAccessTestMysqlDsnParserTest(enhanced)
All confirming:
- Correct low-level driver type
- Correct DSN behavior
- Correct profile routing
- Correct priority merge
π¦ Summary of Phase 15
β Raw driver access layer β Stable DSN parsing for all formats β Reliable driver routing β Full MySqlConfigBuilder normalization β Raw-access tests + DSN tests + dual-driver tests β Architecture ready for Failover Routing (Phase 16)
π§© Compatibility
Fully framework-agnostic.
Optional auto-wiring available via maatify/bootstrap.
- Fully compatible with the new Phase 13 unified configuration engine
- Supports runtime overrides through registry.json
π Quick Usage (Updated for Phase 15 β Raw Driver Layer + DSN Stabilization)
use Maatify\DataAdapters\Core\EnvironmentConfig; use Maatify\DataAdapters\Core\DatabaseResolver; $config = new EnvironmentConfig(__DIR__); // Phase 13: Registry β DSN β Legacy $resolver = new DatabaseResolver($config); // ------------------------------------ // π£ MySQL β Multi-Profile (Phase 13) // ------------------------------------ $mainDb = $resolver->resolve("mysql.main", autoConnect: true); $logsDb = $resolver->resolve("mysql.logs"); $billingDb = $resolver->resolve("mysql.billing"); // unlimited profiles $stmt = $mainDb->getConnection()->query("SELECT 1"); echo $stmt->fetchColumn(); // 1 // ------------------------------------ // π’ MongoDB β Multi-Profile (Phase 13) // ------------------------------------ $mongoMain = $resolver->resolve("mongo.main", autoConnect: true); $mongoLogs = $resolver->resolve("mongo.logs"); $db = $mongoMain->getConnection()->selectDatabase("admin"); $ok = $db->command(["ping" => 1])->toArray()[0]["ok"]; echo $ok; // 1 // ------------------------------------ // π΄ Redis β Unified Builder (Phase 13) // ------------------------------------ $redis = $resolver->resolve("redis.cache", autoConnect: true); $redis->getConnection()->set("key", "maatify"); echo $redis->getConnection()->get("key"); // maatify // ------------------------------------ // π Debug Final Merged Configuration // (Phase 13 unified DTO output) // ------------------------------------ print_r( $mainDb->debugConfig()->toArray() ); /* Output example: [ "dsn" => "mysql://user:pass@127.0.0.1:3306/main", "host" => "127.0.0.1", "port" => "3306", "user" => "user", "pass" => "pass", "database" => "main", "driver" => "pdo", "profile" => "main" ] */ // ------------------------------------ // π¦ Registry Override Example // registry.json: // { // "mysql": { "main": { "user": "override_user" } } // } // ------------------------------------ $mainDbFromRegistry = $resolver->resolve("mysql.main"); print_r($mainDbFromRegistry->debugConfig()->user); // override_user // ------------------------------------ // π Raw Driver Access (Phase 15) // ------------------------------------ $native = $mainDb->getDriver(); // PDO or Doctrine Connection var_dump($native instanceof PDO); // true (example) $mongoNative = $mongoMain->getDriver(); // MongoDB\Database $redisNative = $redis->getDriver(); // Redis or Predis\Client
π§© Diagnostics & Health Checks
All adapters include self-diagnostic capabilities and unified health reporting.
use Maatify\DataAdapters\Diagnostics\DiagnosticService; $diagnostic = new DiagnosticService($config, $resolver); echo $diagnostic->toJson();
Example Output
{
"diagnostics": [
{"adapter": "redis", "connected": true},
{"adapter": "mongo", "connected": true},
{"adapter": "mysql", "connected": true}
]
}
π§ͺ Testing
vendor/bin/phpunit
- Added Phase 15 tests:
- RawDriverRoutingTest
- RawAccessTest
- Enhanced DSN parsing tests
- Real MySQL dual-driver test (PDO + DBAL)
Coverage: β 93%
Status: β
All tests passing (DSN, registry, multi-profile, diagnostics, metrics)
Suites:
- Unit Tests
- Integration Tests
- DSN Parsing Tests
- Registry Merge Tests
- Multi-Profile MySQL & MongoDB Tests
- Redis Builder Tests
- Diagnostics & Metrics Tests
- Phase 15 introduced raw driver validation inside diagnostics, ensuring
accurate low-level connectivity (PDO/DBAL/MongoDB/Redis).
π Documentation
- Introduction:
docs/README.intro.md - Environment Reference:
docs/env.md - Telemetry:
docs/telemetry.md - Architecture:
docs/architecture.md - Multi-Profile MySQL:
docs/mysql-profiles.md - Usage Examples:
docs/USAGE.md - Phases:
docs/README.roadmap.md - Changelog:
CHANGELOG.md
π Related Maatify Libraries
- maatify/common
- maatify/psr-logger
- maatify/bootstrap
- maatify/rate-limiter
- maatify/security-guard
- maatify/mongo-activity
π Full documentation & release notes: see /docs/README.full.md
πͺͺ License
MIT license Β© Maatify.dev
Youβre free to use, modify, and distribute this library with attribution.
π€ Author
Mohamed Abdulalim β Backend Lead & Technical Architect
π https://www.maatify.dev | βοΈ mohamed@maatify.dev
π€ Contributors
Special thanks to the Maatify.dev engineering team and open-source contributors.
Built with β€οΈ by Maatify.dev β Unified Ecosystem for Modern PHP Libraries