Example #1
0
 /**
  * Create a new object that contains all the detected information
  *
  * @param  array|string   $headers   An array with all of the headers or a string with just the User-Agent header
  */
 public function __construct($headers)
 {
     parent::__construct();
     $analyser = new Analyser($headers);
     $analyser->setdata($this);
     $analyser->analyse();
 }
Example #2
0
 public function testCreatingAnalyserSetData()
 {
     $analyser = new Analyser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
     $this->assertTrue($analyser instanceof \WhichBrowser\Analyser);
     $input = new Main();
     $this->assertTrue($input instanceof \WhichBrowser\Model\Main);
     $analyser->setData($input);
     $analyser->analyse();
     $output = $analyser->getData();
     $this->assertTrue($output instanceof \WhichBrowser\Model\Main);
 }
Example #3
0
 /**
  * Analyse the provided headers or User-Agent string
  *
  * @param  array|string   $headers   An array with all of the headers or a string with just the User-Agent header
  */
 public function analyse($headers, $options = [])
 {
     $o = $options;
     if (is_string($headers)) {
         $h = ['User-Agent' => $headers];
     } else {
         if (isset($headers['headers'])) {
             $h = $headers['headers'];
             unset($headers['headers']);
             $o = array_merge($headers, $options);
         } else {
             $h = $headers;
         }
     }
     if ($this->analyseWithCache($h, $o)) {
         return;
     }
     $analyser = new Analyser($h, $o);
     $analyser->setdata($this);
     $analyser->analyse();
 }
Example #4
0
 /**
  * Retrieve the result from the cache, or analyse and store in the cache
  *
  * @internal
  *
  * @param  array|string     $headers   An array with all of the headers or a string with just the User-Agent header
  *
  * @return  boolean         did we actually retrieve or analyse results
  */
 private function analyseWithCache($headers, $options = [])
 {
     if (isset($options['cache'])) {
         if (isset($options['cacheExpires'])) {
             $this->setCache($options['cache'], $options['cacheExpires']);
         } else {
             $this->setCache($options['cache']);
         }
     }
     if ($this->cache instanceof CacheItemPoolInterface) {
         $item = $this->cache->getItem('whichbrowser-' . md5(serialize($headers)));
         if ($item->isHit()) {
             $this->applyCachedData($item->get());
         } else {
             $analyser = new Analyser($headers, $options);
             $analyser->setdata($this);
             $analyser->analyse();
             $item->set($this->retrieveCachedData());
             $item->expiresAfter($this->expires);
             $this->cache->save($item);
         }
         return true;
     }
 }