Skip to content

Commit 97a0af9

Browse files
authored
chore: upgrade datastore tutorial to new sample format (GoogleCloudPlatform#1648)
1 parent 4692e92 commit 97a0af9

16 files changed

+353
-717
lines changed

datastore/tutorial/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ or
2525
1. Download the json key file of the service account.
2626
1. Set GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to that file.
2727

28-
Then you can run the command: `php tasks.php`
28+
Then you can run the code samples:
29+
30+
1. Execute the snippets in the [src/](src/) directory by running:
31+
32+
```text
33+
$ php src/SNIPPET_NAME.php
34+
```
35+
36+
The usage will print for each if no arguments are provided.

datastore/tutorial/composer.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-datastore": "^1.2",
4-
"symfony/console": "^5.0"
5-
},
6-
"autoload": {
7-
"psr-4": { "Google\\Cloud\\Samples\\Datastore\\Tasks\\": "src" },
8-
"files": ["src/functions.php"]
3+
"google/cloud-datastore": "^1.2"
94
}
105
}

datastore/tutorial/src/CreateTaskCommand.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

datastore/tutorial/src/DeleteTaskCommand.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

datastore/tutorial/src/ListTasksCommand.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

datastore/tutorial/src/MarkTaskDoneCommand.php

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Datastore\Tasks;
19+
20+
use DateTime;
21+
// [START datastore_add_entity]
22+
use Google\Cloud\Datastore\DatastoreClient;
23+
24+
/**
25+
* Create a new task with a given description.
26+
*
27+
* @param string $projectId The Google Cloud project ID.
28+
* @param string $description
29+
*/
30+
function add_task(string $projectId, string $description)
31+
{
32+
$datastore = new DatastoreClient(['projectId' => $projectId]);
33+
34+
$taskKey = $datastore->key('Task');
35+
$task = $datastore->entity(
36+
$taskKey,
37+
[
38+
'created' => new DateTime(),
39+
'description' => $description,
40+
'done' => false
41+
],
42+
['excludeFromIndexes' => ['description']]
43+
);
44+
$datastore->insert($task);
45+
printf('Created new task with ID %d.' . PHP_EOL, $task->key()->pathEnd()['id']);
46+
}
47+
// [END datastore_add_entity]
48+
49+
// The following 2 lines are only needed to execute the samples on the CLI
50+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
51+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Datastore\Tasks;
19+
20+
if (isset($argv)) {
21+
return print("This file is for example only and cannot be executed\n");
22+
}
23+
24+
// [START datastore_build_service]
25+
use Google\Cloud\Datastore\DatastoreClient;
26+
27+
/**
28+
* Create a Cloud Datastore client.
29+
*
30+
* @param string $projectId The Google Cloud project ID.
31+
*/
32+
function build_service(string $projectId)
33+
{
34+
$datastore = new DatastoreClient(['projectId' => $projectId]);
35+
return $datastore;
36+
}
37+
// [END datastore_build_service]

0 commit comments

Comments
 (0)