|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Transfer. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
|
7
|
|
|
* in the root directory. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Transfer\Adapter; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
use Transfer\Adapter\Transaction\Iterator\CallbackIterator; |
|
14
|
|
|
use Transfer\Adapter\Transaction\Request; |
|
15
|
|
|
use Transfer\Adapter\Transaction\Response; |
|
16
|
|
|
use Transfer\Data\ValueObject; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Local directory adapter functioning as a source. |
|
20
|
|
|
*/ |
|
21
|
|
|
class LocalDirectoryAdapter implements SourceAdapterInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array Option collection |
|
25
|
|
|
*/ |
|
26
|
|
|
private $options; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string Directory to read |
|
30
|
|
|
*/ |
|
31
|
|
|
private $directory; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array Found files |
|
35
|
|
|
*/ |
|
36
|
|
|
private $fileNames; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $options Options |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function __construct(array $options = array()) |
|
42
|
|
|
{ |
|
43
|
2 |
|
$resolver = new OptionsResolver(); |
|
44
|
2 |
|
$this->configureOptions($resolver); |
|
45
|
|
|
|
|
46
|
2 |
|
$this->options = $resolver->resolve($options); |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Configures options. |
|
51
|
|
|
* |
|
52
|
|
|
* @param OptionsResolver $resolver |
|
53
|
|
|
*/ |
|
54
|
2 |
|
protected function configureOptions(OptionsResolver $resolver) |
|
55
|
|
|
{ |
|
56
|
2 |
|
$resolver->setRequired(array('directory')); |
|
57
|
2 |
|
$resolver->setAllowedTypes('directory', 'string'); |
|
58
|
2 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function receive(Request $request) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->directory = $this->options['directory']; |
|
66
|
1 |
|
$this->fileNames = array_values(array_diff(scandir($this->directory), array('..', '.'))); |
|
67
|
|
|
|
|
68
|
1 |
|
$response = new Response( |
|
69
|
1 |
|
new CallbackIterator(array($this, 'validCallback'), array($this, 'currentCallback')) |
|
70
|
1 |
|
); |
|
71
|
|
|
|
|
72
|
1 |
|
return $response; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Checks whether there are more files left. |
|
77
|
|
|
* |
|
78
|
|
|
* @param CallbackIterator $iterator |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function validCallback(CallbackIterator $iterator) |
|
83
|
|
|
{ |
|
84
|
1 |
|
return (bool) array_key_exists($iterator->key(), $this->fileNames); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns a file object. |
|
89
|
|
|
* |
|
90
|
|
|
* @param CallbackIterator $iterator |
|
91
|
|
|
* |
|
92
|
|
|
* @return ValueObject |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function currentCallback(CallbackIterator $iterator) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$clientFilename = $this->fileNames[$iterator->key()]; |
|
97
|
1 |
|
$filename = $this->directory.DIRECTORY_SEPARATOR.$clientFilename; |
|
98
|
|
|
|
|
99
|
1 |
|
$fileObject = new ValueObject(file_get_contents($filename)); |
|
100
|
1 |
|
$fileObject->setProperty('filename', $filename); |
|
101
|
1 |
|
$fileObject->setProperty('client_filename', $clientFilename); |
|
102
|
|
|
|
|
103
|
1 |
|
return $fileObject; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|