/**
  * @expectedException        \Exception
  * @expectedExceptionMessage Unable to create from distributions: NonExistant, AnotherOne
  */
 public function testFromDistributionsNoMatch()
 {
     $expected = new \Datasift\IfconfigParser\Parser\Ubuntu();
     $actual = \Datasift\IfconfigParser::fromDistributions(array("NonExistant", "AnotherOne"));
     $this->assertEquals($expected, $actual);
 }
Example #2
0
 protected function getHostIpAddress()
 {
     // step 1 - how many adapters do we have on this box?
     // @todo Maybe we want to move this somewhere more central later?
     try {
         $os = Os::getOs();
     } catch (Exception $e) {
         return "127.0.0.1";
     }
     $parser = IfconfigParser::fromDistributions($os->getPossibleClassNames());
     $netifaces = new netifaces($os, $parser);
     $adapters = $netifaces->listAdapters();
     if (empty($adapters)) {
         throw new Exception("unable to parse host machine network adapters list");
     }
     // step 2 - find an adapter that is most likely to have the IP address
     // that we want
     //
     // our algorithm is simple:
     //
     // * we return the first non-loopback adapter that has an IP address
     // * if that fails, we return the first loopback adapter that has
     //   an IP address
     //
     // and if that fails, we give up
     try {
         // special case - when loopback is our only adapter
         $loopback = null;
         // loop over the adapters
         foreach ($adapters as $adapterToTest) {
             // does the adapter have an IP address?
             try {
                 $ipAddress = $netifaces->getIpAddress($adapterToTest);
             } catch (NetifacesException $e) {
                 // We couldn't get an IP address
                 $ipAddress = null;
             }
             // did we get back a valid IP address?
             $parts = explode('.', $ipAddress);
             if (count($parts) == 4) {
                 // success!
                 //
                 // but wait - is it actually the loopback interface?
                 if (in_array($adapterToTest, ['lo0', 'lo']) && $loopback === null) {
                     $loopback = $ipAddress;
                 } else {
                     return $ipAddress;
                 }
             }
         }
         // we didn't find any adapters with an IP address
         //
         // but is the loopback up and running?
         if ($loopback !== null) {
             // this is better than throwing an error
             return $loopback;
         }
         // if we get here, we could not determine the IP address of our
         // host :(
         //
         // this sucks
         throw new NetifacesException("Unable to determine IP address");
     } catch (NetifacesException $e) {
         throw new Exception("could not determine IP address of host machine");
     }
 }