Overview

Namespaces

  • Peridot
    • WebDriverManager
      • Binary
        • Decompression
        • Request
      • Console
      • Event
      • OS
      • Process
      • Test
  • PHP

Classes

  • StandardBinaryRequest

Interfaces

  • BinaryRequestInterface
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: namespace Peridot\WebDriverManager\Binary\Request;
 3: 
 4: use Peridot\WebDriverManager\Event\EventEmitterTrait;
 5: 
 6: /**
 7:  * StandardBinaryRequest uses file_get_contents with a stream context. It is capable of emitting
 8:  * download progress events containing bytes transferred, and bytes total.
 9:  *
10:  * @package Peridot\WebDriverManager\Binary
11:  */
12: class StandardBinaryRequest implements BinaryRequestInterface
13: {
14:     use EventEmitterTrait;
15: 
16:     /**
17:      * @var string
18:      */
19:     private $url;
20: 
21:     /**
22:      * {@inheritdoc}
23:      *
24:      * @param $url
25:      * @return string
26:      */
27:     public function request($url)
28:     {
29:         if (empty($url)) {
30:             return '';
31:         }
32: 
33:         $this->url = $url;
34:         $context = $this->getContext();
35:         $contents = file_get_contents($url, false, $context);
36:         $this->emit('complete');
37:         return $contents;
38:     }
39: 
40:     /**
41:      * Callback used when progress is made requesting.
42:      *
43:      * @param $notification_code
44:      * @param $severity
45:      * @param $message
46:      * @param $message_code
47:      * @param $bytes_transferred
48:      * @param $bytes_max
49:      * @return void
50:      */
51:     public function onNotification($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max)
52:     {
53:         switch($notification_code) {
54:             case STREAM_NOTIFY_PROGRESS:
55:                 $this->emit('progress', [$bytes_transferred]);
56:                 break;
57:             case STREAM_NOTIFY_FILE_SIZE_IS:
58:                 $this->emit('request.start', [$this->url, $bytes_max]);
59:                 break;
60:         }
61:     }
62: 
63:     /**
64:      * Create a context for file_get_contents.
65:      *
66:      * @return resource
67:      */
68:     protected function getContext()
69:     {
70:         $context_options = [
71:             'http' => [
72:                 'method' => 'GET',
73:                 'user_agent' => 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'
74:             ]
75:         ];
76:         $context = stream_context_create($context_options, [
77:             'notification' => [$this, 'onNotification']
78:         ]);
79:         return $context;
80:     }
81: }
82: 
WebDriver Manager API documentation generated by ApiGen