generated from ergebnis/php-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor finders to leverage a common base (#47)
- Loading branch information
Showing
10 changed files
with
274 additions
and
292 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
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,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter; | ||
|
||
use function fgets; | ||
use function filter_var; | ||
use function function_exists; | ||
use function is_int; | ||
use function is_resource; | ||
use function pclose; | ||
use function popen; | ||
use const FILTER_VALIDATE_INT; | ||
|
||
abstract class PopenBasedFinder implements CpuCoreFinder | ||
{ | ||
/** | ||
* @return positive-int|null | ||
*/ | ||
public function find(): ?int | ||
{ | ||
if (!function_exists('popen')) { | ||
return null; | ||
} | ||
|
||
$process = popen($this->getCommand(), 'rb'); | ||
|
||
if (!is_resource($process)) { | ||
return null; | ||
} | ||
|
||
$processResult = fgets($process); | ||
pclose($process); | ||
|
||
return false === $processResult | ||
? null | ||
: self::countCpuCores($processResult); | ||
} | ||
|
||
/** | ||
* @internal | ||
* | ||
* @return positive-int|null | ||
*/ | ||
public static function countCpuCores(string $process): ?int | ||
{ | ||
$cpuCount = filter_var($process, FILTER_VALIDATE_INT); | ||
|
||
return is_int($cpuCount) && $cpuCount > 0 ? $cpuCount : null; | ||
} | ||
|
||
abstract protected function getCommand(): string; | ||
} |
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,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Fidry CPUCounter Config package. | ||
* | ||
* (c) Théo FIDRY <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Fidry\CpuCoreCounter\Test; | ||
|
||
use Fidry\CpuCoreCounter\PopenBasedFinder; | ||
|
||
final class DummyPopenBasedFinder extends PopenBasedFinder | ||
{ | ||
protected function getCommand(): string | ||
{ | ||
return ''; | ||
} | ||
} |
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
Oops, something went wrong.