1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Shared |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Reader; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Message\Message; |
18
|
|
|
use Phossa2\Shared\Base\StaticAbstract; |
19
|
|
|
use Phossa2\Shared\Exception\RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Reader |
23
|
|
|
* |
24
|
|
|
* Read from file base on the suffix |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Shared |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @version 2.0.16 |
29
|
|
|
* @since 2.0.2 added |
30
|
|
|
* @since 2.0.16 updated |
31
|
|
|
*/ |
32
|
|
|
class Reader extends StaticAbstract |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* supported types |
36
|
|
|
* |
37
|
|
|
* @var string[] |
38
|
|
|
* @access protected |
39
|
|
|
* @staticvar |
40
|
|
|
*/ |
41
|
|
|
protected static $supported = ['ini', 'json', 'php', 'xml', 'serialized']; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Read, parse & return contents from the $path |
45
|
|
|
* |
46
|
|
|
* @param string $path |
47
|
|
|
* @param string $type force this type |
48
|
|
|
* @return mixed |
49
|
|
|
* @throws NotFoundException if $path not found |
50
|
|
|
* @throws RuntimeException if something goes wrong |
51
|
|
|
* @access public |
52
|
|
|
* @since 2.0.16 added $type param |
53
|
|
|
* @static |
54
|
|
|
*/ |
55
|
|
|
public static function readFile( |
56
|
|
|
/*# string */ $path, |
57
|
|
|
/*# string */ $type = '' |
58
|
|
|
) { |
59
|
|
|
$suffix = $type ?: substr($path, strpos($path, '.') + 1); |
60
|
|
|
|
61
|
|
|
if (!static::isSupported($suffix)) { |
62
|
|
|
throw new RuntimeException( |
63
|
|
|
Message::get(Message::MSG_PATH_TYPE_UNKNOWN, $suffix), |
64
|
|
|
Message::MSG_PATH_TYPE_UNKNOWN |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* @var ReaderInterface $class */ |
69
|
|
|
$class = static::getNameSpace() . '\\' . ucfirst($suffix) . 'Reader'; |
70
|
|
|
|
71
|
|
|
return $class::readFile($path); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Is this type supported |
76
|
|
|
* |
77
|
|
|
* @param string $type |
78
|
|
|
* @return bool |
79
|
|
|
* @access public |
80
|
|
|
* @static |
81
|
|
|
*/ |
82
|
|
|
public static function isSupported(/*# string */ $type)/*# : bool */ |
83
|
|
|
{ |
84
|
|
|
return in_array($type, static::$supported); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|