1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Bootstrappers\Filesystem; |
6
|
|
|
|
7
|
|
|
use AbterPhp\Framework\Constant\Env; |
8
|
|
|
use AbterPhp\Framework\Environments\Environment; |
9
|
|
|
use AbterPhp\Framework\Filesystem\FileFinder; |
10
|
|
|
use AbterPhp\Framework\Filesystem\IFileFinder; |
11
|
|
|
use League\Flysystem\Filesystem; |
12
|
|
|
use League\Flysystem\Local\LocalFilesystemAdapter; |
13
|
|
|
use Opulence\Ioc\Bootstrappers\Bootstrapper; |
14
|
|
|
use Opulence\Ioc\Bootstrappers\ILazyBootstrapper; |
15
|
|
|
use Opulence\Ioc\IContainer; |
16
|
|
|
|
17
|
|
|
class FileFinderBootstrapper extends Bootstrapper implements ILazyBootstrapper |
18
|
|
|
{ |
19
|
|
|
protected ?array $assetPaths = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function getAssetPaths(): array |
25
|
|
|
{ |
26
|
|
|
global $abterModuleManager; |
27
|
|
|
|
28
|
|
|
if ($this->assetPaths !== null) { |
29
|
|
|
return $this->assetPaths; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$paths = $abterModuleManager->getAssetsPaths() ?: []; |
33
|
|
|
|
34
|
|
|
$this->assetPaths = $paths; |
35
|
|
|
|
36
|
|
|
return $paths; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string[] $assetPaths |
41
|
|
|
* |
42
|
|
|
* @return $this |
43
|
|
|
*/ |
44
|
|
|
public function setAssetPaths(array $assetPaths): self |
45
|
|
|
{ |
46
|
|
|
$this->assetPaths = $assetPaths; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function getBindings(): array |
55
|
|
|
{ |
56
|
|
|
return [ |
57
|
|
|
FileFinder::class, |
58
|
|
|
IFileFinder::class, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param IContainer $container |
64
|
|
|
*/ |
65
|
|
|
public function registerBindings(IContainer $container): void |
66
|
|
|
{ |
67
|
|
|
$fileFinder = new FileFinder(); |
68
|
|
|
|
69
|
|
|
$this->registerResourcePaths($fileFinder); |
70
|
|
|
|
71
|
|
|
$container->bindInstance(FileFinder::class, $fileFinder); |
72
|
|
|
$container->bindInstance(IFileFinder::class, $fileFinder); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param FileFinder $fileFinder |
77
|
|
|
*/ |
78
|
|
|
private function registerResourcePaths(FileFinder $fileFinder): void |
79
|
|
|
{ |
80
|
|
|
foreach ($this->getAssetPaths() as $key => $paths) { |
81
|
|
|
foreach ($paths as $path) { |
82
|
|
|
if (!$path) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
$fileFinder->registerFilesystem(new Filesystem(new LocalFilesystemAdapter($path)), $key); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$dirPublic = rtrim(Environment::mustGetVar(Env::DIR_PUBLIC), DIRECTORY_SEPARATOR); |
90
|
|
|
|
91
|
|
|
$fileFinder->registerFilesystem(new Filesystem(new LocalFilesystemAdapter($dirPublic))); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|