generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from spatie/feature-retry-as-windows-admin
Implement Windows retrying as administrator to update PHP ini
- Loading branch information
Showing
8 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"output": "dump.phar", | ||
"check-requirements": false, | ||
"compactors": ["KevinGH\\Box\\Compactor\\PhpScoper"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"output": "ray.phar", | ||
"check-requirements": false, | ||
"compactors": ["KevinGH\\Box\\Compactor\\PhpScoper"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Spatie\GlobalRay\Commands; | ||
|
||
use Spatie\GlobalRay\Support\PhpIni; | ||
use Spatie\GlobalRay\Support\Platform; | ||
use Symfony\Component\Console\Input\Input; | ||
use Symfony\Component\Console\Output\Output; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
|
||
trait RetriesAsWindowsAdmin | ||
{ | ||
protected function shouldRetryAsWindowsAdmin(PhpIni $ini, Input $input): bool | ||
{ | ||
return ! is_writeable($ini->getPath()) | ||
&& $input->isInteractive() | ||
&& Platform::isWindowsNonAdminUser(); | ||
} | ||
|
||
protected function retryAsWindowsAdmin(PhpIni $ini, Input $input, Output $output): bool | ||
{ | ||
$question = new ConfirmationQuestion('Retry as Adminstrator?', false); | ||
|
||
if (! $this->getHelper('question')->ask($input, $output, $question)) { | ||
return false; | ||
} | ||
|
||
$tmpFile = tempnam(sys_get_temp_dir(), ''); | ||
|
||
file_put_contents($tmpFile, $ini->getContents()); | ||
|
||
$newIni = new PhpIni($tmpFile); | ||
|
||
$newIni->update('auto_prepend_file', $this->getLoaderPath()); | ||
|
||
return $this->tryCopyAsWindowsAdmin($newIni->getPath(), $ini->getPath()); | ||
} | ||
|
||
protected function tryCopyAsWindowsAdmin(string $source, string $destination): bool | ||
{ | ||
$tmpFile = tempnam(sys_get_temp_dir(), ''); | ||
$script = $tmpFile.'.vbs'; | ||
rename($tmpFile, $script); | ||
|
||
$source = str_replace('/', '\\', $source); | ||
$destination = str_replace('/', '\\', $destination); | ||
|
||
$vbs = <<<EOT | ||
Set UAC = CreateObject("Shell.Application") | ||
UAC.ShellExecute "cmd.exe", "/c copy /b /y ""$source"" ""$destination""", "", "runas", 0 | ||
Wscript.Sleep(300) | ||
EOT; | ||
|
||
file_put_contents($script, $vbs); | ||
exec('"'.$script.'"'); | ||
@unlink($script); | ||
|
||
if ($result = is_readable($destination)) { | ||
@unlink($source); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Spatie\GlobalRay\Support; | ||
|
||
class Platform | ||
{ | ||
public static function isWindows(): bool | ||
{ | ||
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; | ||
} | ||
|
||
public static function isWindowsNonAdminUser(): bool | ||
{ | ||
if (! static::isWindows()) { | ||
return false; | ||
} | ||
|
||
exec('fltmc.exe filters', $output, $exitCode); | ||
|
||
return $exitCode !== 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters