Installation
composer require boson-php/cpu-info
No additional configuration is required—this is a pure PHP package with no Laravel-specific setup.
First Use Case Retrieve basic CPU information in a Laravel controller or service:
use Boson\CpuInfo\CpuInfo;
$cpuInfo = new CpuInfo();
$info = $cpuInfo->getInfo();
// Output raw data (array)
dd($info);
// Or fetch specific properties:
$brand = $info['brand'];
$cores = $info['cores'];
Where to Look First
CpuInfo (minimal docs; inspect source for methods).getInfo() method returns an associative array with keys like:
brand (e.g., "Intel(R) Core(TM) i7-9700K")cores, threads, frequency, architecture, cache, etc.composer.json for compatibility).Dynamic System Checks Use CPU info to gate logic (e.g., disable heavy computations on low-core machines):
$cpu = new CpuInfo();
if ($cpu->getInfo()['cores'] < 4) {
Log::warning('Low-core CPU detected; throttling tasks.');
}
Hardware-Based Feature Flags Enable/disable features based on CPU capabilities (e.g., AVX support):
$flags = $cpu->getInfo()['flags'] ?? [];
if (in_array('avx', $flags)) {
$this->useOptimizedAlgorithm();
}
Logging/Monitoring Log CPU specs during deployment or in error reports:
report(function () use ($cpu) {
return [
'cpu' => $cpu->getInfo(),
'php_version' => PHP_VERSION,
];
});
Service Integration Bind the package to Laravel’s container for dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(CpuInfo::class, function () {
return new CpuInfo();
});
}
Then inject CpuInfo into controllers/services:
public function __construct(private CpuInfo $cpu) {}
$info = Cache::remember('cpu_info', now()->addHours(1), function () {
return (new CpuInfo())->getInfo();
});
CpuInfo in unit tests:
$this->mock(CpuInfo::class, function ($mock) {
$mock->shouldReceive('getInfo')->andReturn(['cores' => 8]);
});
Permission Issues
getInfo() returns empty/partial data or throws warnings./proc/cpuinfo (Linux) or equivalent system files. On shared hosting, this may fail silently.php -r "new \Boson\CpuInfo\CpuInfo();" in CLI to test permissions.Platform Limitations
wmic on Windows). Test on your target OS.--cpuset-cpus flags to isolate.Data Inconsistency
flags) may be missing on older CPUs or virtualized environments. Always check array_key_exists() before accessing properties.Performance Overhead
/proc/cpuinfo is lightweight, but avoid calling getInfo() in tight loops. Cache results if needed.dd((new CpuInfo())->getInfo());
error_reporting(E_ALL) to catch suppressed warnings about missing files.Custom Parsers
Override Boson\CpuInfo\CpuInfo::parse() to handle non-standard CPU info formats:
class CustomCpuInfo extends CpuInfo {
protected function parse(string $content): array {
// Custom logic here
return parent::parse($content);
}
}
Add Metadata Extend the returned array with app-specific data:
$info = (new CpuInfo())->getInfo();
$info['app_version'] = config('app.version');
Fallback Logic Provide defaults for missing keys:
$info = (new CpuInfo())->getInfo();
$cores = $info['cores'] ?? 1; // Default to 1 core if unavailable
CpuInfo class.CpuInfo and conditionally override methods.How can I help you explore Laravel packages today?