| 1 |
<?php |
| 2 |
/** |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
* http://www.gnu.org/copyleft/gpl.html |
| 17 |
*/ |
| 18 |
|
| 19 |
namespace Cdb; |
| 20 |
|
| 21 |
/** |
| 22 |
* Write to a CDB file. |
| 23 |
* Native C and pure PHP implementations are provided. |
| 24 |
* |
| 25 |
* @see http://cr.yp.to/cdb.html |
| 26 |
*/ |
| 27 |
abstract class Writer { |
| 28 |
/** |
| 29 |
* File we'll be writing to when we're done |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $realFileName; |
| 33 |
|
| 34 |
/** |
| 35 |
* File we write to temporarily until we're done |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $tmpFileName; |
| 39 |
|
| 40 |
/** |
| 41 |
* Open a writer and return a subclass instance. |
| 42 |
* The user must have write access to the directory, for temporary file creation. |
| 43 |
* |
| 44 |
* @param string $fileName |
| 45 |
* @return Writer |
| 46 |
*/ |
| 47 |
public static function open( $fileName ) { |
| 48 |
return Reader::haveExtension() ? |
| 49 |
new Writer\DBA( $fileName ) : |
| 50 |
new Writer\PHP( $fileName ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Set a key to a given value. The value will be converted to string. |
| 55 |
* |
| 56 |
* @param string $key |
| 57 |
* @param string $value |
| 58 |
*/ |
| 59 |
abstract public function set( $key, $value ): void; |
| 60 |
|
| 61 |
/** |
| 62 |
* Close the writer object. You should call this function before the object |
| 63 |
* goes out of scope, to write out the final hashtables. |
| 64 |
*/ |
| 65 |
abstract public function close(): void; |
| 66 |
|
| 67 |
/** |
| 68 |
* If the object goes out of scope, close it |
| 69 |
*/ |
| 70 |
public function __destruct() { |
| 71 |
$this->close(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Are we running on Windows? |
| 76 |
* @return bool |
| 77 |
*/ |
| 78 |
protected function isWindows() { |
| 79 |
return strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN'; |
| 80 |
} |
| 81 |
} |