/** * Perform cURL requests and throw appropriate exceptions. * * An array of parameters used in a curl_setopt_array, * multiple calls can be passed in. * * This function make use of curl_multi no matter it is * single request or not. * * Callbacks are used to handle results inside the array. * * $option['callbacks'] = array( * 'progress' => [Function] * , 'success' => [Function] * , 'failure' => [Function] * , 'always' => [Function] * ); * * @return void */ public static function curlRequest($options) { $options = Utility::wrapAssoc(array_values((array) $options)); $multiHandle = curl_multi_init(); // Initialize cUrl options array_walk($options, function (&$option) { // 1. Request headers $option['response'] = array('headers' => ''); $option[CURLOPT_HEADERFUNCTION] = function ($curl, $data) use(&$option) { $option['response']['headers'] .= $data; return strlen($data); }; // 2. Progress function $progressCallback =& $option['callbacks']['progress']; if ($progressCallback) { $option[CURLOPT_NOPROGRESS] = false; $option[CURLOPT_PROGRESSFUNCTION] = function () use(&$progressCallback) { if (func_num_args() == 4) { list($dSize, $dLen, $uSize, $uLen) = func_get_args(); } else { list($req, $dSize, $dLen, $uSize, $uLen) = func_get_args(); } if ($dSize || $dLen) { static $_dLen = 0; if ($_dLen != $dLen) { $_dLen = $dLen; /*! Note by Vicary @ 2.Oct.2012 * Total download size is often 0 if server doesn't * response with a Content-Length header. * * Total size guessing logic: * 1. if $dLen < 1M, assume 1M. * 2. if $dLen < 10M, assume 10M. * 3. if $dLen < 100M, assume 100M. * 4. if $dLen < 1G, assume 1G. */ if (!$dSize) { // Do not assume when size under 1K if ($dLen < 5000) { return; } elseif ($dLen < 10000000) { $dSize = 20000000; } elseif ($dLen < 100000000) { $dSize = 200000000; } elseif ($dLen < 1000000000) { $dSize = 2000000000; } else { $dSize = 20000000000; } // $dSize = $dLen / .05; } // Download progress, from 0 to 1. $progressArgs = array($dLen / $dSize, $dLen, $dSize); } } else { if ($uSize) { static $_uLen = 0; if ($_uLen != $uLen) { $_uLen = $uLen; $uSize *= -1; $uLen += $uSize; // Upload progress, from -1 to 0. $progressArgs = array($uLen / $uSize, $uLen, $uSize); } } } // Fire the event for each µSeconds. static $_tOffset = 0; $tOffset = microtime(1); if (isset($progressArgs) && $tOffset - $_tOffset > self::progressInterval()) { $_tOffset = $tOffset; Utility::forceInvoke($progressCallback, $progressArgs); } }; } unset($progressCallback); // 3. Apply cUrl options, numeric keys only. $option['handle'] = curl_init(); curl_setopt_array($option['handle'], array_filter_keys($option, 'is_int')); }); $requestIndex = 0; while ($requestIndex < self::$maximumRequests && isset($options[$requestIndex])) { curl_multi_add_handle($multiHandle, $options[$requestIndex++]['handle']); } // Start the multi request do { $status = curl_multi_exec($multiHandle, $active); /* Added by Vicary @ 6.Nov.2012 Blocks until there is a message arrives. */ curl_multi_select($multiHandle); do { $info = curl_multi_info_read($multiHandle, $queueLength); if ($info === FALSE) { continue; } $optionIndex = array_search($info['handle'], array_map(prop('handle'), $options)); if ($optionIndex === FALSE) { continue; } $curlOption =& $options[$optionIndex]; $callbacks =& $curlOption['callbacks']; // Success handler if ($info['result'] === CURLE_OK) { // Fire a 100% downloaded event. if (@$callbacks['progress']) { Utility::forceInvoke($callbacks['progress'], array(1, 1, 1)); usleep(self::progressInterval() * 1000000); } // Append HTTP status code $curlOption['status'] = curl_getinfo($info['handle'], CURLINFO_HTTP_CODE); Utility::forceInvoke(@$callbacks['success'], array(curl_multi_getcontent($info['handle']), $curlOption)); } else { $errorNumber = curl_errno($info['handle']); $errorMessage = curl_error($info['handle']); // libcurl errors, try to parse it. if ($errorNumber === 0) { if (preg_match('/errno: (\\d+)/', $errorMessage, $matches)) { $errorNumber = (int) $matches[1]; $curlErrors = unserialize(FRAMEWORK_NET_CURL_ERRORS); if (isset($curlErrors[$errorNumber])) { $errorMessage = $curlErrors[$errorNumber]; } } } Utility::forceInvoke(@$callbacks['failure'], array($errorNumber, $errorMessage, $curlOption)); unset($errorNumber, $errorMessage); } // Always handler Utility::forceInvoke(@$callbacks['always'], array($curlOption)); if (isset($options[$requestIndex])) { curl_multi_add_handle($multiHandle, $options[$requestIndex++]['handle']); // Keep the loop alive. $active = TRUE; } curl_multi_remove_handle($multiHandle, $info['handle']); curl_close($info['handle']); unset($info, $callbacks, $curlOption, $options[$optionIndex], $optionIndex); } while ($queueLength > 0); } while ($status === CURLM_CALL_MULTI_PERFORM || $active); curl_multi_close($multiHandle); }
/** * Get a list of data models from the collection. */ function find(array $filter = array()) { if ($filter && !util::isAssoc($filter)) { $filter = array($this->_primaryKey => $filter); } $filter[Node::FIELD_COLLECTION] = self::collectionName(); $collection = array(); Node::getAsync($filter, function ($data) use(&$collection) { // create a new instance for retrieved data $model = get_called_class(); $model = new $model($data); if (isset($this->__request)) { $model->__request = $this->__request; } if (isset($this->__response)) { $model->__response = $this->__response; } // force invoke internal function util::forceInvoke(array($model, 'afterLoad')); // add if model still has data if ($model->data()) { $collection[] = $model; } }); return $collection; }