1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Utils\Composer; |
13
|
|
|
|
14
|
|
|
use Composer\Script\Event; |
15
|
|
|
|
16
|
|
|
final class UpgradeNotice |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param Event $event |
20
|
|
|
*/ |
21
|
|
|
final static public function doWrite(Event $event) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$package = $event->getComposer()->getPackage(); |
24
|
|
|
$version = substr($package->getVersion(), 0, 5); |
25
|
|
|
$console = new ConsoleIO($event->getIO(), $package); |
26
|
|
|
|
27
|
|
|
if (false === version_compare(PHP_VERSION, '5.4', '>=') |
28
|
|
|
|| false === $notice = static::getUpgradeNotice($version)) { |
|
|
|
|
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$console->write('<error> Update Notice: %s </error>', array($version)); |
33
|
|
|
|
34
|
|
|
foreach ($notice as $line) { |
35
|
|
|
$console->write($line); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if ($console->isInteractive()) { |
39
|
|
|
$console->ask('Press [ENTER] to acknowledge the above upgrade notice...'); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Read in updates file and parse for the passed package version. |
45
|
|
|
* |
46
|
|
|
* @param string $version |
47
|
|
|
* |
48
|
|
|
* @return string[]|false |
49
|
|
|
*/ |
50
|
|
|
final static private function getUpgradeNotice($version) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$lines = array_map(function ($line) { |
53
|
|
|
return preg_replace('{^[\s]{2}}', '', rtrim($line)); |
54
|
|
|
}, static::readUpgradeFile()); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$lines = array_filter($lines, function ($line) use ($version) { |
57
|
|
|
return static::filterLine($line, $version); |
|
|
|
|
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
array_shift($lines); |
61
|
|
|
|
62
|
|
|
return 0 === count($lines) ? false : $lines; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Keep lines if header matches package version, up until the next header is reached. |
67
|
|
|
* |
68
|
|
|
* @param string $line |
69
|
|
|
* @param string $version |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
final static private function filterLine($line, $version) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
static $keepLine = false; |
76
|
|
|
|
77
|
|
|
if (0 === strpos($line, '##')) { |
78
|
|
|
preg_match_all('{(?<version>[0-9]+\.[0-9]+(?:\.[0-9]+)?)}', $line, $matches); |
79
|
|
|
|
80
|
|
|
$keepLine = count(array_filter($matches['version'], function ($v) use ($version) { |
81
|
|
|
return false !== strpos($version, $v); |
82
|
|
|
})) > 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $keepLine; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Attempt to read the file to an array of lines. |
90
|
|
|
* |
91
|
|
|
* @return string[] |
92
|
|
|
*/ |
93
|
|
|
final static private function readUpgradeFile() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
if (false !== $lines = @file(__DIR__ . '/../../UPGRADE.md')) { |
96
|
|
|
return $lines; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return array(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|