Example #1
0
$client->addScope(Google_Service_Datastore::DATASTORE);
$client->addScope(Google_Service_Datastore::USERINFO_EMAIL);
// add the api client and project id to our silex app
$app['google_client'] = $client;
$app['project_id'] = getenv('GCP_PROJECT_ID');
$app->get('/', function () use($app) {
    /** @var Google_Client $client */
    $client = $app['google_client'];
    /** @var Twig_Environment $twig */
    $twig = $app['twig'];
    // run a simple query to retrieve the comments
    // - last 20 items ordered by created DESC
    $projectId = $app['project_id'];
    $datastore = new Google_Service_Datastore($client);
    $util = new DatastoreHelper();
    $query = $util->createSimpleQuery();
    $response = $datastore->datasets->runQuery($projectId, $query);
    // create an array of the queried DataStore comments
    // and pass them to the view layer
    $comments = [];
    foreach ($response->getBatch()->getEntityResults() as $entityResult) {
        $properties = $entityResult->getEntity()->getProperties();
        $comments[] = ['name' => $properties['name']->getStringValue(), 'body' => $properties['body']->getStringValue(), 'created' => $properties['created']->getDateTimeValue()];
    }
    return $twig->render('datastore.html.twig', ['project' => $projectId, 'comments' => $comments]);
})->bind('home');
$app->post('/store', function (Request $request) use($app) {
    /** @var Google_Client $client */
    $client = $app['google_client'];
    /** @var Symfony\Component\Routing\Generator\UrlGenerator $urlgen */
    $urlgen = $app['url_generator'];