Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit 5573e5b

Browse files
committed
Allow non-strict mode - ignore missing lockfile
1 parent 25fd543 commit 5573e5b

File tree

2 files changed

+77
-8
lines changed

2 files changed

+77
-8
lines changed

src/Checker.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
class Checker
1515
{
16+
protected const FILE_REQS = 'composer.lock';
17+
protected const FILE_INSTALLED = 'installed.json';
18+
1619
/**
1720
* @var string
1821
*/
@@ -21,6 +24,10 @@ class Checker
2124
* @var string
2225
*/
2326
private $vendorDir;
27+
/**
28+
* @var bool Is strictly required `composer.lock` files?
29+
*/
30+
private $strictReqs = true;
2431

2532

2633
/**
@@ -71,17 +78,45 @@ public function validate(): void
7178
*/
7279
public function isReqsValid(): bool
7380
{
74-
$definitions = $this->loadReqs();
75-
$instalations = $this->loadInstalled();
81+
try {
82+
$definitions = $this->loadReqs();
83+
$instalations = $this->loadInstalled();
84+
85+
$diff = array_diff_assoc($definitions, $instalations);
86+
87+
return count($diff) === 0;
88+
} catch (FileReadException $e) {
89+
if($this->strictReqs !== true && $e->getRequiredfile() === self::FILE_REQS) {
90+
// Ignore missing `composer.lock` file - not deployed to production?
91+
return true;
92+
}
93+
throw $e;
94+
}
95+
}
7696

77-
$diff = array_diff_assoc($definitions, $instalations);
7897

79-
return count($diff) === 0;
98+
/**
99+
* @return bool
100+
*/
101+
public function isStrictReqs(): bool
102+
{
103+
return $this->strictReqs;
104+
}
105+
106+
/**
107+
* @param bool $strictReqs
108+
* @return Checker
109+
*/
110+
public function setStrictReqs(bool $strictReqs): self
111+
{
112+
$this->strictReqs = $strictReqs;
113+
return $this;
80114
}
81115

82116

83117
/**
84118
* @return array
119+
* @throws FileReadException
85120
* @throws RuntimeException
86121
*/
87122
protected function loadReqs(): array
@@ -100,16 +135,18 @@ protected function loadReqs(): array
100135

101136
/**
102137
* @return array
138+
* @throws FileReadException
103139
* @throws RuntimeException
104140
*/
105141
protected function loadReqsFile(): array
106142
{
107-
return $this->readJsonFile($this->rootDir . '/composer.lock');
143+
return $this->readJsonFile($this->rootDir . '/' . self::FILE_REQS);
108144
}
109145

110146

111147
/**
112148
* @return array
149+
* @throws FileReadException
113150
* @throws RuntimeException
114151
*/
115152
protected function loadInstalled(): array
@@ -128,17 +165,19 @@ protected function loadInstalled(): array
128165

129166
/**
130167
* @return array
168+
* @throws FileReadException
131169
* @throws RuntimeException
132170
*/
133171
protected function loadInstalledFile(): array
134172
{
135-
return $this->readJsonFile($this->vendorDir . '/composer/installed.json');
173+
return $this->readJsonFile($this->vendorDir . '/composer/' . self::FILE_INSTALLED);
136174
}
137175

138176

139177
/**
140178
* @param string $filename
141179
* @return array
180+
* @throws FileReadException
142181
* @throws RuntimeException
143182
*/
144183
protected function readJsonFile(string $filename): array
@@ -151,15 +190,16 @@ protected function readJsonFile(string $filename): array
151190
/**
152191
* @param string $file
153192
* @return string
154-
* @throws RuntimeException
193+
* @throws FileReadException
155194
* @link https://doc.nette.org/en/3.0/filesystem
156195
*/
157196
protected function readFile(string $file): string
158197
{
159198
$content = @file_get_contents($file); // @ is escalated to exception
160199
if ($content === false) {
161200
$lastError = preg_replace('#^\w+\(.*?\): #', '', (string)error_get_last()['message']);
162-
throw new RuntimeException("Unable to read file '$file'. " . $lastError);
201+
$requiredFile = basename($file);
202+
throw new FileReadException($requiredFile, "Unable to read file '$file'. " . $lastError);
163203
}
164204
return $content;
165205
}

src/FileReadException.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace JakubBoucek\ComposerVendorChecker;
5+
6+
use RuntimeException;
7+
use Throwable;
8+
9+
class FileReadException extends RuntimeException
10+
{
11+
/**
12+
* @var string
13+
*/
14+
private $requiredfile;
15+
16+
public function __construct(string $requiredfile, $message = '', $code = 0, Throwable $previous = null)
17+
{
18+
parent::__construct($message, $code, $previous);
19+
$this->requiredfile = $requiredfile;
20+
}
21+
22+
/**
23+
* @return string
24+
*/
25+
public function getRequiredfile(): string
26+
{
27+
return $this->requiredfile;
28+
}
29+
}

0 commit comments

Comments
 (0)