|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHINT package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
|
7
|
|
|
* <https://github.com/adhocore> |
|
8
|
|
|
* |
|
9
|
|
|
* Licensed under MIT license. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ahc\Phint\Util; |
|
13
|
|
|
|
|
14
|
|
|
use Ahc\Json\Comment; |
|
15
|
|
|
|
|
16
|
|
|
class Path |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var string */ |
|
19
|
|
|
protected $phintPath; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Platform agnostic absolute path detection. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $path |
|
25
|
|
|
* |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public function isAbsolute(string $path): bool |
|
29
|
|
|
{ |
|
30
|
|
|
if (DIRECTORY_SEPARATOR === '\\') { |
|
31
|
|
|
return strpos($path, ':') === 1; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
return isset($path[0]) && $path[0] === '/'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getRelativePath($fullPath, $basePath): string |
|
38
|
|
|
{ |
|
39
|
|
|
if (strpos($fullPath, $basePath) === 0) { |
|
40
|
|
|
return substr($fullPath, strlen($basePath)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Hmm! |
|
44
|
|
|
return $fullPath; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function ensureDir(string $dir): bool |
|
48
|
|
|
{ |
|
49
|
|
|
if (!\is_dir($dir)) { |
|
50
|
|
|
return \mkdir($dir, 0777, true); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getExtension(string $filePath): string |
|
57
|
|
|
{ |
|
58
|
|
|
return \pathinfo($filePath, \PATHINFO_EXTENSION); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function readAsJson(string $filePath, bool $asArray = true) |
|
62
|
|
|
{ |
|
63
|
|
|
return (new Comment)->decode(\file_get_contents($filePath), $asArray); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function writeFile(string $file, $content, int $mode = null): bool |
|
67
|
|
|
{ |
|
68
|
|
|
$this->ensureDir(\dirname($file)); |
|
69
|
|
|
|
|
70
|
|
|
if (!\is_string($content)) { |
|
71
|
|
|
$content = \json_encode($content, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return \file_put_contents($file, $content, $mode) > 0; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getPhintPath(string $subpath = ''): string |
|
78
|
|
|
{ |
|
79
|
|
|
$this->initPhintPath(); |
|
80
|
|
|
|
|
81
|
|
|
if ($subpath && $this->phintPath) { |
|
82
|
|
|
return $this->phintPath . '/' . \ltrim($subpath, '/'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->phintPath; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function createBinaries(array $bins, string $basePath) |
|
89
|
|
|
{ |
|
90
|
|
|
foreach ($bins as $bin) { |
|
91
|
|
|
$bin = $this->join($basePath, $bin); |
|
92
|
|
|
|
|
93
|
|
|
if ($this->writeFile($bin, "#!/usr/bin/env php\n<?php\n")) { |
|
94
|
|
|
@\chmod($bin, 0755); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function join(...$paths): string |
|
100
|
|
|
{ |
|
101
|
|
|
if (\is_array($paths[0] ?? '')) { |
|
102
|
|
|
$paths = $paths[0]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$join = ''; |
|
|
|
|
|
|
106
|
|
|
foreach ($paths as $i => &$path) { |
|
107
|
|
|
$path = $i === 0 ? \rtrim($path, '\\/') : \trim($path, '\\/'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return \implode('/', $paths); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function initPhintPath() |
|
114
|
|
|
{ |
|
115
|
|
|
if (null !== $this->phintPath) { |
|
116
|
|
|
return; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->phintPath = ''; |
|
120
|
|
|
|
|
121
|
|
|
if (false !== $home = ($_SERVER['HOME'] ?? \getenv('HOME'))) { |
|
122
|
|
|
$path = \rtrim($home, '/') . '/.phint'; |
|
123
|
|
|
|
|
124
|
|
|
if ($this->ensureDir($path)) { |
|
125
|
|
|
$this->phintPath = $path; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: