1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21: 
 22: 
 23: namespace picon;
 24: 
 25:  26:  27:  28:  29:  30:  31:  32: 
 33: class CacheManager
 34: {
 35:     const EXTENSION = '.pcx';
 36:     const SESSION_PATH = 'session_data';
 37:     const APPLICATION_PATH = 'application_data';
 38:     
 39:     const APPLICATION_SCOPE = '1';
 40:     const SESSION_SCOPE = '2';
 41:     
 42:     private static $self;
 43:     
 44:     private function __construct()
 45:     {
 46:         
 47:     }
 48:     
 49:      50:  51:  52:  53:  54: 
 55:     public static function saveResource($name, $resource, $scope)
 56:     {
 57:         Args::isString($name, 'name');
 58:         $instance = self::get();
 59:         $dir = $instance->getDirectoryForScope($scope);
 60:         $instance->internalSaveResource($dir, $name, $resource);
 61:     }
 62:     
 63:      64:  65:  66:  67:  68:  69: 
 70:     public static function loadResource($name, $scope)
 71:     {
 72:         $instance = self::get();
 73:         $directory = $instance->getDirectoryForScope($scope);
 74:         return $instance->internalLoadResource($directory, $name);
 75:     }
 76:     
 77:     private static function get()
 78:     {
 79:         if(!isset(self::$self))
 80:         {
 81:             self::$self = new self();
 82:         }
 83:         return self::$self;
 84:     }
 85:     
 86:     private function getDirectoryForScope($scope)
 87:     {
 88:         $dir = null;
 89:         if($scope==self::SESSION_SCOPE)
 90:         {
 91:             $dir = self::get()->getSessionCacheDirectory();
 92:         }
 93:         else if($scope==self::APPLICATION_SCOPE)
 94:         {
 95:             $dir = self::get()->getApplicationCacheDirectory();
 96:         }
 97:         else
 98:         {
 99:             throw new \InvalidArgumentException('Invalid scope argument for saveResource()');
100:         }
101:         return $dir;
102:     }
103:     
104:     private function getSessionCacheDirectory()
105:     {
106:         return CACHE_DIRECTORY.'/'.self::SESSION_PATH.'/'.session_id().'/';
107:     }
108:     
109:     private function getApplicationCacheDirectory()
110:     {
111:         return CACHE_DIRECTORY.'/'.self::APPLICATION_PATH.'/';
112:     }
113:     
114:     private function getFileName($directory, $name)
115:     {
116:         return $directory.$this->sanitizeFileName($name).self::EXTENSION;
117:     }
118:     
119:     private function internalSaveResource($directory, $name, $resource)
120:     {
121:         $fileName = $this->getFileName($directory, $name);
122:         
123:         if(!file_exists($directory))
124:         {
125:             mkdir($directory, 0755, true);
126:         }
127:         
128:         file_put_contents($fileName, PiconSerializer::serialize($resource));
129:     }
130:     
131:     private function internalLoadResource($directory, $name)
132:     {
133:         $fileName = $this->getFileName($directory, $name);
134:         
135:         if(file_exists($fileName))
136:         {
137:             $contents = file_get_contents($fileName);
138:             return PiconSerializer::unserialize($contents);
139:         }
140:         else
141:         {
142:             return null;
143:         }
144:     }
145:     
146:     147: 148: 149: 150: 151: 
152:     public static function resourceExists($name, $scope)
153:     {
154:         $dir = self::get()->getDirectoryForScope($scope);
155:         $fileName = self::get()->getFileName($dir, $name);
156:         
157:         return file_exists($fileName);
158:     }
159:     
160:     private function sanitizeFileName($fileName)
161:     {
162:         return str_replace(':', '_', $fileName);
163:     }
164: }
165: 
166: ?>
167: