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 |
17
|
|
|
* or file path |
18
|
|
|
*/ |
19
|
|
|
class File extends BaseObject |
20
|
|
|
{ |
21
|
|
|
public const EVENT_CACHED = 'cached'; |
22
|
|
|
public const EVENT_EMPTY = 'empty'; |
23
|
|
|
public const EVENT_ERROR = 'error'; |
24
|
|
|
public const EVENT_NOT_FOUND = 'not_found'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Additional dynamic config for processor class. |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public $config = []; |
31
|
|
|
/** |
32
|
|
|
* @var IFormatAdapter[]|array |
33
|
|
|
*/ |
34
|
|
|
public $formatAdapterArray = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @see Factory::DEFAULT_FORMATTER_ARRAY |
38
|
|
|
* @example file, _normal, _product_preview |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $name; |
42
|
|
|
/** |
43
|
|
|
* Path to original file in contentFS |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
public $path; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var IFile |
50
|
|
|
*/ |
51
|
|
|
protected $file; |
52
|
|
|
/** |
53
|
|
|
* @var FilesystemInterface |
54
|
|
|
*/ |
55
|
|
|
protected $filesystem; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* FileProcessor constructor. |
59
|
|
|
* @param IFile $file |
60
|
|
|
* @param FilesystemInterface $filesystem |
61
|
|
|
* @param array $config |
62
|
|
|
* @throws \tkanstantsin\fileupload\config\InvalidConfigException |
63
|
|
|
* @throws \ReflectionException |
64
|
|
|
*/ |
65
|
1 |
|
public function __construct(IFile $file, FilesystemInterface $filesystem, array $config = []) |
66
|
|
|
{ |
67
|
1 |
|
$this->file = $file; |
68
|
1 |
|
$this->filesystem = $filesystem; |
69
|
1 |
|
parent::__construct($config); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Initialize app |
74
|
|
|
* @throws InvalidConfigException |
75
|
|
|
* @throws \ReflectionException |
76
|
|
|
*/ |
77
|
1 |
|
public function init(): void |
78
|
|
|
{ |
79
|
1 |
|
parent::init(); |
80
|
|
|
|
81
|
1 |
|
if ($this->name === null || !\is_string($this->name) || $this->name === '') { |
82
|
|
|
throw new InvalidConfigException('Formatter name must be defined'); |
83
|
|
|
} |
84
|
1 |
|
if ($this->path === null) { |
85
|
|
|
throw new InvalidConfigException('File path property must be defined and be not empty'); |
86
|
|
|
} |
87
|
1 |
|
foreach ($this->formatAdapterArray as $i => $formatAdapter) { |
88
|
|
|
$this->formatAdapterArray[$i] = Container::createObject($formatAdapter); |
89
|
|
|
|
90
|
|
|
if (!($this->formatAdapterArray[$i] instanceof IFormatAdapter)) { |
91
|
|
|
throw new InvalidConfigException(sprintf('Format adapter must be instance of %s.', IFormatAdapter::class)); |
92
|
|
|
} |
93
|
|
|
} |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function getName(): string |
100
|
|
|
{ |
101
|
|
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return resource|string|bool |
106
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
107
|
|
|
*/ |
108
|
|
|
public function getContent() |
109
|
|
|
{ |
110
|
|
|
if (!$this->filesystem->has($this->path)) { |
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$content = $this->getContentInternal(); |
115
|
|
|
foreach ($this->formatAdapterArray as $formatAdapter) { |
116
|
|
|
$content = $formatAdapter->exec($this->file, $content); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $content; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Call user function after saving cached file |
124
|
|
|
* @param string $event |
125
|
|
|
*/ |
126
|
1 |
|
public function triggerEvent(string $event): void |
127
|
|
|
{ |
128
|
|
|
// TODO: use event library and allow attach events to IFile and ICacheStateful. |
129
|
|
|
// cache triggers |
130
|
1 |
|
if ($this->file instanceof ICacheStateful) { |
131
|
|
|
switch ($event) { |
132
|
|
|
case self::EVENT_CACHED: |
133
|
|
|
$this->file->setCachedAt($this->name, time()); |
134
|
|
|
break; |
135
|
|
|
case self::EVENT_EMPTY: |
136
|
|
|
case self::EVENT_ERROR: |
137
|
|
|
case self::EVENT_NOT_FOUND: |
138
|
|
|
// TODO: split those and save different cases of cache state. |
139
|
|
|
$this->file->setCachedAt($this->name, null); |
140
|
|
|
break; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$this->file->saveState(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// other triggers |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return resource|bool |
151
|
|
|
* @throws \League\Flysystem\FileNotFoundException |
152
|
|
|
*/ |
153
|
|
|
protected function getContentInternal() |
154
|
|
|
{ |
155
|
|
|
return $this->filesystem->readStream($this->path); |
156
|
|
|
} |
157
|
|
|
} |