|
| 1 | +<?php |
| 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; |
0 commit comments