1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace tkanstantsin\fileupload\formatter; |
5
|
|
|
|
6
|
|
|
use League\Flysystem\FilesystemInterface; |
7
|
|
|
use tkanstantsin\fileupload\config\InvalidConfigException; |
8
|
|
|
use tkanstantsin\fileupload\formatter\adapter\IFormatAdapter; |
9
|
|
|
use tkanstantsin\fileupload\model\BaseObject; |
10
|
|
|
use tkanstantsin\fileupload\model\Container; |
11
|
|
|
use tkanstantsin\fileupload\model\ICacheStateful; |
12
|
|
|
use tkanstantsin\fileupload\model\IFile; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class FileProcessor |
16
|
|
|
* TODO: create callbacks or interfaces for getting customized file name or |
17
|
|
|
* filepath |
18
|
|
|
*/ |
19
|
|
|
class File extends BaseObject |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Additional dynamic config for processor class. |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public $config = []; |
26
|
|
|
/** |
27
|
|
|
* @var IFormatAdapter[]|array |
28
|
|
|
*/ |
29
|
|
|
public $formatAdapterArray = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @see Factory::DEFAULT_FORMATTER_ARRAY |
33
|
|
|
* @example file, _normal, _product_preview |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
public $name; |
37
|
|
|
/** |
38
|
|
|
* Path to original file in contentFS |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $path; |
42
|
|
|
/** |
43
|
|
|
* ``` |
44
|
|
|
* <?php function (IFile $file, bool $cached) {} ?> |
45
|
|
|
* ``` |
46
|
|
|
* @var callable|null |
47
|
|
|
*/ |
48
|
|
|
public $afterCacheCallback; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var IFile |
52
|
|
|
*/ |
53
|
|
|
protected $file; |
54
|
|
|
/** |
55
|
|
|
* @var FilesystemInterface |
56
|
|
|
*/ |
57
|
|
|
protected $filesystem; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* FileProcessor constructor. |
61
|
|
|
* @param IFile $file |
62
|
|
|
* @param FilesystemInterface $filesystem |
63
|
|
|
* @param array $config |
64
|
|
|
* @throws \tkanstantsin\fileupload\config\InvalidConfigException |
65
|
|
|
* @throws \ReflectionException |
66
|
|
|
*/ |
67
|
1 |
|
public function __construct(IFile $file, FilesystemInterface $filesystem, array $config = []) |
68
|
|
|
{ |
69
|
1 |
|
$this->file = $file; |
70
|
1 |
|
$this->filesystem = $filesystem; |
71
|
1 |
|
parent::__construct($config); |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Initialize app |
76
|
|
|
* @throws InvalidConfigException |
77
|
|
|
* @throws \ReflectionException |
78
|
|
|
*/ |
79
|
1 |
|
public function init(): void |
80
|
|
|
{ |
81
|
1 |
|
parent::init(); |
82
|
|
|
|
83
|
1 |
|
if ($this->name === null || !\is_string($this->name) || $this->name === '') { |
84
|
|
|
throw new InvalidConfigException('Formatter name must be defined'); |
85
|
|
|
} |
86
|
1 |
|
if ($this->path === null) { |
87
|
|
|
throw new InvalidConfigException('File path property must be defined and be not empty'); |
88
|
|
|
} |
89
|
1 |
|
foreach ($this->formatAdapterArray as $i => $formatAdapter) { |
90
|
|
|
$this->formatAdapterArray[$i] = Container::createObject($formatAdapter); |
91
|
|
|
|
92
|
|
|
if (!($this->formatAdapterArray[$i] instanceof IFormatAdapter)) { |
93
|
|
|
throw new InvalidConfigException(sprintf('Format adapter must be instance of %s.', IFormatAdapter::class)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getName(): string |
102
|
|
|
{ |
103
|
|
|
return $this->name; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return resource|string|bool |
108
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
109
|
|
|
*/ |
110
|
|
|
public function getContent() |
111
|
|
|
{ |
112
|
|
|
if (!$this->filesystem->has($this->path)) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$content = $this->getContentInternal(); |
117
|
|
|
foreach ($this->formatAdapterArray as $formatAdapter) { |
118
|
|
|
$content = $formatAdapter->exec($this->file, $content); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $content; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Call user function after saving cached file |
126
|
|
|
* @param bool $cached |
127
|
|
|
*/ |
128
|
1 |
|
public function afterCacheCallback(bool $cached): void |
129
|
|
|
{ |
130
|
1 |
|
if (!($this->file instanceof ICacheStateful)) { |
131
|
1 |
|
return; |
132
|
|
|
} |
133
|
|
|
if (!\is_callable($this->afterCacheCallback)) { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
\call_user_func($this->afterCacheCallback, $this->file, $cached); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return resource|bool |
142
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
143
|
|
|
*/ |
144
|
|
|
protected function getContentInternal() |
145
|
|
|
{ |
146
|
|
|
return $this->filesystem->readStream($this->path); |
147
|
|
|
} |
148
|
|
|
} |