<?php

namespace WPIS;

class CacheManager {

    private $directory = null;
    private $expiration = 0;

    public function __construct($directory) {


        $this->directory = $directory;

        if( ! is_dir($this->directory = $directory) ){
            mkdir($this->directory = $directory);
        }
    }

    public function set($key, $content, $expiration = null) {

        // if( file_exists($this->directory . '/' . $key) ){
        //     return false;
        // }

        file_put_contents ( $this->directory . '/' . $key, serialize($content) );

        return true;
    }

    public function get($key) {

        if( ! file_exists($this->directory . '/' . $key) ){
            return false;
        }

        $content = file_get_contents( $this->directory . '/' . $key );
        $content = unserialize($content);

        return $content;
    }

    public function delete($key) {

        if( ! file_exists($this->directory . '/' . $key) ){
            return false;
        }

        unlink($this->directory . '/' . $key);

        return true;
    }

    public function flush() {

        $files = glob( $this->directory . '/*');

        foreach($files as $file) {
            if( is_file($file) ) {
                unlink($file);
            }
        }
    }
}