1: <?php
2: /**
3: * Created by PhpStorm.
4: * User: manus
5: * Date: 29/03/16
6: * Time: 12:00
7: */
8:
9: namespace GLFramework;
10:
11:
12: use TijsVerkoyen\CssToInlineStyles\Exception;
13:
14: class Filesystem
15: {
16:
17: private $file;
18:
19: private $pfile;
20: /**
21: * Generara un gestor de archivos
22: * @param $file
23: */
24: public function __construct($file = null)
25: {
26: $this->file = $file;
27: }
28:
29: /**
30: * Solicita un nuevo archivo con ese nombre y extension, si no se indica un nombre, se genera
31: * uno de forma aleatoria.
32: * @param null $filename
33: * @param string $extension
34: * @return Filesystem
35: */
36: public function allocate($filename = null, $extension = ".rnd")
37: {
38: if($filename == null)
39: $filename = sha1(time() . "_" . microtime(true));
40: $file = new Filesystem("{$filename}{$extension}"); //$this->getStorage() . "/{$filename}{$extension}";
41: $file->touch();
42: return $file;
43: }
44:
45: private function getStorage()
46: {
47: $folder = $this->getFilesystemFolder();
48: if(!is_dir($folder))
49: {
50:
51: if(!mkdir($folder, 0777, true))
52: {
53: throw new Exception("Can not create Filesystem folder: '" . $folder . "'. Please verify permissions.");
54: }
55: }
56:
57: return $folder;
58: }
59:
60: /**
61: * Obtiene la ruta al directorio donde se almacenan los archivos
62: * @return string
63: */
64: public function getFilesystemFolder()
65: {
66:
67: $config = Bootstrap::getSingleton()->getConfig();
68: if(isset($config['app']['filesystem']))
69: {
70: return $config['app']['filesystem'];
71: }
72: return "filesystem";
73: }
74:
75: /**
76: * Obtiene la ruta relativa al archivo
77: * @return string
78: * @throws Exception
79: */
80: public function getFilePath()
81: {
82: return $this->getStorage() . "/" . $this->file;
83: }
84:
85: /**
86: * Obtiene la rut absoluta al archivo
87: * @return string
88: * @throws Exception
89: */
90: public function getAbsolutePath()
91: {
92: return realpath($this->getStorage()) . "/" . $this->file;
93: }
94:
95: /**
96: * Crea el archivo vacio
97: * @return bool
98: */
99: public function touch()
100: {
101: return touch($this->getAbsolutePath());
102: }
103:
104: /**
105: * Devuelve true si existe el archivo
106: * @return bool
107: */
108: public function exists()
109: {
110: return file_exists($this->getAbsolutePath());
111: }
112:
113: /**
114: * Obtiene una url accesible por el navegador
115: * @return string
116: */
117: public function url()
118: {
119: $scheme = "http://";
120: if($_SERVER['HTTPS']) $scheme = "https://";
121: return $scheme . $_SERVER['HTTP_HOST'] . "/" . $this->getFilePath();
122: }
123:
124: /**
125: * Abrir un archivo, devuelve el puntero al archio
126: * @param string $mode
127: * @return resource
128: */
129: public function open($mode = "rw")
130: {
131: return ($this->pfile = fopen($this->getAbsolutePath(), $mode));
132: }
133:
134: /**
135: * Cierra el Ășltimo puntero abiero
136: * @return bool
137: */
138: public function close()
139: {
140: return fclose($this->pfile);
141: }
142:
143: /**
144: * Leer el contenido del archivo. No se requiere ejecutar open() ni close()
145: * @return string
146: */
147: public function read()
148: {
149: return file_get_contents($this->getAbsolutePath());
150: }
151:
152: /**
153: * Escribir el contenido al archivo. No se requiere ejecutar open() ni close()
154: * @param $content
155: * @return int
156: */
157: public function write($content)
158: {
159: return file_put_contents($this->getAbsolutePath(), $content);
160: }
161:
162: }