View file vendor/robmorgan/phinx/app/web.php

File size: 3.02Kb
  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.  
  27. // This script can be run as a router with the built in PHP web server:
  28. //
  29. // php -S localhost:8000 app/web.php
  30. //
  31. // Or can be run from any other web server with:
  32. //
  33. // require 'phinx/app/web.php';
  34. //
  35. // This script uses the following query string arguments:
  36. //
  37. // - (string) "e" environment name
  38. // - (string) "t" target version
  39. // - (boolean) "debug" enable debugging?
  40.  
  41. // Get the phinx console application and inject it into TextWrapper.
  42. $app = require __DIR__ . '/phinx.php';
  43. $wrap = new Phinx\Wrapper\TextWrapper($app);
  44.  
  45. // Mapping of route names to commands.
  46. $routes = [
  47. 'status' => 'getStatus',
  48. 'migrate' => 'getMigrate',
  49. 'rollback' => 'getRollback',
  50. ];
  51.  
  52. // Extract the requested command from the URL, default to "status".
  53. $command = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
  54. if (!$command) {
  55. $command = 'status';
  56. }
  57.  
  58. // Verify that the command exists, or list available commands.
  59. if (!isset($routes[$command])) {
  60. $commands = implode(', ', array_keys($routes));
  61. header('Content-Type: text/plain', true, 404);
  62. die("Command not found! Valid commands are: {$commands}.");
  63. }
  64.  
  65. // Get the environment and target version parameters.
  66. $env = isset($_GET['e']) ? $_GET['e'] : null;
  67. $target = isset($_GET['t']) ? $_GET['t'] : null;
  68.  
  69. // Check if debugging is enabled.
  70. $debug = !empty($_GET['debug']) && filter_var($_GET['debug'], FILTER_VALIDATE_BOOLEAN);
  71.  
  72. // Execute the command and determine if it was successful.
  73. $output = call_user_func([$wrap, $routes[$command]], $env, $target);
  74. $error = $wrap->getExitCode() > 0;
  75.  
  76. // Finally, display the output of the command.
  77. header('Content-Type: text/plain', true, $error ? 500 : 200);
  78. if ($debug) {
  79. // Show what command was executed based on request parameters.
  80. $args = implode(', ', [var_export($env, true), var_export($target, true)]);
  81. echo "DEBUG: $command($args)" . PHP_EOL . PHP_EOL;
  82. }
  83. echo $output;