|
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 1.1.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
|
76 |
|
private function validate() |
|
49
|
|
|
{ |
|
50
|
76 |
|
$file = $this->file; |
|
51
|
|
|
|
|
52
|
76 |
|
if (!is_file($file)) { |
|
53
|
1 |
|
throw new \RuntimeException(sprintf("'%s' is not a file", $file)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
75 |
|
if (!is_readable($file)) { |
|
57
|
|
|
// @codeCoverageIgnoreStart |
|
58
|
|
|
throw new \RuntimeException(sprintf("'%s' is not a readable file", $file)); |
|
59
|
|
|
// @codeCoverageIgnoreEnd |
|
60
|
|
|
} |
|
61
|
75 |
|
} |
|
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
|
75 |
|
private function getSupportedLoader($data) |
|
73
|
|
|
{ |
|
74
|
75 |
|
$loaders = $this->vars->loader->getLoaders(); |
|
75
|
|
|
|
|
76
|
75 |
|
foreach ($loaders as $loader) { |
|
77
|
75 |
|
$class_loader = new \ReflectionClass($loader); |
|
78
|
75 |
|
$class_loader = $class_loader->newInstanceArgs(array($data)); |
|
79
|
|
|
|
|
80
|
75 |
|
if ($class_loader->supports()) { |
|
81
|
75 |
|
return $class_loader; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
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
|
75 |
|
private function loadContent($data) |
|
97
|
|
|
{ |
|
98
|
75 |
|
$loader = $this->getSupportedLoader($data); |
|
99
|
|
|
|
|
100
|
75 |
|
if (!$loader) { |
|
101
|
1 |
|
throw new \InvalidArgumentException(sprintf("'%s' is not supported by the current loaders", $this->file)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
74 |
|
$loader->load(); |
|
105
|
67 |
|
return $loader->getContent(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns the passed file |
|
110
|
|
|
* |
|
111
|
|
|
* @return string The passed file |
|
112
|
|
|
*/ |
|
113
|
11 |
|
public function getFile() |
|
114
|
|
|
{ |
|
115
|
11 |
|
return $this->file; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|