Skip to content

Commit 0f250a4

Browse files
author
ace-n
committed
Revert "chore(appengine): delete unused php55 samples"
This reverts commit 8558c81.
1 parent 8558c81 commit 0f250a4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2508
-0
lines changed

appengine/php55/cloudsql/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Cloud SQL & Google App Engine
2+
3+
This sample application demonstrates how to use [Cloud SQL with Google App Engine](https://cloud.google.com/appengine/docs/php/cloud-sql/).
4+
5+
## Setup
6+
7+
Before running this sample:
8+
9+
## Prerequisites
10+
11+
- Install [`composer`](https://getcomposer.org)
12+
- Install dependencies by running:
13+
14+
```sh
15+
composer install
16+
```
17+
18+
## Setup
19+
20+
Before you can run or deploy the sample, you will need to do the following:
21+
22+
1. Create a [First Generation Cloud SQL](https://cloud.google.com/sql/docs/create-instance) instance. You can do this from the [Cloud Console](https://console.developers.google.com) or via the [Cloud SDK](https://cloud.google.com/sdk). To create it via the SDK use the following command:
23+
24+
$ gcloud sql instances create YOUR_INSTANCE_NAME
25+
26+
1. Set the root password on your Cloud SQL instance:
27+
28+
$ gcloud sql instances set-root-password YOUR_INSTANCE_NAME --password YOUR_INSTANCE_ROOT_PASSWORD
29+
30+
1. Update the connection string in `app.yaml` with your configuration values. These values are used when the application is deployed.
31+
32+
## Run locally
33+
34+
To run locally, you can either run your own MySQL server locally and set the connection string in `app.yaml`, or you can [connect to your CloudSQL instance externally](https://cloud.google.com/sql/docs/external#appaccess).
35+
36+
```sh
37+
cd php-docs-samples/appengine/standard/cloudsql
38+
39+
# set local mysql connection parameters
40+
export MYSQL_DSN="mysql:host=127.0.0.1;port=3306;dbname=guestbook"
41+
export MYSQL_USERNAME=root
42+
export MYSQL_PASSWORD=
43+
44+
php -S localhost:8080
45+
```
46+
47+
> be sure the `MYSQL_` environment variables are appropriate for your mysql instance
48+
49+
Now you can view the app running at [http://localhost:8080](http://localhost:8080)
50+
in your browser.
51+
52+
## Deploy to App Engine
53+
54+
**Prerequisites**
55+
56+
- Install the [Google Cloud SDK](https://developers.google.com/cloud/sdk/).
57+
58+
**Deploy with gcloud**
59+
60+
```
61+
gcloud config set project YOUR_PROJECT_ID
62+
gcloud app deploy
63+
gcloud app browse
64+
```
65+
66+
The last command will open `https://{YOUR_PROJECT_ID}.appspot.com/`
67+
in your browser.
68+
69+
## Create the MySQL Tables
70+
71+
Once your application is running, browse to `/create_table` to create the required tables for this example.

appengine/php55/cloudsql/app.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
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+
# [START gae_php_mysql_app]
19+
use Silex\Application;
20+
use Silex\Provider\TwigServiceProvider;
21+
use Symfony\Component\HttpFoundation\Request;
22+
23+
// create the Silex application
24+
$app = new Application();
25+
$app->register(new TwigServiceProvider());
26+
$app['twig.path'] = [ __DIR__ ];
27+
28+
$app->get('/', function () use ($app) {
29+
/** @var PDO $db */
30+
$db = $app['database'];
31+
/** @var Twig_Environment $twig */
32+
$twig = $app['twig'];
33+
34+
// Show existing guestbook entries.
35+
$results = $db->query('SELECT * from entries');
36+
37+
return $twig->render('cloudsql.html.twig', [
38+
'results' => $results,
39+
]);
40+
});
41+
42+
$app->post('/', function (Request $request) use ($app) {
43+
/** @var PDO $db */
44+
$db = $app['database'];
45+
46+
$name = $request->request->get('name');
47+
$content = $request->request->get('content');
48+
49+
if ($name && $content) {
50+
$stmt = $db->prepare('INSERT INTO entries (guestName, content) VALUES (:name, :content)');
51+
$stmt->execute([
52+
':name' => $name,
53+
':content' => $content,
54+
]);
55+
}
56+
57+
return $app->redirect('/');
58+
});
59+
60+
// function to return the PDO instance
61+
$app['database'] = function () use ($app) {
62+
// Connect to CloudSQL from App Engine.
63+
$dsn = getenv('MYSQL_DSN');
64+
$user = getenv('MYSQL_USER');
65+
$password = getenv('MYSQL_PASSWORD');
66+
if (!isset($dsn, $user) || false === $password) {
67+
throw new Exception('Set MYSQL_DSN, MYSQL_USER, and MYSQL_PASSWORD environment variables');
68+
}
69+
70+
$db = new PDO($dsn, $user, $password);
71+
72+
return $db;
73+
};
74+
# [END gae_php_mysql_app]
75+
76+
$app->get('create_tables', function () use ($app) {
77+
/** @var PDO $db */
78+
$db = $app['database'];
79+
// create the tables
80+
$stmt = $db->prepare('CREATE TABLE IF NOT EXISTS entries ('
81+
. 'entryID INT NOT NULL AUTO_INCREMENT, '
82+
. 'guestName VARCHAR(255), '
83+
. 'content VARCHAR(255), '
84+
. 'PRIMARY KEY(entryID))');
85+
$result = $stmt->execute();
86+
87+
if (false === $result) {
88+
return sprintf("Error: %s\n", $stmt->errorInfo()[2]);
89+
} else {
90+
return 'Tables created';
91+
}
92+
});
93+
94+
return $app;

appengine/php55/cloudsql/app.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
runtime: php55
2+
api_version: 1
3+
threadsafe: true
4+
5+
handlers:
6+
- url: /.*
7+
script: index.php
8+
9+
# [START gae_php_mysql_env]
10+
env_variables:
11+
# Replace USER, PASSWORD, DATABASE, and CONNECTION_NAME with the
12+
# values obtained when configuring your Cloud SQL instance.
13+
MYSQL_DSN: mysql:unix_socket=/cloudsql/INSTANCE_CONNECTION_NAME;dbname=DATABASE
14+
MYSQL_USER: USER
15+
MYSQL_PASSWORD: PASSWORD
16+
# [END gae_php_mysql_env]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<body>
3+
{% if results %}
4+
<h2>Guestbook Entriesh2>
5+
6+
{% for row in results %}
7+
<div><strong> {{ row.guestName }} strong> wrote <br> {{ row.content }}div>
8+
{% endfor %}
9+
{% endif %}
10+
11+
<h2>Sign the Guestbookh2>
12+
<form action="/" method="post">
13+
<div>Name: <input name="name" />div>
14+
<div><textarea name="content" rows="3" cols="60">textarea>div>
15+
<div><input type="submit" value="Sign Guestbook">div>
16+
form>
17+
body>
18+
html>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"silex/silex": "^1.3",
4+
"symfony/twig-bridge": "~2.7|3.0.*",
5+
"twig/twig": "~1.8|~2.0"
6+
},
7+
"require-dev": {
8+
"symfony/browser-kit": "^3.0"
9+
}
10+
}

appengine/php55/cloudsql/index.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
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+
// Install composer dependencies with "composer install"
19+
// @see http://getcomposer.org for more information.
20+
require __DIR__ . '/vendor/autoload.php';
21+
22+
$app = require __DIR__ . '/app.php';
23+
24+
// Run the app!
25+
// use "gcloud app deploy" or run "php -S localhost:8080"
26+
// and browse to "index.php"
27+
$app['debug'] = true;
28+
$app->run();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="../../../testing/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="PHP cloudsql test">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="build/logs/clover.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<file>app.phpfile>
29+
<exclude>
30+
<directory>./vendordirectory>
31+
exclude>
32+
whitelist>
33+
filter>
34+
phpunit>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
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+
use Silex\WebTestCase;
18+
19+
class LocalTest extends WebTestCase
20+
{
21+
public function createApplication()
22+
{
23+
$app = require __DIR__ . '/../app.php';
24+
25+
// set some parameters for testing
26+
$app['session.test'] = true;
27+
$app['debug'] = true;
28+
29+
// skip if we are missing required env vars
30+
$dsn = getenv('MYSQL_DSN');
31+
$username = getenv('MYSQL_USERNAME');
32+
$password = getenv('MYSQL_PASSWORD');
33+
if (empty($dsn) || empty($username) || false === $password) {
34+
$this->markTestSkipped('set the MYSQL_DSN, MYSQL_USER and MYSQL_PASSWORD environment variables');
35+
}
36+
37+
// prevent HTML error exceptions
38+
unset($app['exception_handler']);
39+
40+
return $app;
41+
}
42+
43+
public function test_gaePhpMysqlApp_shouldTestHome()
44+
{
45+
$client = $this->createClient();
46+
47+
$crawler = $client->request('GET', '/');
48+
49+
$this->assertTrue($client->getResponse()->isOk());
50+
}
51+
52+
public function testSignGuestbook()
53+
{
54+
$client = $this->createClient();
55+
56+
$time = date('Y-m-d H:i:s');
57+
$crawler = $client->request('POST', '/', [
58+
'name' => 'mr Skeltal',
59+
'content' => sprintf('doot doot (%s)', $time),
60+
]);
61+
62+
$response = $client->getResponse();
63+
$this->assertEquals(302, $response->getStatusCode());
64+
65+
$crawler = $client->followRedirect();
66+
$response = $client->getResponse();
67+
$this->assertEquals(200, $response->getStatusCode());
68+
$this->assertContains($time, $response->getContent());
69+
}
70+
71+
public function testCreateTables()
72+
{
73+
$client = $this->createClient();
74+
75+
$crawler = $client->request('get', '/create_tables');
76+
77+
$this->assertTrue($client->getResponse()->isOk());
78+
}
79+
}

0 commit comments

Comments
 (0)