Skip to content

Commit 67ec072

Browse files
authored
Merge pull request #30 from antonsacred/HW-1005-set-depth-in-configuration
Set directories depth in configuration
2 parents a3fceaf + 03e3047 commit 67ec072

File tree

11 files changed

+104
-49
lines changed

11 files changed

+104
-49
lines changed

composer.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
"autoload": {
55
"psr-0": {
66
"Barberry": "library/"
7-
},
8-
"files": [
9-
"library/Barberry/functions.php"
10-
]
7+
}
118
},
129
"require": {
1310
"php": ">=7.4",

library/Barberry/Cache.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
class Cache {
88

99
private Filesystem $filesystem;
10+
private Destination $destination;
1011

11-
public function __construct(Filesystem $filesystem) {
12+
public function __construct(Filesystem $filesystem, Destination $destination) {
1213
$this->filesystem = $filesystem;
14+
$this->destination = $destination;
1315
}
1416

1517
/**
@@ -27,7 +29,7 @@ public function save($streamOrContent, Request $request): string
2729

2830
public function invalidate($id): void
2931
{
30-
$path = nonlinear\generateDestination($id);
32+
$path = $this->destination->generate($id);
3133
if ($this->directoryExists($path)) {
3234
$this->filesystem->deleteDirectory($path);
3335
}
@@ -65,7 +67,7 @@ private function filePath(Request $request): string
6567
return $file;
6668
}
6769

68-
return nonlinear\generateDestination($request->id) . $file;
70+
return $this->destination->generate($request->id) . $file;
6971
}
7072

7173
private static function directoryByRequest(Request $request): string

library/Barberry/Config.php

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Config
1313
private string $applicationPath;
1414
public FilesystemAdapter $storageAdapter;
1515
public FilesystemAdapter $cacheAdapter;
16+
private int $depth = 3;
1617

1718
/**
1819
* When you pass null, by default will be used ../public/cache/ and ../usr/storage/ locations
@@ -46,4 +47,14 @@ private function setDefaultValues(): void
4647
$this->directoryCache = $this->applicationPath . '/public/cache/';
4748
$this->directoryStorage = $this->applicationPath . '/usr/storage/';
4849
}
50+
51+
public function setDepth(int $depth): void
52+
{
53+
$this->depth = $depth;
54+
}
55+
56+
public function getDepth(): int
57+
{
58+
return $this->depth;
59+
}
4960
}

library/Barberry/Destination.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Barberry;
4+
5+
class Destination
6+
{
7+
private int $depth;
8+
private int $len;
9+
10+
public function __construct(int $depth = 3, int $len = 2) {
11+
$this->depth = $depth;
12+
$this->len = $len;
13+
}
14+
15+
public function generate(string $hash): string
16+
{
17+
$start = 0;
18+
$d = $this->depth;
19+
$dir = [];
20+
21+
while ($d-- > 0) {
22+
$dir[] = substr($hash, $start, $this->len);
23+
$start += $this->len;
24+
}
25+
return self::als(implode(DIRECTORY_SEPARATOR, $dir));
26+
}
27+
28+
private static function als($path): string
29+
{
30+
return rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
31+
}
32+
}

library/Barberry/Resources.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Resources
1818
* @var Filter\FilterInterface
1919
*/
2020
private $filter;
21+
private Destination $destination;
2122

2223
/**
2324
* @param Config $config
@@ -27,6 +28,7 @@ public function __construct(Config $config, Filter\FilterInterface $filter = nul
2728
{
2829
$this->config = $config;
2930
$this->filter = $filter;
31+
$this->destination = new Destination($config->getDepth());
3032
}
3133

3234
/**
@@ -38,7 +40,7 @@ public function cache()
3840
return $this->getResource(
3941
__FUNCTION__,
4042
function () use ($config) {
41-
return new Cache(new Filesystem($config->cacheAdapter));
43+
return new Cache(new Filesystem($config->cacheAdapter), $this->destination);
4244
}
4345
);
4446
}
@@ -52,7 +54,7 @@ public function storage()
5254
return $this->getResource(
5355
__FUNCTION__,
5456
function () use ($config) {
55-
return new Storage\File(new Filesystem($config->storageAdapter));
57+
return new Storage\File(new Filesystem($config->storageAdapter), $this->destination);
5658
}
5759
);
5860
}

library/Barberry/Storage/File.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
namespace Barberry\Storage;
33

44
use Barberry\ContentType;
5-
use Barberry\fs;
6-
use Barberry\nonlinear;
5+
use Barberry\Destination;
76
use GuzzleHttp\Psr7\UploadedFile;
87
use GuzzleHttp\Psr7\Utils;
98
use League\Flysystem\Filesystem;
@@ -12,10 +11,12 @@
1211
class File implements StorageInterface
1312
{
1413
private Filesystem $filesystem;
14+
private Destination $destination;
1515

16-
public function __construct(Filesystem $filesystem)
16+
public function __construct(Filesystem $filesystem, Destination $destination)
1717
{
1818
$this->filesystem = $filesystem;
19+
$this->destination = $destination;
1920
}
2021

2122
/**
@@ -93,7 +94,7 @@ private function filePathById($id)
9394
return $id;
9495
}
9596

96-
return nonlinear\generateDestination($id) . $id;
97+
return $this->destination->generate($id) . $id;
9798
}
9899

99100
private function generateUniqueId(): string

library/Barberry/functions.php

-29
This file was deleted.

test/integration/CacheTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ public function testInvalidateRemovesCachedContent(): void
6060

6161
private function cache(): Cache
6262
{
63-
return new Cache(new Filesystem(new LocalFilesystemAdapter($this->cache_path)));
63+
return new Cache(new Filesystem(new LocalFilesystemAdapter($this->cache_path)), new Destination());
6464
}
6565
}

test/integration/Storage/FileTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace Barberry\Storage;
44

5-
use Barberry\nonlinear;
5+
use Barberry\Destination;
66
use Barberry\Test;
7-
use Barberry\fs;
87
use GuzzleHttp\Psr7\UploadedFile;
98
use GuzzleHttp\Psr7\Utils;
109
use League\Flysystem\Filesystem;
@@ -46,7 +45,9 @@ public function testIsFileSavedInNonLinearStructure(): void
4645
new UploadedFile(Utils::tryFopen(__DIR__ . '/../data/1x1.gif', 'r'), 43, UPLOAD_ERR_OK)
4746
);
4847

49-
$path = $this->storage_path . nonlinear\generateDestination($id);
48+
$destination = new Destination();
49+
50+
$path = $this->storage_path . $destination->generate($id);
5051
self::assertCount(5, array_filter(explode(DIRECTORY_SEPARATOR, $path), function($item) { return !empty($item); }));
5152

5253
$content = file_get_contents($path . $id);
@@ -104,7 +105,7 @@ public function testCanDefineContentType(): void{
104105

105106
private function storage(): File
106107
{
107-
return new File(new Filesystem(new LocalFilesystemAdapter($this->storage_path)));
108+
return new File(new Filesystem(new LocalFilesystemAdapter($this->storage_path)), new Destination());
108109
}
109110

110111
private static function rmDirRecursive(string $dir): void {

test/unit/CacheTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public function setUp(): void
2828
*/
2929
public function testConvertsUriToFilePath(string $uri, string $expectedPath): void
3030
{
31-
$cache = new Cache(self::$filesystem);
31+
$cache = new Cache(self::$filesystem, new Destination());
3232
$cache->save('123', new Request($uri));
3333

3434
self::assertTrue(self::$filesystem->fileExists($expectedPath));
3535
}
3636

3737
public function testStreamDataCanBeSaved(): void
3838
{
39-
$cache = new Cache(self::$filesystem);
39+
$cache = new Cache(self::$filesystem, new Destination());
4040
$cache->save(Utils::streamFor('Cached content'), new Request('/a1b2c3d4.gif'));
4141

4242
self::assertEquals('Cached content', self::$filesystem->read('a1/b2/c3/a1b2c3d4/a1b2c3d4.gif'));

test/unit/DestinationTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace unit;
4+
5+
use Barberry\Destination;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class DestinationTest extends TestCase
9+
{
10+
11+
public static function dataProvider(): array
12+
{
13+
return [
14+
['68d619ea311bae98c6bc', 3,2, '68/d6/19/'],
15+
['e66da9914232c58ec3c1', 3,2, 'e6/6d/a9/'],
16+
['53a1b5e4a4a45d15a2b9', 3,2, '53/a1/b5/'],
17+
['d8e4291be2548fd12247', 3,2, 'd8/e4/29/'],
18+
['3eb2a130c3e916bf571b', 3,2, '3e/b2/a1/'],
19+
['b5756715437509360e7e', 2,2, 'b5/75/'],
20+
['709866548947a3d34e9d', 2,2, '70/98/'],
21+
['38a1fba7d9a6e4f38022', 2,2, '38/a1/'],
22+
['f6545864cf6df30466a4', 2,2, 'f6/54/'],
23+
['64f93d347decf21ea255', 2,2, '64/f9/'],
24+
['ff95231f8b4d861ca3ab', 2,2, 'ff/95/'],
25+
['ff95231f8b4d861ca3ab', 2, 5, 'ff952/31f8b/'],
26+
];
27+
}
28+
29+
/**
30+
* @dataProvider dataProvider
31+
*/
32+
public function testGenerate(string $hash, int $depth,int $length, string $result): void
33+
{
34+
$destination = new Destination($depth, $length);
35+
self::assertEquals($result, $destination->generate($hash));
36+
}
37+
38+
}

0 commit comments

Comments
 (0)