generated from terremoth/php-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor for better naming and error checking
- Loading branch information
Showing
9 changed files
with
108 additions
and
62 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 |
---|---|---|
|
@@ -7,3 +7,6 @@ composer.phar | |
.idea | ||
.vscode | ||
.phpunit.cache | ||
|
||
demo.txt | ||
demo-file.tx |
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
This file was deleted.
Oops, something went wrong.
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,44 @@ | ||
<?php | ||
|
||
namespace Terremoth\Async; | ||
|
||
use Exception; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Process\Process as SymfonyProcess; | ||
|
||
readonly class PhpFile | ||
{ | ||
/** | ||
* @param string $file | ||
* @throws Exception | ||
* @param array<array-key, null>|list{int} $args | ||
*/ | ||
public function __construct(private string $file, private array $args = []) | ||
{ | ||
if (!is_readable($this->file)) { | ||
throw new InvalidArgumentException('Error: file ' . $this->file | ||
. ' does not exists or is not readable!'); | ||
} | ||
|
||
$finfo = finfo_open(FILEINFO_MIME_TYPE); | ||
$mimeType = finfo_file($finfo, $this->file); | ||
finfo_close($finfo); | ||
|
||
if (!in_array($mimeType, ['text/x-php', 'application/x-php', 'application/php', 'application/x-httpd-php'])) { | ||
throw new Exception('Error: file ' . $this->file . ' is not a PHP file!'); | ||
} | ||
} | ||
|
||
public function run(): void | ||
{ | ||
if (PHP_OS_FAMILY === 'Windows') { | ||
$template = ['start', "", '/B', PHP_BINARY, $this->file, ...$this->args]; | ||
$process = new SymfonyProcess($template); | ||
$process->start(); | ||
return; | ||
} | ||
|
||
$args = implode(' ', $this->args); | ||
exec(PHP_BINARY . ' ' . $this->file . ' ' . $args . ' > /dev/null 2>&1 &'); | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
function error(string $error): void | ||
{ | ||
$error = 'Error: ' . $error; | ||
fwrite(STDERR, $error); | ||
error_log($error); | ||
} | ||
|
||
function stringFromMemoryBlock(string $value): string | ||
{ | ||
$position = strpos($value, "\0"); | ||
if ($position === false) { | ||
return $value; | ||
} | ||
return substr($value, 0, $position); | ||
} |