|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2019 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 | +if (file_exists(__DIR__ . '/../../../vendor/autoload.php')) { |
| 19 | + // Run the command from the git clone. |
| 20 | + require_once __DIR__ . '/../../../vendor/autoload.php'; |
| 21 | +} else if (file_exists(__DIR__ . '/../../../../../../vendor/autoload.php')) { |
| 22 | + // Run the command from the vendor dir installation. |
| 23 | + require_once __DIR__ . '/../../../../../../vendor/autoload.php'; |
| 24 | +} |
| 25 | + |
| 26 | +use Symfony\Component\Console\Application; |
| 27 | +use Symfony\Component\Console\Command\Command; |
| 28 | +use Symfony\Component\Console\Input\InputDefinition; |
| 29 | +use Symfony\Component\Console\Input\InputInterface; |
| 30 | +use Symfony\Component\Console\Input\InputArgument; |
| 31 | +use Symfony\Component\Console\Input\InputOption; |
| 32 | +use Symfony\Component\Console\Output\OutputInterface; |
| 33 | +use Google\Cloud\Utils\WordPress\Project; |
| 34 | + |
| 35 | +$wordPressOptions = new InputDefinition([ |
| 36 | + new InputOption('project_id', null, InputOption::VALUE_REQUIRED, 'Google Cloud project id'), |
| 37 | + new InputOption('db_region', null, InputOption::VALUE_REQUIRED, 'Cloud SQL region'), |
| 38 | + new InputOption('db_instance', null, InputOption::VALUE_REQUIRED, 'Cloud SQL instance id'), |
| 39 | + new InputOption('db_name', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database name'), |
| 40 | + new InputOption('db_user', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database username'), |
| 41 | + new InputOption('db_password', null, InputOption::VALUE_REQUIRED, 'Cloud SQL database password'), |
| 42 | + new InputOption('local_db_user', null, InputOption::VALUE_REQUIRED, 'Local SQL database username'), |
| 43 | + new InputOption('local_db_password', null, InputOption::VALUE_REQUIRED, 'Local SQL database password'), |
| 44 | +]); |
| 45 | + |
| 46 | +$application = new Application('Wordpress Helper'); |
| 47 | +$application->add(new Command('create')) |
| 48 | + ->setDescription('Create a new WordPress site for Google Cloud') |
| 49 | + ->setDefinition(clone $wordPressOptions) |
| 50 | + ->addOption('dir', null, InputOption::VALUE_REQUIRED, 'Directory for the new project', Project::DEFAULT_DIR) |
| 51 | + ->addOption('wordpress_url', null, InputOption::VALUE_REQUIRED, 'URL of the WordPress archive', Project::LATEST_WP) |
| 52 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 53 | + $wordpress = new Project($input, $output); |
| 54 | + // Run the wizard to prompt user for project and database parameters. |
| 55 | + $dir = $wordpress->promptForProjectDir(); |
| 56 | + |
| 57 | + // download wordpress |
| 58 | + $wordpress->downloadWordpress($dir); |
| 59 | + |
| 60 | + // initialize the project and download the plugins |
| 61 | + $wordpress->initializeProject($dir); |
| 62 | + $wordpress->downloadGcsPlugin(); |
| 63 | + |
| 64 | + $dbParams = $wordpress->initializeDatabase(); |
| 65 | + |
| 66 | + // populate random key params |
| 67 | + $params = $dbParams + $wordpress->generateRandomValueParams(); |
| 68 | + |
| 69 | + // copy all the sample files into the project dir and replace parameters |
| 70 | + $wordpress->copyFiles(__DIR__ . '/files', [ |
| 71 | + //'.gcloudignore' => '/', |
| 72 | + 'app.yaml' => '/', |
| 73 | + 'cron.yaml' => '/', |
| 74 | + 'php.ini' => '/', |
| 75 | + 'gae-app.php' => '/', |
| 76 | + 'wp-config.php' => '/', |
| 77 | + ], $params); |
| 78 | + |
| 79 | + $output->writeln("<info>Your WordPress project is ready at $dir</info>"); |
| 80 | + }); |
| 81 | + |
| 82 | +$application->add(new Command('update')) |
| 83 | + ->setDescription('Update an existing WordPress site for Google Cloud') |
| 84 | + ->setDefinition(clone $wordPressOptions) |
| 85 | + ->addArgument('dir', InputArgument::REQUIRED, 'Directory for the existing project') |
| 86 | + ->setCode(function (InputInterface $input, OutputInterface $output) { |
| 87 | + // use the supplied dir for the wordpress project |
| 88 | + $dir = $input->getArgument('dir'); |
| 89 | + |
| 90 | + $wordpress = new Project($input, $output); |
| 91 | + $wordpress->initializeProject($dir); |
| 92 | + |
| 93 | + // Download the plugins if they don't exist |
| 94 | + if (!file_exists($dir . '/wp-content/plugins/gcs')) { |
| 95 | + $wordpress->downloadGcsPlugin(); |
| 96 | + } |
| 97 | + |
| 98 | + $dbParams = $wordpress->initializeDatabase(); |
| 99 | + |
| 100 | + // populate random key params |
| 101 | + $params = $dbParams + $wordpress->generateRandomValueParams(); |
| 102 | + |
| 103 | + // copy all the sample files into the project dir and replace parameters |
| 104 | + $wordpress->copyFiles(__DIR__ . '/files', [ |
| 105 | + //'.gcloudignore' => '/', |
| 106 | + 'app.yaml' => '/', |
| 107 | + 'cron.yaml' => '/', |
| 108 | + 'php.ini' => '/', |
| 109 | + 'gae-app.php' => '/', |
| 110 | + 'wp-config.php' => '/', |
| 111 | + ], $params); |
| 112 | + |
| 113 | + $output->writeln("<info>Your WordPress project has been updated at $dir</info>"); |
| 114 | + }); |
| 115 | + |
| 116 | +if (getenv('PHPUNIT_TESTS') === '1') { |
| 117 | + return $application; |
| 118 | +} |
| 119 | + |
| 120 | +$application->run(); |
0 commit comments