| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright 2016 Google LLC |
| 5 |
* All rights reserved. |
| 6 |
* |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions are |
| 9 |
* met: |
| 10 |
* |
| 11 |
* * Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* * Redistributions in binary form must reproduce the above |
| 14 |
* copyright notice, this list of conditions and the following disclaimer |
| 15 |
* in the documentation and/or other materials provided with the |
| 16 |
* distribution. |
| 17 |
* * Neither the name of Google Inc. nor the names of its |
| 18 |
* contributors may be used to endorse or promote products derived from |
| 19 |
* this software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
namespace Dudlewebs\WPMCS\Google\ApiCore; |
| 34 |
|
| 35 |
/** |
| 36 |
* Class containing functions used to build the Agent header. |
| 37 |
*/ |
| 38 |
class AgentHeader |
| 39 |
{ |
| 40 |
const AGENT_HEADER_KEY = 'x-goog-api-client'; |
| 41 |
const UNKNOWN_VERSION = ''; |
| 42 |
/** |
| 43 |
* @param array $headerInfo { |
| 44 |
* Optional. |
| 45 |
* |
| 46 |
* @type string $phpVersion the PHP version. |
| 47 |
* @type string $libName the name of the client application. |
| 48 |
* @type string $libVersion the version of the client application. |
| 49 |
* @type string $gapicVersion the code generator version of the GAPIC library. |
| 50 |
* @type string $apiCoreVersion the ApiCore version. |
| 51 |
* @type string $grpcVersion the gRPC version. |
| 52 |
* @type string $restVersion the REST transport version (typically same as the |
| 53 |
* ApiCore version). |
| 54 |
* @type string $protobufVersion the protobuf version in format 'x.y.z+a' where both 'x.y.z' |
| 55 |
* and '+a' are optional, and where 'a' is a single letter representing the |
| 56 |
* implementation type of the protobuf runtime. It is recommended to use 'c' for a C |
| 57 |
* implementation, and 'n' for the native language implementation (PHP). |
| 58 |
* } |
| 59 |
* @return array Agent header array |
| 60 |
*/ |
| 61 |
public static function buildAgentHeader(array $headerInfo) |
| 62 |
{ |
| 63 |
$metricsHeaders = []; |
| 64 |
// The ordering of the headers is important. We use the fact that $metricsHeaders is an |
| 65 |
// ordered dict. The desired ordering is: |
| 66 |
// - phpVersion (gl-php/) |
| 67 |
// - clientName (e.g. gccl/) |
| 68 |
// - gapicVersion (gapic/) |
| 69 |
// - apiCoreVersion (gax/) |
| 70 |
// - grpcVersion (grpc/) |
| 71 |
// - restVersion (rest/) |
| 72 |
// - protobufVersion (pb/) |
| 73 |
$metricsHeaders['gl-php'] = $headerInfo['phpVersion'] ?? phpversion(); |
| 74 |
if (isset($headerInfo['libName'])) { |
| 75 |
$metricsHeaders[$headerInfo['libName']] = $headerInfo['libVersion'] ?? self::UNKNOWN_VERSION; |
| 76 |
} |
| 77 |
$apiCoreVersion = $headerInfo['apiCoreVersion'] ?? Version::getApiCoreVersion(); |
| 78 |
$metricsHeaders['gapic'] = $headerInfo['gapicVersion'] ?? self::UNKNOWN_VERSION; |
| 79 |
$metricsHeaders['gax'] = $apiCoreVersion; |
| 80 |
// Context on library type identification (between gRPC+REST and REST-only): |
| 81 |
// This uses the gRPC extension's version if 'grpcVersion' is not set, so we |
| 82 |
// cannot use the presence of 'grpcVersion' to determine whether or not a library |
| 83 |
// is gRPC+REST or REST-only. However, we cannot use the extension's presence |
| 84 |
// either, since some clients may have the extension installed but opt to use a |
| 85 |
// REST-only library (e.g. GCE). |
| 86 |
// TODO: Should we stop sending empty gRPC headers? |
| 87 |
$metricsHeaders['grpc'] = $headerInfo['grpcVersion'] ?? phpversion('grpc'); |
| 88 |
$metricsHeaders['rest'] = $headerInfo['restVersion'] ?? $apiCoreVersion; |
| 89 |
// The native version is not set by default because it is complex and costly to retrieve. |
| 90 |
// Users can override this default behavior if needed. |
| 91 |
$metricsHeaders['pb'] = $headerInfo['protobufVersion'] ?? (phpversion('protobuf') ? phpversion('protobuf') . '+c' : '+n'); |
| 92 |
$metricsList = []; |
| 93 |
foreach ($metricsHeaders as $key => $value) { |
| 94 |
$metricsList[] = $key . "/" . $value; |
| 95 |
} |
| 96 |
return [self::AGENT_HEADER_KEY => [implode(" ", $metricsList)]]; |
| 97 |
} |
| 98 |
/** |
| 99 |
* Reads the gapic version string from a VERSION file. In order to determine the file |
| 100 |
* location, this method follows this procedure: |
| 101 |
* - accepts a class name $callingClass |
| 102 |
* - identifies the file defining that class |
| 103 |
* - searches up the directory structure for the 'src' directory |
| 104 |
* - looks in the directory above 'src' for a file named VERSION |
| 105 |
* |
| 106 |
* @param string $callingClass |
| 107 |
* @return string the gapic version |
| 108 |
* @throws \ReflectionException |
| 109 |
*/ |
| 110 |
public static function readGapicVersionFromFile(string $callingClass) |
| 111 |
{ |
| 112 |
$callingClassFile = (new \ReflectionClass($callingClass))->getFileName(); |
| 113 |
$versionFile = substr($callingClassFile, 0, strrpos($callingClassFile, \DIRECTORY_SEPARATOR . 'src' . \DIRECTORY_SEPARATOR)) . \DIRECTORY_SEPARATOR . 'VERSION'; |
| 114 |
return Version::readVersionFile($versionFile); |
| 115 |
} |
| 116 |
} |
| 117 |
|