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: 8

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-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_drivers dan lib_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.