Overview

Namespaces

  • Flea
  • None

Classes

  • Flea
  • Flea\BuildUtil
  • Flea\Cache
  • Flea\DataBase
  • Flea\DataBaseCRUD
  • Flea\DataList
  • Flea\DataUtil
  • Flea\Debug
  • Flea\FileUtil
  • Flea\General
  • Flea\Header
  • Flea\InitUtil
  • Flea\LangList
  • Flea\Login
  • Flea\LoginFormHelper
  • Flea\LoginTableName
  • Flea\LoginUser
  • Flea\Page
  • Flea\PageList
  • Flea\PageListCreate
  • Flea\SqlQuery
  • Flea\TagUtil
  • Flea\UrlUtil
  • Flea\ValueObject
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /*
  4:  * The MIT License
  5:  *
  6:  * Copyright 2014 Damien Doussaud (namide.com).
  7:  *
  8:  * Permission is hereby granted, free of charge, to any person obtaining a copy
  9:  * of this software and associated documentation files (the "Software"), to deal
 10:  * in the Software without restriction, including without limitation the rights
 11:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12:  * copies of the Software, and to permit persons to whom the Software is
 13:  * furnished to do so, subject to the following conditions:
 14:  *
 15:  * The above copyright notice and this permission notice shall be included in
 16:  * all copies or substantial portions of the Software.
 17:  *
 18:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24:  * THE SOFTWARE.
 25:  */
 26: 
 27: namespace Flea;
 28: 
 29: /**
 30:  * Storage datas without SQL
 31:  *
 32:  * @author Namide
 33:  */
 34: class DataUtil {
 35: 
 36:     /**
 37:      * Initialise the "connection" with the directory of data files
 38:      * 
 39:      * @param type $dir     Directory to save the files
 40:      */
 41:     public function __construct($dir) {
 42:         $this->_dir = $dir;
 43:     }
 44: 
 45:     private $_dir = '';
 46: 
 47:     /**
 48:      * Directory to save the files
 49:      * 
 50:      * @return string       Directory to save the files
 51:      */
 52:     public function getDir() {
 53:         return $this->_dir;
 54:     }
 55: 
 56:     /**
 57:      * Add a file in the datas
 58:      * 
 59:      * @param string $key       Key of the data to save (same that the name of a file)
 60:      * @param string $content   Content of the data to save
 61:      */
 62:     public function add($key, &$content) {
 63:         if (_DEBUG && $this->has($key)) {
 64:             Debug::getInstance()->addError('The content [dir:' . $this->_dir . ', key:' . $key . '] already exist');
 65:         }
 66:         FileUtil::writeFile($content, $this->_dir . $key);
 67:     }
 68: 
 69:     /**
 70:      * Add a JSON file in the datas
 71:      * 
 72:      * @param string $key       Key of the data saved (same that the name)
 73:      * @param array $content    Content of the data saved
 74:      */
 75:     public function addJson($key, array &$content) {
 76:         $json = json_encode($content);
 77:         $this->add($key, $json);
 78:     }
 79: 
 80:     /**
 81:      * Update a file in the datas
 82:      * 
 83:      * @param string $key           Key of the data saved (same that the name)
 84:      * @param string $content       New content of the data
 85:      */
 86:     public function update($key, &$content) {
 87:         if (_DEBUG && $this->has($key)) {
 88:             Debug::getInstance()->addError('The content [dir:' . $this->_dir . ', key:' . $key . '] don\'t exist');
 89:         }
 90:         $type = $this->get($key);
 91:         $this->delete($key);
 92:         $this->add($key, $content, $type);
 93:     }
 94: 
 95:     /**
 96:      * Update a JSON file in the datas
 97:      * 
 98:      * @param string $key       Key of the data saved (same that the name)
 99:      * @param array $content    New content of the data
100:      */
101:     public function updateJson($key, array &$content) {
102:         $json = json_encode($content);
103:         update($key, $json);
104:     }
105: 
106:     /**
107:      * Get the content of a file in the datas
108:      * 
109:      * @param string $key       Key of the data saved (same that the name)
110:      * @return string           Content of the data
111:      */
112:     public function get($key) {
113:         if (_DEBUG && !$this->has($key)) {
114:             Debug::getInstance()->addError('The content [dir:' . $this->_dir . ', key:' . $key . '] don\'t exist');
115:         }
116:         return file_get_contents($this->_dir . $key);
117:     }
118: 
119:     /**
120:      * Get the content of a JSON file in the datas
121:      * 
122:      * @param string $key       Key of the data saved (same that the name)
123:      * @return array            Content of the data
124:      */
125:     public function getJson($key) {
126:         $content = $this->get($key);
127:         return json_decode($content, true);
128:     }
129: 
130:     /**
131:      * Test if the file exist in the datas
132:      * 
133:      * @param string $key       Key of the data saved (same that the name)
134:      * @return bool             true if the data exist, otherwise false
135:      */
136:     public function has($key) {
137:         return file_exists($this->_dir . $key);
138:     }
139: 
140:     /**
141:      * Delete a file in the datas
142:      * 
143:      * @param string $key       Key of the files
144:      */
145:     public function delete($key) {
146:         if ($this->has($key)) {
147:             unlink($this->_dir . $key);
148:         }
149:     }
150: 
151:     /**
152:      * Echo the content of a file in the datas
153:      * 
154:      * @param string $key       Key of the data saved (same that the name)
155:      */
156:     public function render($key) {
157:         $ext = strtolower(substr(strrchr($key, '.'), 1));
158:         switch ($ext) {
159:             case "xml":
160:                 header('Content-Type: application/xml;');
161:                 break;
162:             //default: $ctype="application/force-download";
163:         }
164: 
165:         readfile($this->_dir . $key);
166:     }
167: 
168: }
169: 
Flea API documentation generated by ApiGen