web.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /* Phinx
  3. *
  4. * (The MIT license)
  5. * Copyright (c) 2014 Rob Morgan
  6. * Copyright (c) 2014 Woody Gilk
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated * documentation files (the "Software"), to
  10. * deal in the Software without restriction, including without limitation the
  11. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  12. * sell copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. // This script can be run as a router with the built in PHP web server:
  27. //
  28. // php -S localhost:8000 app/web.php
  29. //
  30. // Or can be run from any other web server with:
  31. //
  32. // require 'phinx/app/web.php';
  33. //
  34. // This script uses the following query string arguments:
  35. //
  36. // - (string) "e" environment name
  37. // - (string) "t" target version
  38. // - (boolean) "debug" enable debugging?
  39. // Get the phinx console application and inject it into TextWrapper.
  40. $app = require __DIR__ . '/phinx.php';
  41. $wrap = new Phinx\Wrapper\TextWrapper($app);
  42. // Mapping of route names to commands.
  43. $routes = [
  44. 'status' => 'getStatus',
  45. 'migrate' => 'getMigrate',
  46. 'rollback' => 'getRollback',
  47. ];
  48. // Extract the requested command from the URL, default to "status".
  49. $command = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
  50. if (!$command) {
  51. $command = 'status';
  52. }
  53. // Verify that the command exists, or list available commands.
  54. if (!isset($routes[$command])) {
  55. $commands = implode(', ', array_keys($routes));
  56. header('Content-Type: text/plain', true, 404);
  57. die("Command not found! Valid commands are: {$commands}.");
  58. }
  59. // Get the environment and target version parameters.
  60. $env = isset($_GET['e']) ? $_GET['e'] : null;
  61. $target = isset($_GET['t']) ? $_GET['t'] : null;
  62. // Check if debugging is enabled.
  63. $debug = !empty($_GET['debug']) && filter_var($_GET['debug'], FILTER_VALIDATE_BOOLEAN);
  64. // Execute the command and determine if it was successful.
  65. $output = call_user_func([$wrap, $routes[$command]], $env, $target);
  66. $error = $wrap->getExitCode() > 0;
  67. // Finally, display the output of the command.
  68. header('Content-Type: text/plain', true, $error ? 500 : 200);
  69. if ($debug) {
  70. // Show what command was executed based on request parameters.
  71. $args = implode(', ', [var_export($env, true), var_export($target, true)]);
  72. echo "DEBUG: $command($args)" . PHP_EOL . PHP_EOL;
  73. }
  74. echo $output;