Skip to content

Commit 03e3047

Browse files
committed
Set directories depth in configuration
1 parent 1ad1260 commit 03e3047

File tree

11 files changed

+104
-49
lines changed

11 files changed

+104
-49
lines changed

composer.json

Lines changed: 1 addition & 4 deletions
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

Lines changed: 5 additions & 3 deletions
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

Lines changed: 11 additions & 0 deletions
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

Lines changed: 32 additions & 0 deletions
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/integration/CacheTest.php

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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'));

0 commit comments

Comments
 (0)