-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_adapter_switching.php
More file actions
89 lines (68 loc) · 2.59 KB
/
Copy path07_adapter_switching.php
File metadata and controls
89 lines (68 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Adapter switching — same business logic, different storage.
*
* Because everything goes through DataStoreInterface,
* switching from SQLite to directory to ZIP changes exactly one
* line of setup code. Business logic is completely decoupled.
*/
declare(strict_types=1);
require __DIR__.'/../vendor/autoload.php';
use KDuma\SimpleDAL\Adapter\Database\DatabaseAdapter;
use KDuma\SimpleDAL\Adapter\Flysystem\FlysystemAdapter;
use KDuma\SimpleDAL\Contracts\DataStoreInterface;
use KDuma\SimpleDAL\DataStore;
use KDuma\SimpleDAL\Entity\CollectionEntityDefinition;
use KDuma\SimpleDAL\Query\Filter;
use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
// ── Business logic that doesn't care about storage ──
function seedAndQuery(DataStoreInterface $store): void
{
$certs = $store->collection('certificates');
$certs->create(['domain' => 'example.com', 'status' => 'active'], id: 'c1');
$certs->create(['domain' => 'test.com', 'status' => 'active'], id: 'c2');
$certs->create(['domain' => 'old.com', 'status' => 'expired'], id: 'c3');
$active = $certs->filter(Filter::where('status', '=', 'active'));
echo ' Active: '.count($active)." certificates\n";
foreach ($active as $r) {
echo " - {$r->id}: {$r->get('domain')}\n";
}
}
$entities = [new CollectionEntityDefinition('certificates')];
// ── SQLite adapter ──
echo "=== SQLite ===\n";
seedAndQuery(new DataStore(
adapter: new DatabaseAdapter(new PDO('sqlite::memory:')),
entities: $entities,
));
// ── Flysystem adapter (local directory) ──
echo "\n=== Flysystem (local) ===\n";
$dir = __DIR__.'/adapter-demo';
@mkdir($dir, 0777, true);
seedAndQuery(new DataStore(
adapter: new FlysystemAdapter(new Filesystem(new LocalFilesystemAdapter($dir))),
entities: $entities,
));
// Cleanup directory
$it = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST,
);
foreach ($it as $f) {
$f->isDir() ? rmdir($f->getPathname()) : unlink($f->getPathname());
}
rmdir($dir);
// ── Flysystem adapter (ZIP) ──
echo "\n=== Flysystem (ZIP) ===\n";
$zipPath = __DIR__.'/adapter-demo.zip';
seedAndQuery(new DataStore(
adapter: new FlysystemAdapter(
new Filesystem(new ZipArchiveAdapter(new FilesystemZipArchiveProvider($zipPath))),
),
entities: $entities,
));
unlink($zipPath);
echo "\nSame code, three backends. Done.\n";