|
15 | 15 | * limitations under the License. |
16 | 16 | */ |
17 | 17 |
|
18 | | -// Include Google Cloud dependendencies using Composer |
19 | | -require_once __DIR__ . '/../vendor/autoload.php'; |
20 | | - |
21 | | -if (count($argv) < 2 || count($argv) > 7) { |
22 | | - return printf("Usage: php %s SCOPE [QUERY] [ASSET_TYPES] [PAGE_SIZE] [PAGE_TOKEN] [ORDER_BY]\n", __FILE__); |
23 | | -} |
24 | | -list($_, $scope) = $argv; |
25 | | -$query = isset($argv[2]) ? $argv[2] : ''; |
26 | | -$assetTypes = isset($argv[3]) ? $argv[3] : ''; |
27 | | -$pageSize = isset($argv[4]) ? (int) $argv[4] : 0; |
28 | | -$pageToken = isset($argv[5]) ? $argv[5] : ''; |
29 | | -$orderBy = isset($argv[6]) ? $argv[6] : ''; |
| 18 | +namespace Google\Cloud\Samples\Asset; |
30 | 19 |
|
31 | 20 | // [START asset_quickstart_search_all_resources] |
32 | 21 | use Google\Cloud\Asset\V1\AssetServiceClient; |
33 | 22 |
|
34 | | -/** Uncomment and populate these variables in your code */ |
35 | | -// $scope = 'Scope of the search'; |
36 | | -// $query = ''; // (Optional) Query statement |
37 | | -// $assetTypes = ''; // (Optional) Asset types to search for |
38 | | -// $pageSize = 0; // (Optional) Size of each result page |
39 | | -// $pageToken = ''; // (Optional) Token produced by the preceding call |
40 | | -// $orderBy = ''; // (Optional) Fields to sort the results |
41 | | - |
42 | | -// Instantiate a client. |
43 | | -$asset = new AssetServiceClient(); |
| 23 | +/** |
| 24 | + * @param string $scope Scope of the search |
| 25 | + * @param string $query (Optional) Query statement |
| 26 | + * @param string|array $assetTypes (Optional) Asset types to search for |
| 27 | + * @param int $pageSize (Optional) Size of each result page |
| 28 | + * @param string $pageToken (Optional) Token produced by the preceding call |
| 29 | + * @param string $orderBy (Optional) Fields to sort the results |
| 30 | + */ |
| 31 | +function search_all_resources( |
| 32 | + string $scope, |
| 33 | + string $query = '', |
| 34 | + array $assetTypes = [], |
| 35 | + int $pageSize = 0, |
| 36 | + string $pageToken = '', |
| 37 | + string $orderBy = '' |
| 38 | +) { |
| 39 | + // Instantiate a client. |
| 40 | + $asset = new AssetServiceClient(); |
44 | 41 |
|
45 | | -// Run request |
46 | | -$response = $asset->searchAllResources($scope, [ |
47 | | - 'query' => $query, |
48 | | - 'assetTypes' => empty($assetTypes) ? [] : explode(',', $assetTypes), |
49 | | - 'pageSize' => $pageSize, |
50 | | - 'pageToken' => $pageToken, |
51 | | - 'orderBy' => $orderBy |
52 | | -]); |
| 42 | + // Run request |
| 43 | + $response = $asset->searchAllResources($scope, [ |
| 44 | + 'query' => $query, |
| 45 | + 'assetTypes' => $assetTypes, |
| 46 | + 'pageSize' => $pageSize, |
| 47 | + 'pageToken' => $pageToken, |
| 48 | + 'orderBy' => $orderBy |
| 49 | + ]); |
53 | 50 |
|
54 | | -// Print the resource names in the first page of the result |
55 | | -foreach ($response->getPage() as $resource) { |
56 | | - print($resource->getName() . PHP_EOL); |
| 51 | + // Print the resource names in the first page of the result |
| 52 | + foreach ($response->getPage() as $resource) { |
| 53 | + print($resource->getName() . PHP_EOL); |
| 54 | + } |
57 | 55 | } |
58 | 56 | // [END asset_quickstart_search_all_resources] |
| 57 | + |
| 58 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 59 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments