lithium.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. /**
  9. * This console front-controller file is the gateway to your application
  10. * through the command line. It is responsible for intercepting requests, and
  11. * handing them off to the `Dispatcher` for processing.
  12. *
  13. * Determine if we're in an application context by moving up the directory tree
  14. * looking for a `config` directory with a `bootstrap.php` file in it. If no
  15. * application context is found, just boot up the core framework.
  16. */
  17. $params = getopt("", array("app::"));
  18. $working = $params ? array_pop($params) : getcwd();
  19. $app = null;
  20. /**
  21. * If we're not running inside an application (i.e. a self-bootstrapping library), bootstrap the
  22. * core automatically with the default settings.
  23. */
  24. $bootstrap = function() use ($working) {
  25. define('LITHIUM_LIBRARY_PATH', dirname(dirname(__DIR__)));
  26. define('LITHIUM_APP_PATH', $working);
  27. if (!include LITHIUM_LIBRARY_PATH . '/lithium/core/Libraries.php') {
  28. $message = "Lithium core could not be found. Check the value of LITHIUM_LIBRARY_PATH in ";
  29. $message .= __FILE__ . ". It should point to the directory containing your ";
  30. $message .= "/libraries directory.";
  31. throw new ErrorException($message);
  32. }
  33. $resources = sys_get_temp_dir();
  34. $templates = $resources . '/tmp/cache/templates/';
  35. if (!is_dir($templates)) {
  36. mkdir($resources . '/tmp/cache/templates/', 0777, true);
  37. }
  38. lithium\core\Libraries::add('lithium');
  39. lithium\core\Libraries::add(basename($working), array(
  40. 'default' => true,
  41. 'path' => $working,
  42. 'resources' => $resources
  43. ));
  44. };
  45. /**
  46. * The following will dispatch the request and exit with the status code as
  47. * provided by the `Response` object returned from `run()`.
  48. *
  49. * The following will instantiate a new `Request` object and pass it off to the
  50. * `Dispatcher` class. By default, the `Request` will automatically aggregate
  51. * all the server / environment settings, and request content (i.e. options and
  52. * arguments passed to the command) information.
  53. *
  54. * The `Request` is then used by the `Dispatcher` (in conjunction with the
  55. * `Router`) to determine the correct command to dispatch to. The response
  56. * information is then encapsulated in a `Response` object, which is returned
  57. * from the command to the `Dispatcher`.
  58. *
  59. * The `Response` object will contain information about the status code which
  60. * is used as the exit code when ending the execution of this script and
  61. * returned to the callee.
  62. *
  63. * @see lithium\console\Request
  64. * @see lithium\console\Response
  65. * @see lithium\console\Dispatcher
  66. * @see lithium\console\Router
  67. */
  68. $run = function() {
  69. return lithium\console\Dispatcher::run(new lithium\console\Request())->status;
  70. };
  71. /**
  72. * Look to see if there's a bootstrap file. If there is, this is either a Lithium application or
  73. * plugin.
  74. */
  75. if (file_exists("{$working}/config/bootstrap.php")) {
  76. $app = $working;
  77. } elseif (file_exists("{$working}/app/config/bootstrap.php")) {
  78. $app = "{$working}/app";
  79. }
  80. /**
  81. * Attempt to bootstrap the application and execute the request. On failure, use the default
  82. * bootstrap.
  83. */
  84. if ($app) {
  85. foreach (array("bootstrap.php", "bootstrap/libraries.php") as $file) {
  86. if (!file_exists($path = "{$app}/config/{$file}")) {
  87. continue;
  88. }
  89. if (preg_match("/^define\([\"']LITHIUM_LIBRARY_PATH[\"']/m", file_get_contents($path))) {
  90. include "{$app}/config/bootstrap.php";
  91. exit($run());
  92. }
  93. }
  94. }
  95. /**
  96. * We're not running inside a Lithium application. Use the default bootstrap and execute the
  97. * request.
  98. */
  99. $bootstrap();
  100. $app ? include "{$app}/config/bootstrap.php" : null;
  101. exit($run());
  102. ?>