Skip to content

Commit 97821d9

Browse files
committed
add files
1 parent 88654da commit 97821d9

20 files changed

+1050
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# CHANGELOG
2+
3+
## 1.0.0 (19-07-17)
4+
5+
- first public release

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Andrei K.
3+
Copyright (c) 2018 Andrei Komarov.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# PHP Berkeley DB
2+
3+
Simple Berkeley DB wrapper for PHP.
4+
5+
## Installation
6+
7+
**Download Berkeley DB source:**
8+
9+
```bash
10+
wget 'http://download.oracle.com/berkeley-db/db-x.x.x.tar.gz'
11+
tar -xzvf db-x.x.x.tar.gz
12+
```
13+
14+
**Build Berkeley DB libraries and headers:**
15+
16+
```bash
17+
cd db-x.x.x/build_unix/
18+
../dist/configure --enable-cxx
19+
make
20+
make install
21+
```
22+
23+
**Install php_db4 extension for "phpdb4" adapter (better):**
24+
25+
Build db4 extension:
26+
27+
```bash
28+
cd ../lang/php4_db
29+
phpize
30+
./configure --with-db4=/usr/local/BerkeleyDB.x.x
31+
make
32+
make install
33+
```
34+
35+
Add db4 extension to php.ini:
36+
37+
```ini
38+
extension=db4.so
39+
```
40+
41+
**Or install db4 DBA handler for "dba" adapter:**
42+
43+
Build PHP with db4 support:
44+
45+
```bash
46+
./configure --with-db4=/usr/local/BerkeleyDB.x.x
47+
make
48+
sudo make install
49+
```
50+
51+
**Install composer package:**
52+
53+
```bash
54+
composer install andkom/php-berkeley-db
55+
```
56+
57+
## Usage
58+
59+
**Create instance:**
60+
61+
```PHP
62+
use AndKom\PhpBerkeleyDb\Adapter\AdapterFactory;
63+
64+
$adapter = AdapterFactory::create(); // use first available adapter
65+
$adapter = AdapterFactory::create("phpdb4"); // use phpdb4 adapter
66+
$adapter = AdapterFactory::create("dba"); // use dba adapter
67+
```
68+
69+
**Open database for reading:**
70+
71+
```PHP
72+
$adapter->open("filename", "r");
73+
```
74+
75+
**Open database with optional database name:**
76+
77+
```PHP
78+
$adapter->open("filename", "r", "main");
79+
```
80+
81+
**Open database for writing:**
82+
83+
```PHP
84+
$adapter->open("filename", "w");
85+
```
86+
87+
**Close database:**
88+
89+
```PHP
90+
$adapter->close();
91+
```
92+
93+
**Sync changes to database:**
94+
95+
```PHP
96+
$adapter->sync();
97+
```
98+
99+
**Read key value:**
100+
101+
```PHP
102+
$value = $adapter->get("key");
103+
```
104+
105+
**Write key value:**
106+
107+
```PHP
108+
$adapter->put("key", "value");
109+
```
110+
111+
**Delete key:**
112+
113+
```PHP
114+
$adapter->delete("key");
115+
```
116+
117+
**Returns first key and moves cursor to the next position:**
118+
119+
```PHP
120+
$adapter->firstKey();
121+
```
122+
123+
**Read next key and moves cursor to the next position:**
124+
125+
```PHP
126+
$adapter->nextKey();
127+
```
128+
129+
**Read all database keys:**
130+
131+
```PHP
132+
foreach ($adapter->read() as $key => $value) {
133+
...
134+
}
135+
```
136+
137+
### Laravel Service Provider
138+
139+
**Publish configuration to app config:**
140+
141+
```bash
142+
./artisan vendor:publish --provider=AndKom\\PhpBerkeleyDb\\ServiceProvider
143+
```
144+
145+
**Edit app/config/berkeleydb.php:**
146+
147+
```PHP
148+
<?php
149+
150+
return [
151+
'adapter' => 'phpdb4',
152+
153+
'filename' => database_path('database.dat'),
154+
155+
'database' => null,
156+
157+
'mode' => 'w',
158+
];
159+
160+
```
161+
162+
**Use BDB facade to access database methods:**
163+
164+
```PHP
165+
$value = BDB::get("key");
166+
```

composer.json

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"name": "andkom/php-berkeley-db",
3+
"description": "A simple Berkeley DB library for PHP.",
4+
"type": "library",
5+
"keywords": [
6+
"berkeley",
7+
"database",
8+
"db",
9+
"db4"
10+
],
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Andrei Komarov"
15+
}
16+
],
17+
"require": {
18+
"php": "^7.0",
19+
"ext-db4": ">=4.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": ">=5.0"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"AndKom\\PhpBerkeleyDb\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"AndKom\\PhpBerkeleyDb\\Tests\\": "tests/"
32+
}
33+
},
34+
"extra": {
35+
"laravel": {
36+
"providers": [
37+
"AndKom\\PhpBerkeleyDb\\ServiceProvider"
38+
],
39+
"aliases": {
40+
"BDB": "AndKom\\PhpBerkeleyDb\\Facade\\BDB"
41+
}
42+
}
43+
}
44+
}

config/berkeleydb.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
return [
4+
/**
5+
* Adapter to use: "phpdb4", "dba" or null.
6+
*/
7+
'adapter' => null,
8+
9+
/**
10+
* Path to database filename.
11+
*/
12+
'filename' => database_path('database.dat'),
13+
14+
/**
15+
* Database name (optional).
16+
*/
17+
'database' => null,
18+
19+
/**
20+
* Database open mode (r - read, w - write).
21+
*/
22+
'mode' => 'w',
23+
];

phpunit.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
verbose="true">
5+
<testsuites>
6+
<testsuite name="Unit">
7+
<directory suffix="Test.php">./tests/</directory>
8+
</testsuite>
9+
</testsuites>
10+
<filter>
11+
<whitelist processUncoveredFilesFromWhitelist="true">
12+
<directory suffix=".php">./src</directory>
13+
</whitelist>
14+
</filter>
15+
</phpunit>

src/Adapter/AbstractAdapter.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\PhpBerkeleyDb\Adapter;
6+
7+
use AndKom\PhpBerkeleyDb\Exception;
8+
9+
/**
10+
* Class AbstractAdapter
11+
* @package AndKom\PhpBerkeleyDb\Adapter
12+
*/
13+
abstract class AbstractAdapter implements AdapterInterface
14+
{
15+
/**
16+
* Read database keys.
17+
* @return \Generator
18+
* @throws Exception
19+
*/
20+
public function read(): \Generator
21+
{
22+
if ($key = $this->firstKey()) {
23+
yield $key => $this->get($key);
24+
}
25+
26+
while ($key = $this->nextKey()) {
27+
yield $key => $this->get($key);
28+
}
29+
}
30+
}

src/Adapter/AdapterFactory.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AndKom\PhpBerkeleyDb\Adapter;
6+
7+
/**
8+
* Class AdapterFactory
9+
* @package AndKom\PhpBerkeleyDb\Adapter
10+
*/
11+
class AdapterFactory
12+
{
13+
/**
14+
* @param string|null $adapter
15+
* @return AdapterInterface
16+
*/
17+
public static function create(string $adapter = null): AdapterInterface
18+
{
19+
if (is_null($adapter)) {
20+
$adapter = static::getDefaultAdapter();
21+
}
22+
23+
switch ($adapter) {
24+
case 'phpdb4':
25+
return new PhpDb4Adapter();
26+
27+
case 'dba':
28+
return new DbaAdapter();
29+
}
30+
31+
throw new \InvalidArgumentException("Unsupported adapter $adapter.");
32+
}
33+
34+
/**
35+
* @return string
36+
*/
37+
public static function getDefaultAdapter(): string
38+
{
39+
if (class_exists('\Db4')) {
40+
return 'phpdb4';
41+
} elseif (extension_loaded('dba') && in_array('db4', dba_handlers())) {
42+
return 'dba';
43+
} else {
44+
throw new \RuntimeException("Your PHP installation doesn't support Berkeley DB.");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)