|
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.2 |
|
29
|
|
|
* @since 2.0.2 added |
|
30
|
|
|
*/ |
|
31
|
|
|
class Reader extends StaticAbstract implements ReaderInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* supported types |
|
35
|
|
|
* |
|
36
|
|
|
* @var string[] |
|
37
|
|
|
* @access protected |
|
38
|
|
|
* @staticvar |
|
39
|
|
|
*/ |
|
40
|
|
|
protected static $supported = ['ini', 'json', 'php', 'xml', 'serialized']; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function readFile(/*# string */ $path) |
|
46
|
|
|
{ |
|
47
|
|
|
$suffix = substr($path, strpos($path, '.') + 1); |
|
48
|
|
|
|
|
49
|
|
|
if (!static::isSupported($suffix)) { |
|
50
|
|
|
throw new RuntimeException( |
|
51
|
|
|
Message::get(Message::MSG_PATH_TYPE_UNKNOWN, $suffix), |
|
52
|
|
|
Message::MSG_PATH_TYPE_UNKNOWN |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/* @var ReaderInterface $class */ |
|
57
|
|
|
$class = static::getNameSpace() . '\\' . ucfirst($suffix) . 'Reader'; |
|
58
|
|
|
|
|
59
|
|
|
return $class::readFile($path); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Is this type supported |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $type |
|
66
|
|
|
* @return bool |
|
67
|
|
|
* @access public |
|
68
|
|
|
* @static |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function isSupported(/*# string */ $type)/*# : bool */ |
|
71
|
|
|
{ |
|
72
|
|
|
return in_array($type, static::$supported); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|