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
|
|
|
use Phossa2\Shared\Exception\NotFoundException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ReaderAbstract |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Shared |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @version 2.0.1 |
28
|
|
|
* @since 2.0.1 added |
29
|
|
|
*/ |
30
|
|
|
abstract class ReaderAbstract extends StaticAbstract implements ReaderInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
|
|
public static function readFile(/*# string */ $path) |
36
|
|
|
{ |
37
|
|
|
// check file |
38
|
|
|
static::checkPath($path); |
39
|
|
|
|
40
|
|
|
// read file |
41
|
|
|
$data = static::readFromFile($path); |
42
|
|
|
|
43
|
|
|
// exception on error |
44
|
|
|
if (false === $data || null === $data) { |
45
|
|
|
throw new RuntimeException(static::getError($path)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $data; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check path existance & readability |
53
|
|
|
* |
54
|
|
|
* @param string $path |
55
|
|
|
* @throws NotFoundException if $path not found |
56
|
|
|
* @throws RuntimeException if $path not readable |
57
|
|
|
* @access protected |
58
|
|
|
* @static |
59
|
|
|
*/ |
60
|
|
|
protected static function checkPath(/*# string */ $path) |
61
|
|
|
{ |
62
|
|
|
if (!file_exists($path)) { |
63
|
|
|
throw new NotFoundException( |
64
|
|
|
Message::get(Message::MSG_PATH_NOTFOUND, $path), |
65
|
|
|
Message::MSG_PATH_NOTFOUND |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!is_readable($path)) { |
70
|
|
|
throw new RuntimeException( |
71
|
|
|
Message::get(Message::MSG_PATH_NONREADABLE, $path), |
72
|
|
|
Message::MSG_PATH_NONREADABLE |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get custom error |
79
|
|
|
* |
80
|
|
|
* @param string $path |
81
|
|
|
* @access protected |
82
|
|
|
* @static |
83
|
|
|
*/ |
84
|
|
|
protected static function getError(/*# string */ $path)/*#: string */ |
85
|
|
|
{ |
86
|
|
|
// default |
87
|
|
|
return error_get_last()['message']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Really read from file |
92
|
|
|
* |
93
|
|
|
* @param string $path |
94
|
|
|
* @return mixed |
95
|
|
|
* @access protected |
96
|
|
|
* @static |
97
|
|
|
*/ |
98
|
|
|
protected static function readFromFile($path) |
99
|
|
|
{ |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|