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-10-28 13:25:11 UTC
README
A refactored, modular driver library system for CodeIgniter 3 with PSR-4 autoloading and modern design patterns.
๐ Structure
driver/
โโโ src/
โ โโโ Contracts/
โ โ โโโ DriverInterface.php # Interface implemented by every driver
โ โ โโโ DriverLibraryInterface.php # Interface for the parent library
โ โโโ AbstractDriver.php # Base abstract class for drivers
โ โโโ DriverLibrary.php # Base class for the parent library
โ โโโ NullDriver.php # Null object pattern fallback
โ โโโ Resolvers/
โ โ โโโ DriverFileResolver.php # Resolves & loads driver files
โ โโโ Registry/
โ โ โโโ DriverInstanceRegistry.php # Registry pattern for 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
โจ Features
- 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 and registry are injected via the constructor
- Performance Optimized:
- Path caching for file lookup
- Instance caching per request
- Lazy loading drivers
- 100% Backward Compatible - The CI3 API keeps working
๐ง Installation
composer require kodhe/driver:^2.0
Or add it to composer.json:
{
"require": {
"kodhe/driver": "^2.0"
}
}
๐ Usage
Creating a Multi-Driver Library
// application/libraries/Cache.php class Cache extends \Kodhe\Driver\DriverLibrary { protected $valid_drivers = ['file', 'apcu', 'redis', 'memcached']; public function __construct() { parent::__construct(); } }
Creating a 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 } }
Using It in a Controller
class UserController extends CI_Controller { public function __construct() { parent::__construct(); $this->load->library('cache'); } public function index() { // Access the driver via the magic getter (lazy loaded) $data = $this->cache->file->get('user_data'); // Or load it explicitly $this->cache->load_driver('file'); $data = $this->cache->file->get('user_data'); // Check whether the driver is 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
// Safe fallback when a driver is not found $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
๐ Comparison
| Area | Before (CI3 native) | After (Kodhe\Driver) |
|---|---|---|
| Structure | 2 monolithic classes | Modular (8+ components) |
| Namespace | None | PSR-4 Kodhe\Driver\ |
| Testing | Hard (hardcoded paths) | Easy (DI + interfaces) |
| Caching | Manual per library | Built-in registry |
| Extensibility | Hard | Easy (implement an interface) |
| PSR compliance | None | PSR-4, PSR-12 |
๐ Backward Compatibility
All features of the CI3 driver system keep working:
- โ
extends CI_Driver_Libraryโextends DriverLibrary - โ
extends CI_Driverโextends AbstractDriver - โ
$this->lib->driver_name->method() - โ
$this->parent->method()from within a driver - โ
The
valid_driversandlib_nameproperties - โ
Magic methods
__get,__call
Class aliases are available for full backward compatibility:
// In compat.php (optional) class_alias(\Kodhe\Driver\DriverLibrary::class, 'CI_Driver_Library'); class_alias(\Kodhe\Driver\AbstractDriver::class, 'CI_Driver');
๐ License
MIT License - see LICENSE for details.