Search by

kodhe / driver

karyakode

Modular PSR-4 compatible Driver Library for CodeIgniter 3

Package info

github.com/kodhe/driver

pkg:composer/kodhe/driver

Statistics

Installs: 14

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

2.0.0 2026-09-28 18:25 UTC

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_drivers and lib_name properties
  • โœ… 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.