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 | |||
13 | /** |
||
14 | * Allows change logs to be loaded from the file system. |
||
15 | * |
||
16 | * Config can contain the keys 'file' and 'line_separator'. 'file' is not optional. |
||
17 | */ |
||
18 | class File extends AbstractIO |
||
19 | { |
||
20 | |||
21 | protected $configDefaults = [ |
||
22 | 'line_separator' => "\n", |
||
23 | ]; |
||
24 | |||
25 | /** |
||
26 | * {@inheritdoc} |
||
27 | * |
||
28 | * @throws InvalidArgumentException |
||
29 | */ |
||
30 | 11 | public function getContent() |
|
31 | { |
||
32 | 11 | $file = $this->getFileLocation(); |
|
33 | |||
34 | 10 | $content = file_get_contents($file); |
|
35 | |||
36 | 10 | return explode( |
|
37 | 10 | $this->getConfig('line_separator'), |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
38 | 10 | $content |
|
39 | 10 | ); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 8 | public function setContent($content) |
|
46 | { |
||
47 | 8 | $file = $this->getFileLocation(); |
|
48 | 8 | file_put_contents($file, $content); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * Gets the file location from the config. |
||
53 | * |
||
54 | * @return string |
||
55 | */ |
||
56 | 12 | protected function getFileLocation() |
|
57 | { |
||
58 | 12 | $file = $this->getConfig('file'); |
|
59 | |||
60 | 12 | if ( ! $file) |
|
61 | { |
||
62 | 1 | throw new InvalidArgumentException('File not specified.'); |
|
63 | } |
||
64 | |||
65 | 11 | return $file; |
|
0 ignored issues
–
show
|
|||
66 | } |
||
67 | |||
68 | } |
||
69 |