|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Retrinko\Ini; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Retrinko\Ini\Exceptions\FileException; |
|
8
|
|
|
|
|
9
|
|
|
class IniFileLocator |
|
10
|
|
|
{ |
|
11
|
|
|
const LOCAL_FILE_CHUNK = 'local'; |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $supportedExtensions = ['ini']; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var IniFileLocator |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $instance; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* IniFileLocator constructor. |
|
25
|
|
|
*/ |
|
26
|
1 |
|
protected function __construct() |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
1 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return IniFileLocator |
|
33
|
|
|
*/ |
|
34
|
15 |
|
public static function i() |
|
35
|
|
|
{ |
|
36
|
15 |
|
return self::getInstance(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return IniFileLocator |
|
41
|
|
|
*/ |
|
42
|
15 |
|
public static function getInstance() |
|
43
|
|
|
{ |
|
44
|
15 |
|
if (is_null(self::$instance)) |
|
45
|
15 |
|
{ |
|
46
|
1 |
|
self::$instance = new self(); |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
15 |
|
return self::$instance; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
private function __clone() |
|
53
|
|
|
{ |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
|
57
|
|
|
private function __wakeup() |
|
58
|
|
|
{ |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string $filePath |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
* @throws FileException |
|
66
|
|
|
*/ |
|
67
|
14 |
|
public function locate($filePath) |
|
68
|
|
|
{ |
|
69
|
14 |
|
$file = $filePath; |
|
70
|
14 |
|
if (!is_file($filePath)) |
|
71
|
14 |
|
{ |
|
72
|
2 |
|
throw new FileException(sprintf('Invalid file! File "%s" does not exist.', |
|
73
|
2 |
|
$filePath)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
if (!is_readable($filePath)) |
|
77
|
12 |
|
{ |
|
78
|
|
|
throw new FileException(sprintf('Invalid file! File "%s" is not readable.', |
|
79
|
|
|
$filePath)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
12 |
|
$pathInfo = pathinfo($filePath); |
|
83
|
12 |
|
if (!isset($pathInfo['extension']) |
|
84
|
12 |
|
|| !in_array($pathInfo['extension'], $this->supportedExtensions) |
|
85
|
12 |
|
) |
|
86
|
12 |
|
{ |
|
87
|
|
|
throw new FileException(sprintf('Invalid file extension! Supported file extensions: %s', |
|
88
|
|
|
implode(', ', $this->supportedExtensions))); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
12 |
|
$localFileName = $pathInfo['dirname'] . DIRECTORY_SEPARATOR |
|
92
|
12 |
|
. $this->composeLocalFileName($pathInfo['filename'], |
|
93
|
12 |
|
$pathInfo['extension']); |
|
94
|
12 |
|
if (is_file($localFileName) && is_readable($localFileName)) |
|
95
|
12 |
|
{ |
|
96
|
1 |
|
$file = $localFileName; |
|
97
|
1 |
|
} |
|
98
|
|
|
|
|
99
|
12 |
|
return realpath($file); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $baseName |
|
104
|
|
|
* @param string $extension |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
12 |
|
protected function composeLocalFileName($baseName, $extension) |
|
109
|
|
|
{ |
|
110
|
12 |
|
return $baseName . '.' . self::LOCAL_FILE_CHUNK . '.' . $extension; |
|
111
|
|
|
} |
|
112
|
|
|
} |