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
|
|
|
* @param bool $namespace |
42
|
|
|
* @param bool $strict |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function loadFiles(array $files, $append = false, $namespace = true, $strict = true) |
46
|
|
|
{ |
47
|
|
|
$this->initializeFileLoader(); |
48
|
|
|
$this->fileLoader->addFiles($files); |
49
|
|
|
$data = $this->fileLoader->process($namespace, $strict); |
50
|
|
|
$this->hydrate($data, $append); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function loadFile($file, $append = false, $namespace = true) |
54
|
|
|
{ |
55
|
|
|
$this->loadFiles([$file], $append, $namespace); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Allows for the addition of a custom decoder to the manager load files system. |
60
|
|
|
* |
61
|
|
|
* @param DecoderInterface $decoder |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function addDecoder(DecoderInterface $decoder) |
65
|
|
|
{ |
66
|
|
|
$this->initializeFileLoader(); |
67
|
|
|
$this->fileLoader->addDecoder($decoder); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return FileLoader |
72
|
|
|
*/ |
73
|
|
|
public function getFileLoader() |
74
|
|
|
{ |
75
|
|
|
return $this->fileLoader; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param FileLoader $fileLoader |
80
|
|
|
*/ |
81
|
|
|
public function setFileLoader($fileLoader) |
82
|
|
|
{ |
83
|
|
|
$this->fileLoader = $fileLoader; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|