0

am writing a command line app using php, o need to automatically detect the os, so far i have the following code for detectOS method

public function detectOS() 
{
    $user_agent = $_SERVER['HTTP_USER_AGENT'];

    // is it linux?
    if(preg_match('/linux/i', $user_agent))
    {
        $this->env = new Linux;
        return $this->env;
    }

    // is it windows?
    if(preg_match('/windows/i', $user_agent))
    {
        $this->env = new Windows;
        return $this->env;
    }

    // is it mac
    if(preg_match('/mac/i', $user_agent))
    {
        $this->env = new Mac;
        return $this->env;
    }

    return false;
}

and this is the test am using for that method

// -------------------------------------------------- detectOS
public function testOsIsLinux()
{
    $os = new OS($this->path);
    $env_name = $os->detectOS();

    $this->assertInstanceOf('Linux', $os->env);
}

when i run the test i get the following error

PHPUnit_Framework_Exception: Undefined index: HTTP_USER_AGENT

so i guess that i need a native php function to detect the os am on.

any ideas, or i just implement it in python?

Oussama Elgoumri
  • 609
  • 1
  • 5
  • 15

0 Answers0