<?php

namespace Neag\MainBundle\Cache;

use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException;

class CacheManager
{
    // ??????????
    const DIR = '../app/site_cache/';
    
    /**
     * ?????? Filesystem
     *
     * @var       object
     * @access    protected
     */
    protected $fs;
    
    /**
     * ??????????? ??????
     *
     * @access      public
     */
    public function __construct ()
    {
        $this->fs = new Filesystem();
    }

    /**
     * ???????? ?????????? ??????????
     *
     * @access      public
     */
    public function tidy ()
    {
        $entries = scandir(self::DIR);
        foreach ($entries as $entry) {
            $filepath = self::DIR.$entry;
            if (@is_file($filepath)) {
                $time = @filemtime($filepath);
                // todo: ????? ????? ?????????? ????? ????????? ????, ???????????? ??????? ?????
                // ????? ???-?? ??????? ????? ????? ???? ?????
            }
        }
    }
    
    /**
     * J?????? ???? ?? ?????
     *
     * @access      public
     */
    public function clear ($token)
    {
        $fs = $this->fs;
        $filepath = self::DIR.$token;
        try {
            $fs->remove($filepath);
        } catch (Exception $e) {
            
        } 
        //@unlink(self::DIR.$token);        
    }    

    /**
     * ?????? ??????? ????
     *
     * @access      public
     */
    public function clearAll ()
    {
        $fs = $this->fs;
        $entries = scandir(self::DIR);
        foreach ($entries as $entry) {
            $filepath = self::DIR.$entry;
            try {
                $fs->remove($filepath);
            } catch (Exception $e) {
                
            }
            /*
            if (@is_file($filepath)) {
                @unlink($filepath);
            }
            */
        }
    }                                         

}