-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfiniteVersionWardenBundle.php
64 lines (51 loc) · 2.49 KB
/
InfiniteVersionWardenBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* This file is part of the Infinite VersionWardenBundle project.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Infinite\VersionWardenBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class InfiniteVersionWardenBundle extends Bundle
{
public function boot()
{
// This should run whenever debugging is enabled (in dev but not prod).
// In emergencies, this can be bypassed from parameters.yml.
if ($this->container->getParameter('kernel.debug') && !$this->container->getParameter('infinite_version_warden.bypass')) {
if ($this->container->hasParameter('kernel.project_dir')) {
$lockFilename = $this->container->getParameter('kernel.project_dir').'/composer.lock';
} else {
$lockFilename = $this->container->getParameter('kernel.root_dir') . '/../composer.lock';
}
// Load PHP version constraint from composer.lock
$composerLock = json_decode(file_get_contents($lockFilename));
$constraint = $composerLock->platform->php;
// Constraints must be formatted as "5.6.*" or "~5.6.23".
// Extract the "5.6" part.
if (!preg_match('/^(\d+\.\d+).\*$/', $constraint, $matches) &&
!preg_match('/^\~(\d+\.\d+).\d+$/', $constraint, $matches)) {
// Throwing an exception here might not result in console output, depending on display_errors.
// Print and die manually.
echo "Could not parse composer.lock version constraint. It must be formatted like '5.6.*' or '~7.1.1'\n";
exit(1);
}
// Compare to the currently running version.
$requiredVersion = $matches[1];
$runningVersion = sprintf('%s.%s', PHP_MAJOR_VERSION, PHP_MINOR_VERSION);
if ($runningVersion !== $requiredVersion) {
// Throwing an exception here might not result in console output, depending on display_errors.
// Print and die manually.
echo(sprintf(
"This project requires PHP %s, but is running as PHP %s\n",
$requiredVersion,
$runningVersion
));
exit(1);
}
}
}
}