PHP code example of kodhe / driver

1. Go to this page and download the library: Download kodhe/driver library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kodhe / driver example snippets


// application/libraries/Cache.php
class Cache extends \Kodhe\Driver\DriverLibrary
{
    protected $valid_drivers = ['file', 'apcu', 'redis', 'memcached'];
    
    public function __construct()
    {
        parent::__construct();
    }
}

// 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
    }
}

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);
        }
    }
}

$registry = new DriverInstanceRegistry();
$registry->set('Cache', 'file', $driverInstance);
$cached = $registry->get('Cache', 'file'); // Returns cached instance

// Fallback aman ketika driver tidak ditemukan
$driver = $this->isValidDriver($name) 
    ? $this->load_driver($name) 
    : new NullDriver();

$registry = new DriverInstanceRegistry();
$resolver = new DriverFileResolver();
$library = new MyLibrary($registry, $resolver);

// Di compat.php (optional)
class_alias(\Kodhe\Driver\DriverLibrary::class, 'CI_Driver_Library');
class_alias(\Kodhe\Driver\AbstractDriver::class, 'CI_Driver');