| 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 |
* Read data from 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 Reader { |
| 28 |
/** |
| 29 |
* Open a file and return a subclass instance |
| 30 |
* |
| 31 |
* @param string $fileName |
| 32 |
* @return Reader |
| 33 |
*/ |
| 34 |
public static function open( $fileName ): Reader { |
| 35 |
return self::haveExtension() ? |
| 36 |
new Reader\DBA( $fileName ) : |
| 37 |
new Reader\PHP( $fileName ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns true if the native extension is available |
| 42 |
* |
| 43 |
* @return bool |
| 44 |
* @codeCoverageIgnore |
| 45 |
*/ |
| 46 |
public static function haveExtension(): bool { |
| 47 |
if ( !function_exists( 'dba_handlers' ) ) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
$handlers = dba_handlers(); |
| 51 |
|
| 52 |
return in_array( 'cdb', $handlers ) && in_array( 'cdb_make', $handlers ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Close the file. Optional, you can just let the variable go out of scope. |
| 57 |
*/ |
| 58 |
abstract public function close(): void; |
| 59 |
|
| 60 |
/** |
| 61 |
* Get a value with a given key. Only string values are supported. |
| 62 |
* |
| 63 |
* @param string|int $key |
| 64 |
* @return string|false |
| 65 |
*/ |
| 66 |
abstract public function get( $key ); |
| 67 |
|
| 68 |
/** |
| 69 |
* Check whether key exists |
| 70 |
* |
| 71 |
* @param string $key |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
abstract public function exists( $key ): bool; |
| 75 |
|
| 76 |
/** |
| 77 |
* Fetch first key |
| 78 |
* |
| 79 |
* @return string|false |
| 80 |
*/ |
| 81 |
abstract public function firstkey(); |
| 82 |
|
| 83 |
/** |
| 84 |
* Fetch next key |
| 85 |
* |
| 86 |
* @return string|false |
| 87 |
*/ |
| 88 |
abstract public function nextkey(); |
| 89 |
} |