Skip to content

Commit 1b728f5

Browse files
committed
Merge branch 'cloudsql/postgres' of github.com:jdpedrie/php-docs-samples into cloudsql/postgres
2 parents 4019b60 + c741d01 commit 1b728f5

File tree

172 files changed

+5062
-2214
lines changed

Some content is hidden

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

172 files changed

+5062
-2214
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ credentials.*
1010
.php_cs.cache
1111
.vscode/
1212
.kokoro/secrets.sh
13+
.phpunit.result.cache

.kokoro/docker/php73/Dockerfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ RUN ln -s /usr/lib/libc-client.a /usr/lib/x86_64-linux-gnu/libc-client.a && \
9898
php -r "if (hash_file('SHA384', 'composer-setup.php') === rtrim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
9999
php composer-setup.php --filename=composer --install-dir=/usr/local/bin
100100

101+
# Install phpunit globally
102+
RUN composer global require phpunit/phpunit:^7.0
103+
101104
# Install Google Cloud SDK
102105
RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz \
103106
-o ${HOME}/google-cloud-sdk.tar.gz \
@@ -108,7 +111,7 @@ RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz \
108111
--command-completion false
109112

110113
# Make composer and gcloud bins available via the PATH variable
111-
ENV PATH="$PATH:/root/google-cloud-sdk/bin:/root/.composer/vendor/bin"
114+
ENV PATH="$PATH:/root/.composer/vendor/bin:/root/google-cloud-sdk/bin"
112115

113116
# Configure Google Cloud SDK
114117
RUN gcloud config set app/promote_by_default false && \
@@ -117,3 +120,5 @@ RUN gcloud config set app/promote_by_default false && \
117120
gcloud -q components install app-engine-php && \
118121
gcloud -q components update
119122

123+
# Build php-cs-fixer
124+
RUN composer global require friendsofphp/php-cs-fixer

.kokoro/secrets-example.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export REDIS_PORT=
100100
export GOOGLE_PUBSUB_SUBSCRIPTION=php-example-subscription
101101
export GOOGLE_PUBSUB_TOPIC=php-example-topic
102102

103+
# Security Center
104+
export GOOGLE_ORGANIZATION_ID=
105+
export GOOGLE_SECURITYCENTER_PUBSUB_TOPIC=
106+
103107
# Spanner
104108
export GOOGLE_SPANNER_INSTANCE_ID=
105109
export GOOGLE_SPANNER_DATABASE_ID=test-database

.kokoro/secrets.sh.enc

132 Bytes
Binary file not shown.

.kokoro/system_tests.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ export PULL_REQUEST_NUMBER=$KOKORO_GITHUB_PULL_REQUEST_NUMBER
5252

5353
# Run code standards check when appropriate
5454
if [ "${RUN_CS_CHECK}" = "true" ]; then
55-
curl -L https://cs.sensiolabs.org/download/php-cs-fixer-v2.phar -o php-cs-fixer \
56-
&& chmod a+x php-cs-fixer
5755
bash testing/run_cs_check.sh
5856
fi
5957

appengine/flexible/helloworld/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"php": "5.6.*|7.0.*|7.1.*|7.2.*",
3+
"php": "5.6.*|7.0.*|7.1.*|7.2.*|7.3.*",
44
"silex/silex": "^2.3"
55
},
66
"require-dev": {

appengine/flexible/logging/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"require": {
3-
"google/cloud-logging": "^1.16.1",
4-
"google/cloud-error-reporting": "^0.15.0",
3+
"google/cloud-logging": "^1.20.0",
4+
"google/cloud-error-reporting": "^0.16.2",
55
"silex/silex": "^2.0",
66
"twig/twig": "^1.29"
77
},

appengine/flexible/mailjet/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"require": {
33
"silex/silex": "^2.3",
44
"mailjet/mailjet-apiv3-php": "^1.1",
5-
"guzzlehttp/guzzle": "~6.1.0"
5+
"guzzlehttp/guzzle": "~6.5.0"
66
},
77
"require-dev": {
88
"symfony/browser-kit": "^3.0",

appengine/php55/grpc/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"require": {
33
"google/cloud-spanner": "^1.15.0",
4-
"google/cloud-monitoring": "^0.18.0",
4+
"google/cloud-monitoring": "^1.0.0",
55
"google/appengine-php-sdk": "^1.9"
66
}
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require-dev": {
3-
"paragonie/random_compat": "^2.0"
3+
"paragonie/random_compat": "^9.0.0"
44
}
55
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-error-reporting": "^0.15.0"
3+
"google/cloud-error-reporting": "^0.16.2"
44
}
55
}

appengine/php72/grpc/composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"require": {
33
"google/cloud-spanner": "^1.15.0",
4-
"google/cloud-monitoring": "^0.18.0",
5-
"google/cloud-speech": "^0.27.0"
4+
"google/cloud-monitoring": "^1.0.0",
5+
"google/cloud-speech": "^1.0.0"
66
},
77
"require-dev": {
8-
"paragonie/random_compat": "^2.0"
8+
"paragonie/random_compat": "^9.0.0"
99
}
1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require-dev": {
3-
"paragonie/random_compat": "^2.0"
3+
"paragonie/random_compat": "^9.0.0"
44
}
55
}

appengine/php72/wordpress/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"require": {
33
"ext-phar": "*",
44
"ext-zip": "*",
5-
"paragonie/random_compat": "^1.3",
5+
"paragonie/random_compat": "^9.0.0",
66
"google/cloud-tools": "^0.9.0"
77
}
88
}

asset/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"require": {
33
"google/cloud-storage": "^1.9",
4-
"google/cloud-asset": "^0.4.0",
4+
"google/cloud-asset": "^1.0.0",
55
"symfony/console": " ^3.0"
66
},
77
"autoload": {

bigquerydatatransfer/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/cloud-bigquerydatatransfer": "^0.14.0"
3+
"google/cloud-bigquerydatatransfer": "^1.0"
44
}
55
}

bigtable/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-bigtable": "^1.0.0",
3+
"google/cloud-bigtable": "^1.3.1",
44
"psr/cache": "^1.0"
55
},
66
"autoload-dev": {

cloud_sql/mysql/pdo/README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Connection to Cloud SQL - MySQL
2+
3+
## Before you begin
4+
5+
1. Before you use this code sample, you need to have [Composer](https://getcomposer.org/) installed or downloaded into this folder. Download instructions can be found [here](https://getcomposer.org/download/). Once you've installed composer, use it to install required dependencies by running `composer install`.
6+
2. Create a MySQL Cloud SQL Instance by following these [instructions](https://cloud.google.com/sql/docs/mysql/create-instance). Note the connection string, database user, and database password that you create.
7+
3. Create a database for your application by following these [instructions](https://cloud.google.com/sql/docs/mysql/create-manage-databases). Note the database name.
8+
4. Create a service account with the 'Cloud SQL Client' permissions by following these [instructions](https://cloud.google.com/sql/docs/mysql/connect-external-app#4_if_required_by_your_authentication_method_create_a_service_account). Download a JSON key to use to authenticate your connection.
9+
10+
## Running Locally
11+
12+
To run this application locally, download and install the `cloud_sql_proxy` by following the instructions [here](https://cloud.google.com/sql/docs/mysql/sql-proxy#install).
13+
14+
To authenticate with Cloud SQL, set the `$GOOGLE_APPLICATION_CREDENTIALS` environment variable:
15+
16+
```bash
17+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json
18+
```
19+
20+
To run the Cloud SQL proxy, you need to set the instance connection name. See the instructions [here](https://cloud.google.com/sql/docs/mysql/quickstart-proxy-test#get_the_instance_connection_name) for finding the instance connection name.
21+
22+
```bash
23+
export CLOUD_SQL_CONNECTION_NAME='::'
24+
```
25+
26+
Once the proxy is ready, use one of the following commands to start the proxy in the background.
27+
28+
You may connect to your instance via either unix sockets or TCP. To connect using a socket, you must provide the `-dir` option when starting the proxy. To connect via TCP, you must provide a port as part of the instance name. Both are demonstrated below.
29+
30+
### Unix Socket mode
31+
32+
```bash
33+
$ ./cloud_sql_proxy -dir=/cloudsql \
34+
--instances=$CLOUD_SQL_CONNECTION_NAME \
35+
--credential_file=$GOOGLE_APPLICATION_CREDENTIALS
36+
```
37+
38+
Note: Make sure to run the command under a user with write access in the `/cloudsql` directory. This proxy will use this folder to create a unix socket the application will use to connect to Cloud SQL.
39+
40+
### TCP mode
41+
42+
```bash
43+
$ ./cloud_sql_proxy \
44+
--instances=$CLOUD_SQL_CONNECTION_NAME=tcp:3306 \
45+
--credential_file=$GOOGLE_APPLICATION_CREDENTIALS
46+
```
47+
48+
### Set Configuration Values
49+
Set the required environment variables for your connection to Cloud SQL. If you are using TCP mode as described above, do not set the `CLOUD_SQL_CONNECTION_NAME` variable.
50+
51+
```bash
52+
export DB_USER='my-db-user'
53+
export DB_PASS='my-db-pass'
54+
export DB_NAME='my-db-name'
55+
export DB_HOSTNAME='localhost'
56+
```
57+
58+
Note: Saving credentials in environment variables is convenient, but not secure - consider a more secure solution such as [Secret Manager](https://cloud.google.com/secret-manager/) to help keep secrets safe.
59+
60+
Execute the following:
61+
62+
```bash
63+
$ php -S localhost:8080
64+
```
65+
66+
Navigate towards http://localhost:8080 to verify your application is running correctly.
67+
68+
## Google App Engine Flex
69+
70+
To run on App Engine Flex, create an App Engine project by following the setup for these [instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin).
71+
72+
First, update `app.yaml` with the correct values to pass the environment variables into the runtime.
73+
74+
Then, make sure that the service account `service-{PROJECT_NUMBER}>@gae-api-prod.google.com.iam.gserviceaccount.com` has the IAM role `Cloud SQL Client`.
75+
76+
Next, the following command will deploy the application to your Google Cloud project:
77+
78+
```bash
79+
$ gcloud beta app deploy
80+
```
81+
82+
## Google App Engine Standard
83+
84+
To run on GAE-Standard, create an App Engine project by following the setup for these [instructions](https://cloud.google.com/appengine/docs/standard/php7/quickstart#before-you-begin).
85+
86+
First, update `app.yaml` with the correct values to pass the environment variables into the runtime.
87+
88+
Next, the following command will deploy the application to your Google Cloud project:
89+
90+
```bash
91+
$ gcloud app deploy app-standard.yaml
92+
```

cloud_sql/mysql/pdo/app.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
runtime: php
16+
env: flex
17+
18+
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
19+
# something like https://cloud.google.com/secret-manager/ to help keep secrets
20+
# secret.
21+
env_variables:
22+
CLOUD_SQL_CONNECTION_NAME: "::"
23+
DB_USER: my-db-user
24+
DB_PASS: my-db-pass
25+
DB_NAME: my-db
26+
27+
beta_settings:
28+
cloud_sql_instances: "::"
29+
30+
runtime_config:
31+
document_root: .
32+
33+
# Defaults to "serve index.php" and "serve public/index.php". Can be used to
34+
# serve a custom PHP front controller (e.g. "serve backend/index.php") or to
35+
# run a long-running PHP script as a worker process (e.g. "php worker.php").
36+
#
37+
# entrypoint: serve index.php

cloud_sql/mysql/pdo/composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "google/cloud-sql-mysql-example",
3+
"autoload": {
4+
"psr-4": {
5+
"Google\\Cloud\\Samples\\CloudSQL\\MySQL\\": "src"
6+
}
7+
},
8+
"autoload-dev": {
9+
"psr-4": {
10+
"Google\\Cloud\\Samples\\CloudSQL\\MySQL\\Tests\\": "src"
11+
}
12+
},
13+
"require": {
14+
"php": ">= 7.2",
15+
"slim/slim": "^4.5",
16+
"slim/twig-view": "^3.1",
17+
"pimple/pimple": "^3.3",
18+
"guzzlehttp/psr7": "^1.6",
19+
"http-interop/http-factory-guzzle": "^1.0"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^8.5"
23+
}
24+
}

cloud_sql/mysql/pdo/index.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
/*
3+
* Copyright 2020 Google LLC.
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+
declare(strict_types=1);
19+
20+
use GuzzleHttp\Psr7;
21+
22+
include __DIR__ . '/vendor/autoload.php';
23+
24+
$app = include __DIR__ . '/src/app.php';
25+
26+
$app->get('/', function ($request, $response) {
27+
$this->get('votes')->createTableIfNotExists();
28+
29+
return $this->get('view')->render($response, 'template.twig', [
30+
'votes' => $this->get('votes')->listVotes(),
31+
'tabCount' => $this->get('votes')->getCountByValue('TABS'),
32+
'spaceCount' => $this->get('votes')->getCountByValue('SPACES'),
33+
]);
34+
});
35+
36+
$app->post('/', function ($request, $response) {
37+
$this->get('votes')->createTableIfNotExists();
38+
39+
$message = 'Invalid vote. Choose Between TABS and SPACES';
40+
41+
$formData = $request->getParsedBody() + [
42+
'voteValue' => ''
43+
];
44+
45+
if (in_array($formData['voteValue'], ['SPACES', 'TABS'])) {
46+
$message = $this->get('votes')->insertVote($formData['voteValue'])
47+
? 'Vote cast for ' . $formData['voteValue']
48+
: 'An error occurred';
49+
}
50+
51+
return $response->withBody(Psr7\stream_for($message));
52+
});
53+
54+
$app->run();

cloud_sql/mysql/pdo/phpunit.xml.dist

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true">
3+
<testsuites>
4+
<testsuite name="CloudSQLMySQLSample">
5+
<directory>testsdirectory>
6+
testsuite>
7+
testsuites>
8+
<filter>
9+
<whitelist>
10+
<directory suffix=".php">srcdirectory>
11+
whitelist>
12+
filter>
13+
phpunit>

0 commit comments

Comments
 (0)