Multi.php

Go to the documentation of this file.
00001 <?php
00002 
00026 class Robo47_Curl_Multi implements Countable
00027 {
00028 
00034     protected $_curlMulti = null;
00038     protected $_curls = array();
00039 
00044     public function __construct($curlMulti = null)
00045     {
00046         $this->setCurlMulti($curlMulti);
00047     }
00048 
00054     public function getCurls()
00055     {
00056         return $this->_curls;
00057     }
00058 
00064     public function addHandle(Robo47_Curl $curl)
00065     {
00066         curl_multi_add_handle($this->_curlMulti, $curl->getCurl());
00067         $this->_curls[] = $curl;
00068         return $this;
00069     }
00070 
00076     public function removeHandle(Robo47_Curl $curl)
00077     {
00078         curl_multi_remove_handle($this->_curlMulti, $curl->getCurl());
00079         foreach ($this->_curls as $key => $internalCurl) {
00080             if ($internalCurl === $curl) {
00081                 unset($this->_curls[$key]);
00082             }
00083         }
00084         return $this;
00085     }
00086 
00092     public function exec( & $running)
00093     {
00094         curl_multi_exec($this->_curlMulti, $running);
00095         return $this;
00096     }
00097 
00104     public function setCurlMulti($curlMulti)
00105     {
00106         if (null === $curlMulti) {
00107             $curlMulti = curl_multi_init();
00108         }
00109         if (!is_resource($curlMulti) ||
00110             get_resource_type($curlMulti) != 'curl_multi') {
00111             $message = '$curlMulti is not an curl-resource';
00112             throw new Robo47_Curl_Multi_Exception($message);
00113         }
00114         $this->_curlMulti = $curlMulti;
00115         return $this;
00116     }
00117 
00123     public function getCurlMulti()
00124     {
00125         return $this->_curlMulti;
00126     }
00127 
00133     public function count()
00134     {
00135         return count($this->_curls);
00136     }
00137 
00141     public function close()
00142     {
00143         @curl_multi_close($this->_curlMulti);
00144         return $this;
00145     }
00146 
00152     public function __destruct()
00153     {
00154         $this->close();
00155     }
00156 }