kodhe / driver
Modular PSR-4 compatible Driver Library for CodeIgniter 3
2.0.0
2026-09-28 18:25 UTC
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-09-29 21:14:42 UTC
README
Refactored modular driver library system for CodeIgniter 3 dengan PSR-4 autoloading dan design patterns modern.
๐ Struktur
driver/
โโโ src/
โ โโโ Contracts/
โ โ โโโ DriverInterface.php # Interface untuk setiap driver
โ โ โโโ DriverLibraryInterface.php # Interface untuk parent library
โ โโโ AbstractDriver.php # Base abstract class untuk drivers
โ โโโ DriverLibrary.php # Base class untuk parent library
โ โโโ NullDriver.php # Null object pattern fallback
โ โโโ Resolvers/
โ โ โโโ DriverFileResolver.php # Resolve & load driver files
โ โโโ Registry/
โ โ โโโ DriverInstanceRegistry.php # Registry pattern untuk caching instances
โ โโโ Traits/
โ โโโ ConfigurableTrait.php # Configuration handling
โ โโโ MagicCallTrait.php # Magic method delegation
โโโ tests/
โ โโโ DriverLibraryTest.php
โ โโโ AbstractDriverTest.php
โ โโโ Resolvers/
โ โ โโโ DriverFileResolverTest.php
โ โโโ Registry/
โ โโโ DriverInstanceRegistryTest.php
โโโ composer.json
โจ Fitur
- PSR-4 Autoloading - Namespace
Kodhe\Driver\ - PSR-12 Coding Standards - Clean code style
- Design Patterns:
- Registry Pattern (instance caching)
- Factory Pattern (driver instantiation)
- Proxy/Delegation Pattern (magic methods)
- Template Method Pattern (abstract driver)
- Null Object Pattern (fallback driver)
- Dependency Injection - Resolver dan registry di-inject via constructor
- Performance Optimized:
- Path caching untuk file lookup
- Instance caching per request
- Lazy loading drivers
- 100% Backward Compatible - API CI3 tetap berfungsi
๐ง Instalasi
composer require kodhe/driver:^2.0
Atau tambahkan ke composer.json:
{
"require": {
"kodhe/driver": "^2.0"
}
}
๐ Penggunaan
Membuat Library Multi-Driver
// application/libraries/Cache.php class Cache extends \Kodhe\Driver\DriverLibrary { protected $valid_drivers = ['file', 'apcu', 'redis', 'memcached']; public function __construct() { parent::__construct(); } }
Membuat Driver
// application/libraries/Cache/drivers/Cache_file.php class Cache_file extends \Kodhe\Driver\AbstractDriver { public function isSupported(): bool { return is_writable(APPPATH . 'cache/'); } public function get($key) { // Implementation } public function save($key, $data, $ttl = 60) { // Implementation } }
Menggunakan di Controller
class UserController extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('cache'); } public function index() { // Akses driver via magic getter (lazy loaded) $data = $this->cache->file->get('user_data'); // Atau explicit load $this->cache->load_driver('file'); $data = $this->cache->file->get('user_data'); // Cek apakah driver supported if ($this->cache->file->isSupported()) { $this->cache->file->save('key', 'value', 300); } } }
๐ฏ Design Patterns
Registry Pattern
$registry = new DriverInstanceRegistry(); $registry->set('Cache', 'file', $driverInstance); $cached = $registry->get('Cache', 'file'); // Returns cached instance
Null Object Pattern
// Fallback aman ketika driver tidak ditemukan $driver = $this->isValidDriver($name) ? $this->load_driver($name) : new NullDriver();
Dependency Injection
$registry = new DriverInstanceRegistry(); $resolver = new DriverFileResolver(); $library = new MyLibrary($registry, $resolver);
๐งช Testing
composer test
composer test:coverage
๐ Perbandingan
| Area | Sebelum (CI3 Native) | Sesudah (Kodhe\Driver) |
|---|---|---|
| Struktur | 2 classes monolithic | Modular (8+ components) |
| Namespace | None | PSR-4 Kodhe\Driver\ |
| Testing | Sulit (hardcoded paths) | Mudah (DI + interfaces) |
| Caching | Manual per library | Built-in registry |
| Extension | Sulit | Mudah (implement interface) |
| PSR Compliance | None | PSR-4, PSR-12 |
๐ Backward Compatibility
Semua fitur CI3 Driver system tetap berfungsi:
- โ
extends CI_Driver_Libraryโextends DriverLibrary - โ
extends CI_Driverโextends AbstractDriver - โ
$this->lib->driver_name->method() - โ
$this->parent->method()dari dalam driver - โ
Property
valid_driversdanlib_name - โ
Magic methods
__get,__call
Class aliases tersedia untuk backward compatibility penuh:
// Di compat.php (optional) class_alias(\Kodhe\Driver\DriverLibrary::class, 'CI_Driver_Library'); class_alias(\Kodhe\Driver\AbstractDriver::class, 'CI_Driver');
๐ License
MIT License - lihat LICENSE untuk detail.