1313
1414namespace Webmozarts \Console \Parallelization ;
1515
16+ use Fidry \CpuCoreCounter \CpuCoreCounter as FidryCpuCoreCounter ;
17+ use Fidry \CpuCoreCounter \NumberOfCpuCoreNotFound ;
1618use Webmozart \Assert \Assert ;
1719
1820/**
1921 * @internal
20- * From https://github.com/phpstan/phpstan-src/blob/1.8.x/src/Process/CpuCoreCounter.php
2122 */
2223final class CpuCoreCounter
2324{
@@ -32,11 +33,7 @@ public static function getNumberOfCpuCores(): int
3233 return self ::$ count ;
3334 }
3435
35- if (!function_exists ('proc_open ' )) {
36- return self ::$ count = 1 ;
37- }
38-
39- $ count = $ _ENV ['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT ' ];
36+ $ count = $ _ENV ['WEBMOZARTS_CONSOLE_PARALLELIZATION_CPU_COUNT ' ] ?? false ;
4037
4138 if (false !== $ count ) {
4239 Assert::numeric ($ count );
@@ -45,38 +42,12 @@ public static function getNumberOfCpuCores(): int
4542 return self ::$ count = (int ) $ count ;
4643 }
4744
48- // from brianium/paratest
49- if (@is_file ('/proc/cpuinfo ' )) {
50- // Linux (and potentially Windows with linux sub systems)
51- $ cpuinfo = @file_get_contents ('/proc/cpuinfo ' );
52- if (false !== $ cpuinfo ) {
53- preg_match_all ('/^processor/m ' , $ cpuinfo , $ matches );
54-
55- return self ::$ count = count ($ matches [0 ]);
56- }
57- }
58-
59- if (DIRECTORY_SEPARATOR === '\\' ) {
60- // Windows
61- $ process = @popen ('wmic cpu get NumberOfLogicalProcessors ' , 'rb ' );
62- if (is_resource ($ process )) {
63- fgets ($ process );
64- $ cores = (int ) fgets ($ process );
65- pclose ($ process );
66-
67- return self ::$ count = $ cores ;
68- }
69- }
70-
71- $ process = @popen ('sysctl -n hw.ncpu ' , 'rb ' );
72- if (is_resource ($ process )) {
73- // *nix (Linux, BSD and Mac)
74- $ cores = (int ) fgets ($ process );
75- pclose ($ process );
76-
77- return self ::$ count = $ cores ;
45+ try {
46+ self ::$ count = (new FidryCpuCoreCounter ())->getCount ();
47+ } catch (NumberOfCpuCoreNotFound $ exception ) {
48+ self ::$ count = 1 ;
7849 }
7950
80- return self ::$ count = 2 ;
51+ return self ::$ count ;
8152 }
8253}
0 commit comments