1
|
|
|
<?php |
2
|
|
|
namespace Michaels\Manager\Traits; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Allows a class to have file loading capabilities. |
6
|
|
|
* |
7
|
|
|
* @implements Michaels\Manager\Contracts\ManagesItemsInterface |
8
|
|
|
* @package Michaels\Manager |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use Michaels\Manager\Contracts\DecoderInterface; |
12
|
|
|
use Michaels\Manager\FileLoader; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Loads data from configuration-type files into Manager |
16
|
|
|
* @package Michaels\Manager\Traits |
17
|
|
|
*/ |
18
|
|
|
trait LoadsFilesTrait |
19
|
|
|
{ |
20
|
|
|
use DependsOnManagesItemsTrait; |
21
|
|
|
|
22
|
|
|
/** @var \Michaels\Manager\FileLoader Instance of the FileLoader */ |
23
|
|
|
protected $fileLoader; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Makes sure a FileLoader object was created or creates one. |
27
|
|
|
* @param FileLoader $fileLoader |
28
|
|
|
*/ |
29
|
|
|
protected function initializeFileLoader(FileLoader $fileLoader = null) |
30
|
|
|
{ |
31
|
|
|
if (!isset($this->fileLoader)) { |
32
|
|
|
$this->setFileLoader(($fileLoader) ? $fileLoader : new FileLoader()); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This method adds the file loading functionality. |
38
|
|
|
* |
39
|
|
|
* @param array $files an array of SplFileInfo Objects |
40
|
|
|
* @param $append boolean true, if data should be appended to the manager. |
41
|
|
|
* @return array |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function loadFiles(array $files, $append = false) |
44
|
|
|
{ |
45
|
|
|
$this->initializeFileLoader(); |
46
|
|
|
$this->fileLoader->addFiles($files); |
47
|
|
|
$data = $this->fileLoader->process(); |
48
|
|
|
$this->hydrate($data, $append); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function loadFile($file, $append = false) |
52
|
|
|
{ |
53
|
|
|
return $this->loadFiles([$file], $append); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Allows for the addition of a custom decoder to the manager load files system. |
58
|
|
|
* |
59
|
|
|
* @param DecoderInterface $decoder |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
public function addDecoder(DecoderInterface $decoder) |
63
|
|
|
{ |
64
|
|
|
$this->initializeFileLoader(); |
65
|
|
|
$this->fileLoader->addDecoder($decoder); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return FileLoader |
70
|
|
|
*/ |
71
|
|
|
public function getFileLoader() |
72
|
|
|
{ |
73
|
|
|
return $this->fileLoader; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param FileLoader $fileLoader |
78
|
|
|
*/ |
79
|
|
|
public function setFileLoader($fileLoader) |
80
|
|
|
{ |
81
|
|
|
$this->fileLoader = $fileLoader; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.