1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use Symfony\Component\Process\Process; |
13
|
|
|
|
14
|
|
|
class Util |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Symfony\Component\Filesystem. |
18
|
|
|
* |
19
|
|
|
* @var Filesystem |
20
|
|
|
*/ |
21
|
|
|
protected static $fs; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Return Symfony\Component\Filesystem instance. |
25
|
|
|
* |
26
|
|
|
* @return Filesystem |
27
|
|
|
*/ |
28
|
|
|
public static function getFS() |
29
|
|
|
{ |
30
|
|
|
if (!self::$fs instanceof Filesystem) { |
31
|
|
|
self::$fs = new Filesystem(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return self::$fs; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Runs a Git command on the repository. |
39
|
|
|
* |
40
|
|
|
* @param string $command The command. |
41
|
|
|
* |
42
|
|
|
* @throws \RuntimeException If the command failed. |
43
|
|
|
* |
44
|
|
|
* @return string The trimmed output from the command. |
45
|
|
|
*/ |
46
|
|
|
public static function runGitCommand($command) |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
$process = new Process($command, __DIR__); |
50
|
|
|
if (0 === $process->run()) { |
51
|
|
|
return trim($process->getOutput()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
throw new \RuntimeException( |
55
|
|
|
sprintf( |
56
|
|
|
'The tag or commit hash could not be retrieved from "%s": %s', |
57
|
|
|
__DIR__, |
58
|
|
|
$process->getErrorOutput() |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} catch (\RuntimeException $exception) { |
62
|
|
|
throw new \RuntimeException('Process error'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Sort array by 'date' item. |
68
|
|
|
* |
69
|
|
|
* @param $a |
70
|
|
|
* @param $b |
71
|
|
|
* |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
|
|
public static function sortByDate($a, $b) |
75
|
|
|
{ |
76
|
|
|
if (!isset($a['date'])) { |
77
|
|
|
return -1; |
78
|
|
|
} |
79
|
|
|
if (!isset($b['date'])) { |
80
|
|
|
return 1; |
81
|
|
|
} |
82
|
|
|
if ($a['date'] == $b['date']) { |
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return ($a['date'] > $b['date']) ? -1 : 1; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Checks if a date is valid. |
91
|
|
|
* |
92
|
|
|
* @param string $date |
93
|
|
|
* @param string $format |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public static function isValidDate(string $date, string $format = 'Y-m-d'): bool |
98
|
|
|
{ |
99
|
|
|
$d = \DateTime::createFromFormat($format, $date); |
100
|
|
|
|
101
|
|
|
return $d && $d->format($format) === $date; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|