Skip to content

Commit 9143636

Browse files
authored
Add the ability for drivers to check Composer dependencies (#1345)
1 parent 303462f commit 9143636

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

cli/Valet/Drivers/Specific/SculpinValetDriver.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,18 @@ public function serves(string $sitePath, string $siteName, string $uri): bool
1515
$this->isLegacySculpinProject($sitePath);
1616
}
1717

18-
private function isModernSculpinProject($sitePath): bool
18+
private function isModernSculpinProject(string $sitePath): bool
1919
{
2020
return is_dir($sitePath.'/source') &&
2121
is_dir($sitePath.'/output_dev') &&
22-
$this->composerRequiresSculpin($sitePath);
22+
$this->composerRequires($sitePath, 'sculpin/sculpin');
2323
}
2424

25-
private function isLegacySculpinProject($sitePath): bool
25+
private function isLegacySculpinProject(string $sitePath): bool
2626
{
2727
return is_dir($sitePath.'/.sculpin');
2828
}
2929

30-
private function composerRequiresSculpin($sitePath): bool
31-
{
32-
if (! file_exists($sitePath.'/composer.json')) {
33-
return false;
34-
}
35-
36-
$composer_json_source = file_get_contents($sitePath.'/composer.json');
37-
$composer_json = json_decode($composer_json_source, true);
38-
39-
if (json_last_error() !== JSON_ERROR_NONE) {
40-
return false;
41-
}
42-
43-
return isset($composer_json['require']['sculpin/sculpin']);
44-
}
45-
4630
/**
4731
* Mutate the incoming URI.
4832
*/

cli/Valet/Drivers/ValetDriver.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,20 @@ public function loadServerEnvironmentVariables(string $sitePath, string $siteNam
196196
putenv($key.'='.$value);
197197
}
198198
}
199+
200+
public function composerRequires(string $sitePath, string $namespacedPackage): bool
201+
{
202+
if (! file_exists($sitePath.'/composer.json')) {
203+
return false;
204+
}
205+
206+
$composer_json_source = file_get_contents($sitePath.'/composer.json');
207+
$composer_json = json_decode($composer_json_source, true);
208+
209+
if (json_last_error() !== JSON_ERROR_NONE) {
210+
return false;
211+
}
212+
213+
return isset($composer_json['require'][$namespacedPackage]);
214+
}
199215
}

tests/Drivers/ValetDriverTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Valet\Drivers\BasicValetDriver;
34
use Valet\Drivers\Specific\BedrockValetDriver;
45
use Valet\Drivers\ValetDriver;
56

@@ -28,4 +29,11 @@ public function test_it_prioritizes_non_basic_matches()
2829
$this->assertNotEquals('Valet\Drivers\BasicWithPublicValetDriver', get_class($assignedDriver));
2930
$this->assertNotEquals('Valet\Drivers\BasicValetDriver', get_class($assignedDriver));
3031
}
32+
33+
public function test_it_checks_composer_dependencies()
34+
{
35+
$driver = new BasicValetDriver;
36+
$this->assertTrue($driver->composerRequires(__DIR__.'/../files/sites/has-composer', 'tightenco/collect'));
37+
$this->assertFalse($driver->composerRequires(__DIR__.'/../files/sites/has-composer', 'tightenco/ziggy'));
38+
}
3139
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"tightenco/collect": "1.0.0"
4+
}
5+
}

0 commit comments

Comments
 (0)