|
| 1 | +<?php |
| 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 Google\Cloud\Samples\CloudSQL\MySQL\Votes; |
| 21 | +use Pimple\Container; |
| 22 | +use Pimple\Psr11\Container as Psr11Container; |
| 23 | +use Slim\Factory\AppFactory; |
| 24 | +use Slim\Views\Twig; |
| 25 | +use Slim\Views\TwigMiddleware; |
| 26 | + |
| 27 | +$container = new Container; |
| 28 | +AppFactory::setContainer(new Psr11Container($container)); |
| 29 | +$app = AppFactory::create(); |
| 30 | + |
| 31 | +$container['votes'] = function (Container $container) { |
| 32 | + return new Votes($container['db']); |
| 33 | +}; |
| 34 | + |
| 35 | +$container['db'] = function () { |
| 36 | + $username = getenv("DB_USER"); |
| 37 | + $password = getenv("DB_PASS"); |
| 38 | + $schema = getenv("DB_NAME"); |
| 39 | + $hostname = getenv("DB_HOSTNAME") ?: "127.0.0.1"; |
| 40 | + $cloud_sql_connection_name = getenv("CLOUD_SQL_CONNECTION_NAME"); |
| 41 | + |
| 42 | + try { |
| 43 | + // # [START cloud_sql_mysql_pdo_create] |
| 44 | + // // $username = 'your_db_user'; |
| 45 | + // // $password = 'yoursupersecretpassword'; |
| 46 | + // // $schema = 'your_db_name'; |
| 47 | + // // $cloud_sql_connection_name = getenv("CLOUD_SQL_CONNECTION_NAME"); |
| 48 | + |
| 49 | + if ($cloud_sql_connection_name) { |
| 50 | + // Connect using UNIX sockets |
| 51 | + $dsn = sprintf( |
| 52 | + 'mysql:dbname=%s;unix_socket=/Users/jdp/cloudsql/%s', |
| 53 | + $schema, |
| 54 | + $cloud_sql_connection_name |
| 55 | + ); |
| 56 | + } else { |
| 57 | + // Connect using TCP |
| 58 | + // $hostname = '127.0.0.1'; |
| 59 | + $dsn = sprintf('mysql:dbname=%s;host=%s', $schema, $hostname); |
| 60 | + } |
| 61 | + |
| 62 | + $conn = new PDO($dsn, $username, $password); |
| 63 | + # [END cloud_sql_mysql_pdo_create] |
| 64 | + } catch (PDOException $e) { |
| 65 | + throw new RuntimeException( |
| 66 | + "Could not connect to the Cloud SQL Database. " . |
| 67 | + "Refer to https://cloud.google.com/sql/docs/mysql/connect-admin-proxy " . |
| 68 | + "for more assistance. The PDO error was " . $e->getMessage(), |
| 69 | + $e->getCode(), |
| 70 | + $e |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 75 | + return $conn; |
| 76 | +}; |
| 77 | + |
| 78 | +$container['view'] = function() { |
| 79 | + return Twig::create(__DIR__ . '/../views'); |
| 80 | +}; |
| 81 | + |
| 82 | +$app->add(TwigMiddleware::createFromContainer($app)); |
| 83 | + |
| 84 | +$app->addErrorMiddleware(true, false, false); |
| 85 | + |
| 86 | +return $app; |
0 commit comments