Skip to content

Commit 34b1cec

Browse files
Merging master.
2 parents a11c086 + 09d2a3d commit 34b1cec

File tree

17 files changed

+234
-295
lines changed

17 files changed

+234
-295
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ jobs:
2929

3030
- name: Install dependencies
3131
run: composer install --no-interaction --prefer-dist
32-
if: matrix.php != 8.0
33-
34-
- name: Install dependencies
35-
run: composer install --no-interaction --prefer-dist --ignore-platform-req=php
36-
if: matrix.php == 8.0
3732

3833
- name: Execute tests
3934
env:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ vendor/
33
composer.lock
44
error.log
55
.idea
6+
.phpunit.result.cache

cli/Valet/Brew.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Brew
2323
'php56'
2424
];
2525

26-
const LATEST_PHP_VERSION = 'php@7.4';
26+
const LATEST_PHP_VERSION = 'php@8.0';
2727

2828
var $cli, $files;
2929

@@ -41,14 +41,23 @@ function __construct(CommandLine $cli, Filesystem $files)
4141
}
4242

4343
/**
44-
* Determine if the given formula is installed.
44+
* Ensure the formula exists in the current Homebrew configuration
4545
*
4646
* @param string $formula
4747
* @return bool
4848
*/
4949
function installed($formula)
5050
{
51-
return in_array($formula, explode(PHP_EOL, $this->cli->runAsUser('brew list --formula | grep '.$formula)));
51+
$result = $this->cli->runAsUser("brew info $formula --json");
52+
53+
// should be a json response, but if not installed then "Error: No available formula ..."
54+
if (starts_with($result, 'Error: No')) {
55+
return false;
56+
}
57+
58+
$details = json_decode($result);
59+
60+
return !empty($details[0]->installed);
5261
}
5362

5463
/**
@@ -58,9 +67,11 @@ function installed($formula)
5867
*/
5968
function hasInstalledPhp()
6069
{
61-
return $this->supportedPhpVersions()->contains(function ($version) {
62-
return $this->installed($version);
70+
$installed = $this->installedPhpFormulae()->first(function ($formula) {
71+
return $this->supportedPhpVersions()->contains($formula);
6372
});
73+
74+
return !empty($installed);
6475
}
6576

6677
/**
@@ -73,13 +84,25 @@ function supportedPhpVersions()
7384
return collect(static::SUPPORTED_PHP_VERSIONS);
7485
}
7586

87+
function installedPhpFormulae()
88+
{
89+
return collect(
90+
explode(PHP_EOL, $this->cli->runAsUser('brew list --formula | grep php'))
91+
);
92+
}
93+
7694
/**
7795
* Get the aliased formula version from Homebrew
7896
*/
7997
function determineAliasedVersion($formula)
8098
{
8199
$details = json_decode($this->cli->runAsUser("brew info $formula --json"));
82-
return $details[0]->aliases[0] ?: 'ERROR - NO BREW ALIAS FOUND';
100+
101+
if (!empty($details[0]->aliases[0])) {
102+
return $details[0]->aliases[0];
103+
}
104+
105+
return 'ERROR - NO BREW ALIAS FOUND';
83106
}
84107

85108
/**
@@ -135,6 +158,9 @@ function installOrFail($formula, $options = [], $taps = [])
135158
}
136159

137160
output('<info>['.$formula.'] is not installed, installing it now via Brew...</info> 🍻');
161+
if ($formula !== 'php' && starts_with($formula, 'php') && preg_replace('/[^\d]/', '', $formula) < '73') {
162+
warning('Note: older PHP versions may take 10+ minutes to compile from source. Please wait ...');
163+
}
138164

139165
$this->cli->runAsUser(trim('brew install '.$formula.' '.implode(' ', $options)), function ($exitCode, $errorOutput) use ($formula) {
140166
output($errorOutput);
@@ -213,16 +239,16 @@ function hasLinkedPhp()
213239
function getParsedLinkedPhp()
214240
{
215241
if (! $this->hasLinkedPhp()) {
216-
throw new DomainException("Homebrew PHP appears not to be linked.");
242+
throw new DomainException("Homebrew PHP appears not to be linked. Please run [valet use [email protected]]");
217243
}
218244

219245
$resolvedPath = $this->files->readLink(BREW_PREFIX.'/bin/php');
220246

221247
/**
222248
* Typical homebrew path resolutions are like:
223-
* "../Cellar/php@7.2/7.2.13/bin/php"
249+
* "../Cellar/php@7.4/7.4.13/bin/php"
224250
* or older styles:
225-
* "../Cellar/php/7.2.9_2/bin/php
251+
* "../Cellar/php/7.4.9_2/bin/php
226252
* "../Cellar/php55/bin/php
227253
*/
228254
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
@@ -231,12 +257,11 @@ function getParsedLinkedPhp()
231257
}
232258

233259
/**
234-
* Gets the currently linked formula
235-
* E.g if under php, will be php, if under [email protected] will be that
236-
* Different to ->linkedPhp() in that this will just get the linked directory name (whether that is php, php55 or
237-
260+
* Gets the currently linked formula by identifying the symlink in the hombrew bin directory.
261+
* Different to ->linkedPhp() in that this will just get the linked directory name,
262+
* whether that is php, php74 or [email protected]
238263
*
239-
* @return mixed
264+
* @return string
240265
*/
241266
function getLinkedPhpFormula()
242267
{

cli/Valet/Diagnose.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Diagnose
2020
'brew services list',
2121
'brew list --formula --versions | grep -E "(php|nginx|dnsmasq|mariadb|mysql|mailhog|openssl)(@\d\..*)?\s"',
2222
'brew outdated',
23+
'brew tap',
2324
'php -v',
2425
'which -a php',
2526
'php --ini',
@@ -40,6 +41,8 @@ class Diagnose
4041
'ls -al ~/Library/LaunchAgents | grep homebrew',
4142
'ls -al /Library/LaunchAgents | grep homebrew',
4243
'ls -al /Library/LaunchDaemons | grep homebrew',
44+
'ls -aln /etc/resolv.conf',
45+
'cat /etc/resolv.conf',
4346
];
4447

4548
var $cli, $files, $print, $progressBar;

cli/Valet/PhpFpm.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ class PhpFpm
99
var $brew, $cli, $files;
1010

1111
var $taps = [
12-
'homebrew/homebrew-core'
12+
'homebrew/homebrew-core',
13+
'shivammathur/php',
1314
];
1415

1516
/**
@@ -142,11 +143,8 @@ function fpmConfigPath()
142143
{
143144
$version = $this->brew->linkedPhp();
144145

145-
$versionNormalized = preg_replace(
146-
'/php@?(\d)\.?(\d)/',
147-
'$1.$2',
148-
$version === 'php' ? Brew::LATEST_PHP_VERSION : $version
149-
);
146+
$versionNormalized = $this->normalizePhpVersion($version === 'php' ? Brew::LATEST_PHP_VERSION : $version);
147+
$versionNormalized = preg_replace('~[^\d\.]~', '', $versionNormalized);
150148

151149
return $versionNormalized === '5.6'
152150
? BREW_PREFIX.'/etc/php/5.6/php-fpm.conf'
@@ -171,14 +169,26 @@ function stopRunning()
171169
* Use a specific version of php
172170
*
173171
* @param $version
172+
* @param $force
174173
* @return string
175174
*/
176-
function useVersion($version)
175+
function useVersion($version, $force = false)
177176
{
178177
$version = $this->validateRequestedVersion($version);
179178

180-
// Install the relevant formula if not already installed
181-
$this->brew->ensureInstalled($version);
179+
try {
180+
if ($this->brew->linkedPhp() === $version && !$force) {
181+
output(sprintf('<info>Valet is already using version: <comment>%s</comment>.</info> To re-link and re-configure use the --force parameter.' . PHP_EOL,
182+
$version));
183+
exit();
184+
}
185+
} catch (DomainException $e)
186+
{ /* ignore thrown exception when no linked php is found */ }
187+
188+
if (!$this->brew->installed($version)) {
189+
// Install the relevant formula if not already installed
190+
$this->brew->ensureInstalled($version, [], $this->taps);
191+
}
182192

183193
// Unlink the current php if there is one
184194
if ($this->brew->hasLinkedPhp()) {
@@ -190,11 +200,23 @@ function useVersion($version)
190200
info(sprintf('Linking new version: %s', $version));
191201
$this->brew->link($version, true);
192202

203+
$this->stopRunning();
204+
205+
// ensure configuration is correct and start the linked version
193206
$this->install();
194207

195208
return $version === 'php' ? $this->brew->determineAliasedVersion($version) : $version;
196209
}
197210

211+
212+
/**
213+
* If passed php7.4 or php74 formats, normalize to [email protected] format.
214+
*/
215+
function normalizePhpVersion($version)
216+
{
217+
return preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version);
218+
}
219+
198220
/**
199221
* Validate the requested version to be sure we can support it.
200222
*
@@ -203,18 +225,7 @@ function useVersion($version)
203225
*/
204226
function validateRequestedVersion($version)
205227
{
206-
// If passed php7.2 or php72 formats, normalize to [email protected] format:
207-
$version = preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version);
208-
209-
if ($version === 'php') {
210-
if (strpos($this->brew->determineAliasedVersion($version), '@')) {
211-
return $version;
212-
}
213-
214-
if ($this->brew->hasInstalledPhp()) {
215-
throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: [email protected]');
216-
}
217-
}
228+
$version = $this->normalizePhpVersion($version);
218229

219230
if (!$this->brew->supportedPhpVersions()->contains($version)) {
220231
throw new DomainException(
@@ -225,6 +236,20 @@ function validateRequestedVersion($version)
225236
);
226237
}
227238

239+
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
240+
return $aliasedVersion;
241+
}
242+
243+
if ($version === 'php') {
244+
if (strpos($aliasedVersion = $this->brew->determineAliasedVersion($version), '@')) {
245+
return $aliasedVersion;
246+
}
247+
248+
if ($this->brew->hasInstalledPhp()) {
249+
throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: [email protected]');
250+
}
251+
}
252+
228253
return $version;
229254
}
230255
}

cli/includes/helpers.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ function ends_with($haystack, $needles) {
172172
}
173173
}
174174

175+
if (! function_exists('starts_with')) {
176+
/**
177+
* Determine if a given string starts with a given substring.
178+
*
179+
* @param string $haystack
180+
* @param string|string[] $needles
181+
* @return bool
182+
*/
183+
function starts_with($haystack, $needles)
184+
{
185+
foreach ((array) $needles as $needle) {
186+
if ((string) $needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
187+
return true;
188+
}
189+
}
190+
191+
return false;
192+
}
193+
}
194+
175195
/**
176196
* Get the user
177197
*/

cli/stubs/etc-phpfpm-error_log.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# php-fpm error logging directives
1+
; php-fpm error logging directives
22

33
error_log="VALET_HOME_PATH/Log/php-fpm.log"
44
log_errors=on

cli/valet.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
Container::setInstance(new Container);
3434

35-
$version = '2.13.4';
35+
$version = '2.13.15';
3636

3737
$app = new Application('Laravel Valet', $version);
3838

@@ -464,11 +464,14 @@
464464
/**
465465
* Allow the user to change the version of php valet uses
466466
*/
467-
$app->command('use phpVersion', function ($phpVersion) {
467+
$app->command('use [phpVersion] [--force]', function ($phpVersion, $force) {
468+
if (!$phpVersion) {
469+
return info('Valet is using ' . Brew::linkedPhp());
470+
}
471+
468472
PhpFpm::validateRequestedVersion($phpVersion);
469473

470-
PhpFpm::stopRunning();
471-
$newVersion = PhpFpm::useVersion($phpVersion);
474+
$newVersion = PhpFpm::useVersion($phpVersion, $force);
472475

473476
Nginx::restart();
474477
info(sprintf('Valet is now using %s.', $newVersion) . PHP_EOL);

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"require-dev": {
3535
"mockery/mockery": "^1.2.3",
36-
"phpunit/phpunit": "~5.7"
36+
"yoast/phpunit-polyfills": "^0.2.0"
3737
},
3838
"bin": [
3939
"valet"

phpunit.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false">
10+
stopOnFailure="false"
11+
beStrictAboutTestsThatDoNotTestAnything="false"
12+
>
1113
<testsuites>
1214
<testsuite name="Valet Test Suite">
1315
<directory suffix="Test.php">./tests</directory>

0 commit comments

Comments
 (0)