|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of YaEtl |
|
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl |
|
6
|
|
|
* This source file is licensed under the MIT license which you will |
|
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace fab2s\YaEtl\Etl\Streams\Traits; |
|
11
|
|
|
|
|
12
|
|
|
use fab2s\YaEtl\YaEtlException; |
|
13
|
|
|
|
|
14
|
|
|
trait StreamRead |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $sourcePath = ''; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $sourceFileName; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var resource|null |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $sourceStream = null; |
|
30
|
|
|
|
|
31
|
|
|
public function setSourcePath(string $sourcePath): self |
|
32
|
|
|
{ |
|
33
|
|
|
$this->sourcePath = $this->slashPath($sourcePath); |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getSourcePath(): string |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->sourcePath; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setSourceStream($resource): self |
|
44
|
|
|
{ |
|
45
|
|
|
if (!is_resource($resource)) { |
|
46
|
|
|
throw new YaEtlException('[' . class_basename(static::class) . '] Invalid handle'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->sourceStream = $resource; |
|
50
|
|
|
|
|
51
|
|
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return null|false|resource |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getSourceStream(bool $fresh = false) |
|
58
|
|
|
{ |
|
59
|
|
|
if (!$fresh && !empty($this->sourceStream)) { |
|
60
|
|
|
return $this->sourceStream; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $this->sourceStream = fopen($this->getSourceFilePath(), 'rb'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getSourceFileName(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->sourceFileName; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setSourceFileName($sourceFileName): self |
|
72
|
|
|
{ |
|
73
|
|
|
$this->sourceFileName = basename($sourceFileName); |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getSourceFilePath(): string |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->getSourcePath() . $this->getSourceFileName(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
abstract public function slashPath(string $path) : string; |
|
84
|
|
|
} |
|
85
|
|
|
|