Skip to content

Commit

Permalink
Getting Started: Sessions (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Oct 3, 2019
1 parent 5813099 commit b3121b4
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Platform. Please follow the tutorials for instructions on using these samples.

* [Getting started with PHP][getting-started]
* [Authenticating users on PHP][authenticate-users]
* [Session handling with PHP][sessions]

## Contributing changes

Expand All @@ -20,3 +21,4 @@ Platform. Please follow the tutorials for instructions on using these samples.
[travis-link]: https://travis-ci.org/GoogleCloudPlatform/getting-started-php
[getting-started]: http://cloud.google.com/php/getting-started
[authenticate-users]: http://cloud.google.com/php/getting-started/authenticate-users
[sessions]: http://cloud.google.com/php/getting-started/sessions
5 changes: 0 additions & 5 deletions bookshelf/phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
<directory>test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
<env name="APP_ENV" value="test"/>
Expand Down
2 changes: 0 additions & 2 deletions bookshelf/test/DeployTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Google\Cloud\TestUtils\FileUtil;
use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
use Google\Cloud\TestUtils\TestTrait;
use Google\Cloud\Logging\LoggingClient;
use Google\Cloud\ErrorReporting\V1beta1\ErrorStatsServiceClient;
use Google\Cloud\ErrorReporting\V1beta1\QueryTimeRange;
Expand All @@ -32,7 +31,6 @@
*/
class DeployTest extends TestCase
{
use TestTrait;
use AppEngineDeploymentTrait;
use EventuallyConsistentTestTrait;

Expand Down
2 changes: 2 additions & 0 deletions sessions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/.idea
8 changes: 8 additions & 0 deletions sessions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Session handling with PHP on Google Cloud Platform

This directory contains the complete sample code for session handling with PHP
using Firestore. Follow the tutorial to run the code:

* [Session handling with PHP][sessions]

[sessions]: http://cloud.google.com/php/getting-started/sessions
1 change: 1 addition & 0 deletions sessions/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runtime: php72
5 changes: 5 additions & 0 deletions sessions/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"google/cloud-firestore": "^1.9"
}
}
63 changes: 63 additions & 0 deletions sessions/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

# [START getting_started_sessions_all]
require_once __DIR__ . '/vendor/autoload.php';

use Google\Cloud\Firestore\FirestoreClient;

$projectId = getenv('GOOGLE_CLOUD_PROJECT');
# [START getting_started_sessions_register_handler]
// Instantiate the Firestore Client for your project ID.
$firestore = new FirestoreClient([
'projectId' => $projectId,
]);

# [START getting_started_sessions_create_handler]
$handler = $firestore->sessionHandler(['gcLimit' => 500]);
# [END getting_started_sessions_create_handler]

// Configure PHP to use the the Firebase session handler.
session_set_save_handler($handler, true);
session_save_path('sessions');
session_start();
# [END getting_started_sessions_register_handler]

# [START getting_started_sessions_front_controller]
$colors = ['red', 'blue', 'green', 'yellow', 'pink'];
/**
* This is an example of a front controller for a flat file PHP site. Using a
* Static list provides security against URL injection by default.
*/
switch (@parse_url($_SERVER['REQUEST_URI'])['path']) {
case '/':
if (!isset($_SESSION['views'])) {
$_SESSION['views'] = 0;
$_SESSION['color'] = $colors[rand(0, 4)];
}
printf(
'<body bgcolor="%s">Views: %s</body>',
$_SESSION['color'],
$_SESSION['views']++
);
break;
default:
http_response_code(404);
exit('Not Found');
}
# [END getting_started_sessions_front_controller]
# [END getting_started_sessions_all]
3 changes: 3 additions & 0 deletions sessions/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; enable the gRPC extension
extension=grpc.so
extension=protobuf.so
28 changes: 28 additions & 0 deletions sessions/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2019 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="PHP Getting Started Sessions Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
55 changes: 55 additions & 0 deletions sessions/test/DeployTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\GettingStarted\Sessions;

use Google\Cloud\TestUtils\AppEngineDeploymentTrait;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;

/**
* Class DeployTest
*/
class DeployTest extends TestCase
{
use AppEngineDeploymentTrait;

public function testIndex()
{
$resp = $this->client->get('/');
$this->assertEquals('200', $resp->getStatusCode());
$this->assertContains('Views: 0', (string) $resp->getBody());

$resp = $this->client->get('/');
$this->assertEquals('200', $resp->getStatusCode());
$this->assertContains('Views: 1', (string) $resp->getBody());
}

/**
* Set up the client.
*
* @before
*/
public function setUpClient()
{
$url = self::$gcloudWrapper->getBaseUrl();
$this->client = new Client([
'base_uri' => $url,
'cookies' => true,
]);
}
}

0 comments on commit b3121b4

Please sign in to comment.