|
17 | 17 |
|
18 | 18 | namespace Google\Cloud\Samples\Datastore\Tasks; |
19 | 19 |
|
| 20 | +use DateTime; |
20 | 21 | use Generator; |
| 22 | +use Google; |
| 23 | +// [START build_service] |
21 | 24 | use Google\Cloud\Datastore\DatastoreClient; |
22 | | -use Google\Cloud\Datastore\Entity; |
23 | 25 |
|
24 | 26 | /** |
25 | | - * Create a Datastore service. |
| 27 | + * Create a Cloud Datastore client. |
26 | 28 | * |
| 29 | + * @param string $projectId |
27 | 30 | * @return DatastoreClient |
28 | 31 | */ |
29 | | -function build_datastore_service() |
| 32 | +function build_datastore_service($projectId) |
30 | 33 | { |
31 | | - // [START build_service] |
32 | | - $datastore = new DatastoreClient(); |
33 | | - // [END build_service] |
34 | | - $namespace = getenv('CLOUD_DATASTORE_NAMESPACE'); |
35 | | - if ($namespace !== false) { |
36 | | - $datastore = new DatastoreClient(['namespaceId' => $namespace]); |
37 | | - } |
| 34 | + $datastore = new DatastoreClient(['projectId' => $projectId]); |
38 | 35 | return $datastore; |
39 | 36 | } |
| 37 | +// [END build_service] |
| 38 | + |
| 39 | +/** |
| 40 | + * Create a Cloud Datastore client with a namespace. |
| 41 | + * |
| 42 | + * @return DatastoreClient |
| 43 | + */ |
| 44 | +function build_datastore_service_with_namespace() |
| 45 | +{ |
| 46 | + $namespaceId = getenv('CLOUD_DATASTORE_NAMESPACE'); |
| 47 | + if ($namespaceId === false) { |
| 48 | + return new DatastoreClient(); |
| 49 | + } |
| 50 | + return new DatastoreClient(['namespaceId' => $namespaceId]); |
| 51 | +} |
40 | 52 |
|
41 | 53 | // [START add_entity] |
42 | 54 | /** |
43 | 55 | * Create a new task with a given description. |
44 | 56 | * |
45 | 57 | * @param DatastoreClient $datastore |
46 | 58 | * @param $description |
47 | | - * @return \Google\Cloud\Datastore\Entity |
| 59 | + * @return Google\Cloud\Datastore\Entity |
48 | 60 | */ |
49 | 61 | function add_task(DatastoreClient $datastore, $description) |
50 | 62 | { |
51 | 63 | $taskKey = $datastore->key('Task'); |
52 | 64 | $task = $datastore->entity( |
53 | 65 | $taskKey, |
54 | 66 | [ |
55 | | - 'created' => new \DateTime(), |
| 67 | + 'created' => new DateTime(), |
56 | 68 | 'description' => $description, |
57 | 69 | 'done' => false |
58 | 70 | ], |
@@ -100,7 +112,7 @@ function delete_task(DatastoreClient $datastore, $taskId) |
100 | 112 | * Return a generator for all the tasks in ascending order of creation time. |
101 | 113 | * |
102 | 114 | * @param DatastoreClient $datastore |
103 | | - * @return Generator<Entity> |
| 115 | + * @return Generator<Google\Cloud\Datastore\Entity> |
104 | 116 | */ |
105 | 117 | function list_tasks(DatastoreClient $datastore) |
106 | 118 | { |
|
0 commit comments