Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Cpu Info Laravel Package

boson-php/cpu-info

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require boson-php/cpu-info
    

    No additional configuration is required—this is a pure PHP package with no Laravel-specific setup.

  2. 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'];
    
  3. Where to Look First

    • Class Reference: CpuInfo (minimal docs; inspect source for methods).
    • Output Structure: The getInfo() method returns an associative array with keys like:
      • brand (e.g., "Intel(R) Core(TM) i7-9700K")
      • cores, threads, frequency, architecture, cache, etc.
    • Dependencies: None beyond PHP 8.0+ (check your composer.json for compatibility).

Implementation Patterns

Core Workflows

  1. 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.');
    }
    
  2. 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();
    }
    
  3. Logging/Monitoring Log CPU specs during deployment or in error reports:

    report(function () use ($cpu) {
        return [
            'cpu' => $cpu->getInfo(),
            'php_version' => PHP_VERSION,
        ];
    });
    
  4. 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) {}
    

Laravel-Specific Tips

  • Caching: Cache CPU info if your app runs long-lived processes (e.g., queues):
    $info = Cache::remember('cpu_info', now()->addHours(1), function () {
        return (new CpuInfo())->getInfo();
    });
    
  • Testing: Mock CpuInfo in unit tests:
    $this->mock(CpuInfo::class, function ($mock) {
        $mock->shouldReceive('getInfo')->andReturn(['cores' => 8]);
    });
    

Gotchas and Tips

Pitfalls

  1. Permission Issues

    • Symptom: getInfo() returns empty/partial data or throws warnings.
    • Fix: Ensure PHP has read access to /proc/cpuinfo (Linux) or equivalent system files. On shared hosting, this may fail silently.
    • Workaround: Run php -r "new \Boson\CpuInfo\CpuInfo();" in CLI to test permissions.
  2. Platform Limitations

    • Windows/macOS: The package relies on parsing system files (e.g., wmic on Windows). Test on your target OS.
    • Docker/Containers: CPU info may reflect the host, not the container. Use --cpuset-cpus flags to isolate.
  3. Data Inconsistency

    • Some keys (e.g., flags) may be missing on older CPUs or virtualized environments. Always check array_key_exists() before accessing properties.
  4. Performance Overhead

    • Parsing /proc/cpuinfo is lightweight, but avoid calling getInfo() in tight loops. Cache results if needed.

Debugging

  • Verify Output: Dump the full array to confirm expected keys:
    dd((new CpuInfo())->getInfo());
    
  • Check for Warnings: Run with error_reporting(E_ALL) to catch suppressed warnings about missing files.

Extension Points

  1. 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);
        }
    }
    
  2. Add Metadata Extend the returned array with app-specific data:

    $info = (new CpuInfo())->getInfo();
    $info['app_version'] = config('app.version');
    
  3. Fallback Logic Provide defaults for missing keys:

    $info = (new CpuInfo())->getInfo();
    $cores = $info['cores'] ?? 1; // Default to 1 core if unavailable
    

Config Quirks

  • No Config File: This package has no Laravel config. All behavior is hardcoded in the CpuInfo class.
  • Environment Awareness: If you need environment-specific behavior (e.g., different parsing for CI vs. production), subclass CpuInfo and conditionally override methods.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cocosmos/filament-sticky-save-bar
patrickbussmann/oauth2-apple
3brs/enterprise-security-bundle
anousss007/vigilance
supportpal/eloquent-model
ardenexal/fhir-models
laravel-at/laravel-image-sanitize
romalytar/yammi-audit-log-laravel
ardenexal/fhir-validation
arshaviras/weather-widget
laravel-chronicle/core
sunchayn/nimbus
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope