1 : <?php
2 : /**
3 : * This file is part of NoiseLabs-PHP-ToolKit
4 : *
5 : * NoiseLabs-PHP-ToolKit is free software; you can redistribute it
6 : * and/or modify it under the terms of the GNU Lesser General Public
7 : * License as published by the Free Software Foundation; either
8 : * version 3 of the License, or (at your option) any later version.
9 : *
10 : * NoiseLabs-PHP-ToolKit is distributed in the hope that it will be
11 : * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 : * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : * Lesser General Public License for more details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public
16 : * License along with NoiseLabs-PHP-ToolKit; if not, see
17 : * <http://www.gnu.org/licenses/>.
18 : *
19 : * Copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
20 : *
21 : *
22 : * @category NoiseLabs
23 : * @package ConfigParser
24 : * @version 0.1.1
25 : * @author Vítor Brandão <noisebleed@noiselabs.org>
26 : * @copyright (C) 2011 Vítor Brandão <noisebleed@noiselabs.org>
27 : */
28 :
29 : namespace NoiseLabs\ToolKit\ConfigParser;
30 :
31 : class File
32 1 : {
33 : /**
34 : * Pathname
35 : */
36 : protected $_path;
37 :
38 : /**
39 : * File pointer to the given filename.
40 : */
41 : protected $_handle;
42 :
43 : /**
44 : * A list of possible modes for fopen():
45 : *
46 : * 'r': Open for reading only; place the file pointer at the beginning of
47 : * the file.
48 : *
49 : * 'r+': Open for reading and writing; place the file pointer at the
50 : * beginning of the file.
51 : *
52 : * 'w': Open for writing only; place the file pointer at the beginning of
53 : * the file and truncate the file to zero length. If the file does not
54 : * exist, attempt to create it.
55 : *
56 : * 'w+': Open for reading and writing; place the file pointer at the
57 : * beginning of the file and truncate the file to zero length. If the file
58 : * does not exist, attempt to create it.
59 : *
60 : * 'a': Open for writing only; place the file pointer at the end of the
61 : * file. If the file does not exist, attempt to create it.
62 : *
63 : * 'a+': Open for reading and writing; place the file pointer at the end
64 : * of the file. If the file does not exist, attempt to create it.
65 : *
66 : * 'x': Create and open for writing only; place the file pointer at the
67 : * beginning of the file. If the file already exists, the fopen() call will
68 : * fail by returning FALSE and generating an error of level E_WARNING. If
69 : * the file does not exist, attempt to create it. This is equivalent to
70 : * specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.
71 : *
72 : * 'x+': Create and open for reading and writing; otherwise it has the
73 : * same behavior as 'x'.
74 : *
75 : * 'c': Open the file for writing only. If the file does not exist, it is
76 : * created. If it exists, it is neither truncated (as opposed to 'w'), nor
77 : * the call to this function fails (as is the case with 'x'). The file
78 : * pointer is positioned on the beginning of the file. This may be useful
79 : * if it's desired to get an advisory lock (see flock()) before attempting
80 : * to modify the file, as using 'w' could truncate the file before the
81 : * lock was obtained (if truncation is desired, ftruncate() can be used
82 : * after the lock is requested).
83 : *
84 : * 'c+': Open the file for reading and writing; otherwise it has the
85 : * same behavior as 'c'.
86 : */
87 : protected $_mode;
88 :
89 : public function __construct($filename, $mode = 'rb')
90 : {
91 5 : $this->_path = $filename;
92 5 : $this->_mode = $mode;
93 5 : }
94 :
95 : public function getPathname()
96 : {
97 0 : return $this->_path;
98 : }
99 :
100 : public function open($mode = null)
101 : {
102 0 : if (!isset($mode)) {
103 0 : $mode = $this->_mode;
104 0 : }
105 :
106 0 : $this->_handle = fopen($this->_path, $mode);
107 :
108 0 : return $this->_handle;
109 : }
110 :
111 : public function write($content)
112 : {
113 0 : return ($this->_handle) ? fwrite($this->_handle, $content) : false;
114 : }
115 :
116 : public function close()
117 : {
118 0 : return fclose($this->_handle);
119 : }
120 :
121 : public function isReadable()
122 : {
123 0 : return is_readable($this->_path);
124 : }
125 :
126 : public function isWritable()
127 : {
128 0 : return is_writable($this->_path);
129 : }
130 :
131 : public function remove()
132 : {
133 0 : return unlink($this->_path);
134 : }
135 : }
136 :
|