Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
76.92% |
10 / 13 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
DBA | |
76.92% |
10 / 13 |
|
33.33% |
1 / 3 |
7.60 | |
0.00% |
0 / 1 |
__construct | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
close | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
4.37 |
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\Writer; |
20 | |
21 | use Cdb\Exception; |
22 | use Cdb\Writer; |
23 | |
24 | /** |
25 | * Writer class which uses the DBA extension (php-dba) |
26 | */ |
27 | class DBA extends Writer { |
28 | /** |
29 | * The file handle |
30 | */ |
31 | protected $handle; |
32 | |
33 | /** |
34 | * Create the object and open the file. |
35 | * |
36 | * @param string $fileName |
37 | */ |
38 | public function __construct( string $fileName ) { |
39 | $this->realFileName = $fileName; |
40 | $this->tmpFileName = $fileName . '.tmp.' . mt_rand( 0, 0x7fffffff ); |
41 | $this->handle = dba_open( $this->tmpFileName, 'n', 'cdb_make' ); |
42 | if ( !$this->handle ) { |
43 | throw new Exception( 'Unable to open CDB file for write "' . $fileName . '"' ); |
44 | } |
45 | } |
46 | |
47 | public function set( $key, $value ): void { |
48 | dba_insert( $key, $value, $this->handle ); |
49 | } |
50 | |
51 | public function close(): void { |
52 | if ( $this->handle ) { |
53 | dba_close( $this->handle ); |
54 | if ( $this->isWindows() ) { |
55 | unlink( $this->realFileName ); |
56 | } |
57 | if ( !rename( $this->tmpFileName, $this->realFileName ) ) { |
58 | throw new Exception( 'Unable to move the new CDB file into place.' ); |
59 | } |
60 | } |
61 | $this->handle = null; |
62 | } |
63 | } |