1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @category Library
|
4
|
|
|
* @license MIT http://opensource.org/licenses/MIT
|
5
|
|
|
* @link https://github.com/emlynwest/changelog
|
6
|
|
|
*/
|
7
|
|
|
|
8
|
|
|
namespace ChangeLog\IO;
|
9
|
|
|
|
10
|
|
|
use ChangeLog\AbstractIO;
|
11
|
|
|
use InvalidArgumentException;
|
12
|
|
|
use League\Flysystem\Filesystem;
|
13
|
|
|
|
14
|
|
|
/**
|
15
|
|
|
* Allows change logs to be loaded via a Flysystem adaptor.
|
16
|
|
|
*
|
17
|
|
|
* Config can contain the keys 'file', 'filesystem' and 'line_separator'. 'file' and 'filesystem' are not optional.
|
18
|
|
|
*/
|
19
|
|
|
class Flysystem extends AbstractIO
|
20
|
|
|
{
|
21
|
|
|
|
22
|
|
|
protected $configDefaults = [
|
23
|
|
|
'line_separator' => "\n",
|
24
|
|
|
];
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* {@inheritdoc}
|
28
|
|
|
*
|
29
|
|
|
* @throws InvalidArgumentException
|
30
|
|
|
*/
|
31
|
3 |
|
public function getContent()
|
32
|
|
|
{
|
33
|
3 |
|
$file = $this->getFileLocation();
|
34
|
2 |
|
$filesystem = $this->getFilesystem();
|
35
|
|
|
|
36
|
1 |
|
$content = $filesystem->read($file);
|
37
|
|
|
|
38
|
1 |
|
return explode(
|
39
|
1 |
|
$this->getConfig('line_separator'),
|
|
|
|
|
40
|
1 |
|
$content
|
41
|
1 |
|
);
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* {@inheritdoc}
|
46
|
|
|
*/
|
47
|
1 |
|
public function setContent($content)
|
48
|
|
|
{
|
49
|
1 |
|
$file = $this->getFileLocation();
|
50
|
1 |
|
$filesystem = $this->getFilesystem();
|
51
|
|
|
|
52
|
1 |
|
$filesystem->put($file, $content);
|
|
|
|
|
53
|
|
|
}
|
54
|
|
|
|
55
|
|
|
/**
|
56
|
|
|
* Gets the file location from the config.
|
57
|
|
|
*
|
58
|
|
|
* @return string
|
59
|
|
|
*/
|
60
|
4 |
|
protected function getFileLocation()
|
61
|
|
|
{
|
62
|
4 |
|
$file = $this->getConfig('file');
|
63
|
|
|
|
64
|
4 |
|
if ( ! $file)
|
65
|
|
|
{
|
66
|
1 |
|
throw new InvalidArgumentException('File not specified.');
|
67
|
|
|
}
|
68
|
|
|
|
69
|
3 |
|
return $file;
|
|
|
|
|
70
|
|
|
}
|
71
|
|
|
|
72
|
|
|
/**
|
73
|
|
|
* @return Filesystem
|
74
|
|
|
*/
|
75
|
3 |
|
protected function getFilesystem()
|
76
|
|
|
{
|
77
|
3 |
|
$adaptor = $this->getConfig('filesystem');
|
78
|
|
|
|
79
|
3 |
|
if ( ! $adaptor)
|
80
|
|
|
{
|
81
|
1 |
|
throw new InvalidArgumentException('Filesystem object not specified.');
|
82
|
|
|
}
|
83
|
|
|
|
84
|
2 |
|
return $adaptor;
|
|
|
|
|
85
|
|
|
}
|
86
|
|
|
|
87
|
|
|
}
|
88
|
|
|
|