13
13
14
14
namespace Webmozarts \Console \Parallelization ;
15
15
16
+ use Fidry \CpuCoreCounter \CpuCoreCounter as FidryCpuCoreCounter ;
17
+ use Fidry \CpuCoreCounter \NumberOfCpuCoreNotFound ;
16
18
use Webmozart \Assert \Assert ;
17
19
18
20
/**
19
21
* @internal
20
- * From https://github.com/phpstan/phpstan-src/blob/1.8.x/src/Process/CpuCoreCounter.php
21
22
*/
22
23
final class CpuCoreCounter
23
24
{
@@ -32,11 +33,7 @@ public static function getNumberOfCpuCores(): int
32
33
return self ::$ count ;
33
34
}
34
35
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 ;
40
37
41
38
if (false !== $ count ) {
42
39
Assert::numeric ($ count );
@@ -45,38 +42,12 @@ public static function getNumberOfCpuCores(): int
45
42
return self ::$ count = (int ) $ count ;
46
43
}
47
44
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 ;
78
49
}
79
50
80
- return self ::$ count = 2 ;
51
+ return self ::$ count ;
81
52
}
82
53
}
0 commit comments