Skip to content

Commit

Permalink
Move registry lists to the registry (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 8, 2022
1 parent 6106a1f commit 408e7cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 22 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ $finders = [
$cores = (new CpuCoreCounter($finders))->getCount();
```

### Choosing only logical or physical finders

`FinderRegistry` provides two helpful entries:

- `::getDefaultLogicalFinders()`: gives an ordered list of finders that will
look for the _logical_ CPU cores count
- `::getDefaultPhysicalFinders()`: gives an ordered list of finders that will
look for the _physical_ CPU cores count

By default when using `CpuCoreCounter`, it will use the logical finders since
it is more likely what you are looking for and is what is used by PHP source to
build the PHP binary.


### Checks what finders find what on your system

Expand All @@ -89,7 +102,7 @@ Note that the code marked as `@private` or `@internal` are excluded from the BCP
The following elements are also excluded:

- The `diagnose` and `execute` commands: those are for debugging/inspection purposes only
- `CpuCoreCounter::getDefaultFinders()`: new finders may be added or the order of finders changed at any time
- `FinderRegistry::get*Finders()`: new finders may be added or the order of finders changed at any time


## License
Expand Down
20 changes: 2 additions & 18 deletions src/CpuCoreCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
namespace Fidry\CpuCoreCounter;

use Fidry\CpuCoreCounter\Finder\CpuCoreFinder;
use Fidry\CpuCoreCounter\Finder\CpuInfoFinder;
use Fidry\CpuCoreCounter\Finder\HwLogicalFinder;
use Fidry\CpuCoreCounter\Finder\NProcFinder;
use Fidry\CpuCoreCounter\Finder\WindowsWmicLogicalFinder;
use Fidry\CpuCoreCounter\Finder\FinderRegistry;

final class CpuCoreCounter
{
Expand All @@ -36,7 +33,7 @@ final class CpuCoreCounter
*/
public function __construct(?array $finders = null)
{
$this->finders = $finders ?? self::getDefaultFinders();
$this->finders = $finders ?? FinderRegistry::getDefaultLogicalFinders();
}

/**
Expand Down Expand Up @@ -89,17 +86,4 @@ public function getFinderAndCores(): array

throw NumberOfCpuCoreNotFound::create();
}

/**
* @return list<CpuCoreFinder>
*/
public static function getDefaultFinders(): array
{
return [
new NProcFinder(),
new WindowsWmicLogicalFinder(),
new HwLogicalFinder(),
new CpuInfoFinder(),
];
}
}
29 changes: 26 additions & 3 deletions src/Finder/FinderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@

namespace Fidry\CpuCoreCounter\Finder;

/**
* @private
*/
final class FinderRegistry
{
/**
Expand All @@ -38,6 +35,32 @@ public static function getAllVariants(): array
];
}

/**
* @return list<CpuCoreFinder>
*/
public static function getDefaultLogicalFinders(): array
{
return [
new NProcFinder(),
new WindowsWmicLogicalFinder(),
new HwLogicalFinder(),
new LinuxyNProcessorFinder(),
new NProcessorFinder(),
new CpuInfoFinder(),
];
}

/**
* @return list<CpuCoreFinder>
*/
public static function getDefaultPhysicalFinders(): array
{
return [
new WindowsWmicPhysicalFinder(),
new HwPhysicalFinder(),
];
}

private function __construct()
{
}
Expand Down

0 comments on commit 408e7cf

Please sign in to comment.