Skip to content

Commit

Permalink
Make OnlyOn & SkippedOnWindows finders more generic and extend them t…
Browse files Browse the repository at this point in the history
…o OS families (#94)
  • Loading branch information
theofidry authored Dec 17, 2022
1 parent b830f3c commit 040127e
Show file tree
Hide file tree
Showing 13 changed files with 541 additions and 334 deletions.
4 changes: 2 additions & 2 deletions e2e/expected-output-osx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ NProcessorFinder: .
NProcFinder(all=true): F
NProcFinder(all=false): F
NullCpuCoreFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
SkipOnOSFamilyFinder(skip=(Windows),DummyCpuCoreFinder(value=1)): .
OnlyOnOSFamilyFinder(only=(Windows),DummyCpuCoreFinder(value=1)): F
WmicPhysicalFinder: F
WmicLogicalFinder: F
4 changes: 2 additions & 2 deletions e2e/expected-output-ubuntu
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ NProcessorFinder: F
NProcFinder(all=true): .
NProcFinder(all=false): .
NullCpuCoreFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
SkipOnOSFamilyFinder(skip=(Windows),DummyCpuCoreFinder(value=1)): .
OnlyOnOSFamilyFinder(only=(Windows),DummyCpuCoreFinder(value=1)): F
WmicPhysicalFinder: F
WmicLogicalFinder: F
4 changes: 2 additions & 2 deletions e2e/expected-output-ubuntu-restricted
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ NProcessorFinder: F
NProcFinder(all=true): F
NProcFinder(all=false): F
NullCpuCoreFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
SkipOnOSFamilyFinder(skip=(Windows),DummyCpuCoreFinder(value=1)): .
OnlyOnOSFamilyFinder(only=(Windows),DummyCpuCoreFinder(value=1)): F
WmicPhysicalFinder: F
WmicLogicalFinder: F
4 changes: 2 additions & 2 deletions e2e/expected-output-windows
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ NProcessorFinder: F
NProcFinder(all=true): F
NProcFinder(all=false): F
NullCpuCoreFinder: F
OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): .
SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): F
SkipOnOSFamilyFinder(skip=(Windows),DummyCpuCoreFinder(value=1)): F
OnlyOnOSFamilyFinder(only=(Windows),DummyCpuCoreFinder(value=1)): .
WmicPhysicalFinder: .
WmicLogicalFinder: .
8 changes: 4 additions & 4 deletions src/Finder/FinderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public static function getAllVariants(): array
new NProcFinder(true),
new NProcFinder(false),
new NullCpuCoreFinder(),
new OnlyOnWindowsFinder(
SkipOnOSFamilyFinder::forWindows(
new DummyCpuCoreFinder(1)
),
new SkipOnWindowsFinder(
OnlyOnOSFamilyFinder::forWindows(
new DummyCpuCoreFinder(1)
),
new WmicPhysicalFinder(),
Expand All @@ -47,7 +47,7 @@ public static function getAllVariants(): array
public static function getDefaultLogicalFinders(): array
{
return [
new OnlyOnWindowsFinder(new WmicLogicalFinder()),
OnlyOnOSFamilyFinder::forWindows(new WmicLogicalFinder()),
new NProcFinder(),
new HwLogicalFinder(),
new _NProcessorFinder(),
Expand All @@ -62,7 +62,7 @@ public static function getDefaultLogicalFinders(): array
public static function getDefaultPhysicalFinders(): array
{
return [
new OnlyOnWindowsFinder(new WmicPhysicalFinder()),
OnlyOnOSFamilyFinder::forWindows(new WmicPhysicalFinder()),
new HwPhysicalFinder(),
];
}
Expand Down
113 changes: 113 additions & 0 deletions src/Finder/OnlyOnOSFamilyFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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\Finder;

use function implode;
use function sprintf;
use const PHP_OS_FAMILY;

final class OnlyOnOSFamilyFinder implements CpuCoreFinder
{
/**
* @var list<string>
*/
private $skippedOSFamilies;

/**
* @var CpuCoreFinder
*/
private $decoratedFinder;

/**
* @param string|list<string> $skippedOSFamilyOrFamilies
*/
public function __construct(
$skippedOSFamilyOrFamilies,
CpuCoreFinder $decoratedFinder
) {
$this->skippedOSFamilies = (array) $skippedOSFamilyOrFamilies;
$this->decoratedFinder = $decoratedFinder;
}

public static function forWindows(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Windows',
$decoratedFinder
);
}

public static function forBSD(CpuCoreFinder $decoratedFinder): self
{
return new self(
'BSD',
$decoratedFinder
);
}

public static function forDarwin(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Darwin',
$decoratedFinder
);
}

public static function forSolaris(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Solaris',
$decoratedFinder
);
}

public static function forLinux(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Linux',
$decoratedFinder
);
}

public function diagnose(): string
{
return $this->skip()
? sprintf(
'Skipped platform detected ("%s").',
PHP_OS_FAMILY
)
: $this->decoratedFinder->diagnose();
}

public function find(): ?int
{
return $this->skip()
? null
: $this->decoratedFinder->find();
}

public function toString(): string
{
return sprintf(
'OnlyOnOSFamilyFinder(only=(%s),%s)',
implode(',', $this->skippedOSFamilies),
$this->decoratedFinder->toString()
);
}

private function skip(): bool
{
return !in_array(PHP_OS_FAMILY, $this->skippedOSFamilies, true);
}
}
59 changes: 0 additions & 59 deletions src/Finder/OnlyOnWindowsFinder.php

This file was deleted.

113 changes: 113 additions & 0 deletions src/Finder/SkipOnOSFamilyFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?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\Finder;

use function implode;
use function in_array;
use function sprintf;

final class SkipOnOSFamilyFinder implements CpuCoreFinder
{
/**
* @var list<string>
*/
private $skippedOSFamilies;

/**
* @var CpuCoreFinder
*/
private $decoratedFinder;

/**
* @param string|list<string> $skippedOSFamilyOrFamilies
*/
public function __construct(
$skippedOSFamilyOrFamilies,
CpuCoreFinder $decoratedFinder
) {
$this->skippedOSFamilies = (array) $skippedOSFamilyOrFamilies;
$this->decoratedFinder = $decoratedFinder;
}

public static function forWindows(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Windows',
$decoratedFinder
);
}

public static function forBSD(CpuCoreFinder $decoratedFinder): self
{
return new self(
'BSD',
$decoratedFinder
);
}

public static function forDarwin(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Darwin',
$decoratedFinder
);
}

public static function forSolaris(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Solaris',
$decoratedFinder
);
}

public static function forLinux(CpuCoreFinder $decoratedFinder): self
{
return new self(
'Linux',
$decoratedFinder
);
}

public function diagnose(): string
{
return $this->skip()
? sprintf(
'Skipped platform detected ("%s").',
PHP_OS_FAMILY
)
: $this->decoratedFinder->diagnose();
}

public function find(): ?int
{
return $this->skip()
? null
: $this->decoratedFinder->find();
}

public function toString(): string
{
return sprintf(
'SkipOnOSFamilyFinder(skip=(%s),%s)',
implode(',', $this->skippedOSFamilies),
$this->decoratedFinder->toString()
);
}

private function skip(): bool
{
return in_array(PHP_OS_FAMILY, $this->skippedOSFamilies, true);
}
}
Loading

0 comments on commit 040127e

Please sign in to comment.