1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the m1\vars library |
5
|
|
|
* |
6
|
|
|
* (c) m1 <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @package m1/vars |
12
|
|
|
* @version 0.2.0 |
13
|
|
|
* @author Miles Croxford <[email protected]> |
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace M1\Vars\Traits; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* File trait gives common operation functions needed for files in Vars |
23
|
|
|
* |
24
|
|
|
* @since 0.1.0 |
25
|
|
|
*/ |
26
|
|
|
trait FileTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The file used in the trait |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $file; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The parent Vars instance |
37
|
|
|
* |
38
|
|
|
* @var \M1\Vars\Vars |
39
|
|
|
*/ |
40
|
|
|
private $vars; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Validates the file passed to see if it is a file and is readable |
44
|
|
|
* |
45
|
|
|
* @throws \RuntimeException If the file passed is not a file |
46
|
|
|
* @throws \RuntimeException If the file passed is not readable |
47
|
|
|
*/ |
48
|
61 |
|
private function validate() |
49
|
|
|
{ |
50
|
61 |
|
$file = $this->file; |
51
|
|
|
|
52
|
61 |
|
if (!is_file($file)) { |
53
|
1 |
|
throw new \RuntimeException(sprintf("'%s' is not a file", $file)); |
54
|
|
|
} |
55
|
|
|
|
56
|
60 |
|
if (!is_readable($file)) { |
57
|
|
|
// @codeCoverageIgnoreStart |
58
|
|
|
throw new \RuntimeException(sprintf("'%s' is not a readable file", $file)); |
59
|
|
|
// @codeCoverageIgnoreEnd |
60
|
|
|
} |
61
|
60 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Gets the supported loader for the passed file |
65
|
|
|
* |
66
|
|
|
* @see \M1\Vars\Vars::getLoaders() \M1\Vars\Vars::getLoaders() |
67
|
|
|
* |
68
|
|
|
* @param string $data The passed file |
69
|
|
|
* |
70
|
|
|
* @return mixed Returns the loader class or false if no loader calls was found |
71
|
|
|
*/ |
72
|
60 |
|
private function getSupportedLoader($data) |
73
|
|
|
{ |
74
|
60 |
|
$loaders = $this->vars->loader->getLoaders(); |
75
|
|
|
|
76
|
60 |
|
foreach ($loaders as $loader) { |
77
|
60 |
|
$class_loader = new \ReflectionClass($loader); |
78
|
60 |
|
$class_loader = $class_loader->newInstanceArgs(array($data)); |
79
|
|
|
|
80
|
60 |
|
if ($class_loader->supports()) { |
81
|
59 |
|
return $class_loader; |
82
|
|
|
} |
83
|
56 |
|
} |
84
|
1 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Loads raw content from the file |
89
|
|
|
* |
90
|
|
|
* @param string $data The passed file |
91
|
|
|
* |
92
|
|
|
* @throws \InvalidArugmentException If the file passed is not supported by the current loaders |
93
|
|
|
* |
94
|
|
|
* @return mixed The content from the file via the loader |
95
|
|
|
*/ |
96
|
60 |
|
private function loadContent($data) |
97
|
|
|
{ |
98
|
60 |
|
$loader = $this->getSupportedLoader($data); |
99
|
|
|
|
100
|
60 |
|
if (!$loader) { |
101
|
1 |
|
throw new \InvalidArgumentException(sprintf("'%s' is not supported by the current loaders", $this->file)); |
102
|
|
|
} |
103
|
|
|
|
104
|
59 |
|
$loader->load(); |
105
|
53 |
|
return $loader->getContent(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the passed file |
110
|
|
|
* |
111
|
|
|
* @return string The passed file |
112
|
|
|
*/ |
113
|
10 |
|
public function getFile() |
114
|
|
|
{ |
115
|
10 |
|
return $this->file; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|