/piecrust.php

https://bitbucket.org/ndj/piecrust · PHP · 145 lines · 104 code · 14 blank · 27 comment · 10 complexity · a201123ac1c27ddfabf194a2cd15f3a3 MD5 · raw file

  1. <?php
  2. /**
  3. * Shows a hard-coded system message.
  4. */
  5. function piecrust_show_system_message($message, $details = null)
  6. {
  7. $contents = file_get_contents(__DIR__ . '/res/messages/' . $message . '.html');
  8. if ($details != null)
  9. {
  10. $contents = str_replace('{{ details }}', $details, $contents);
  11. }
  12. echo $contents;
  13. }
  14. /**
  15. * The PieCrust error handler.
  16. */
  17. function piecrust_error_handler($errno, $errstr, $errfile = null, $errline = 0, $errcontext = null)
  18. {
  19. if (error_reporting() & $errno)
  20. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  21. return;
  22. }
  23. /**
  24. * The PieCrust shutdown function.
  25. */
  26. function piecrust_shutdown_function()
  27. {
  28. $error = error_get_last();
  29. if ($error)
  30. {
  31. try
  32. {
  33. $guard = 100;
  34. while (ob_get_level() > 0 && (--$guard) > 0)
  35. ob_end_clean();
  36. }
  37. catch (Exception $e)
  38. {
  39. }
  40. piecrust_show_system_message('critical', "{$error['message']} in '{$error['file']}:{$error['line']}'");
  41. exit();
  42. }
  43. }
  44. /**
  45. * The Chef shutdown function (command-line version of `piecrust_shutdown_function`).
  46. */
  47. function chef_shutdown_function()
  48. {
  49. $error = error_get_last();
  50. if ($error)
  51. {
  52. try
  53. {
  54. $guard = 100;
  55. while (ob_get_level() > 0 && (--$guard) > 0)
  56. ob_end_clean();
  57. }
  58. catch (Exception $e)
  59. {
  60. }
  61. echo "Critical error in '{$error['file']}:{$error['line']}': {$error['message']}\n";
  62. exit();
  63. }
  64. }
  65. /**
  66. * Sets up basic things like the global error handler or the timezone.
  67. */
  68. function piecrust_setup($profile = 'web')
  69. {
  70. // Add the `libs` directory to the include path for the PEAR stuff
  71. // that still uses old-style includes, and for other stuff like Markdown
  72. // and Smartypants that have conditional include directories (i.e. include
  73. // a different implement of the same class based on runtime conditions).
  74. $srcDir = __DIR__ . '/src';
  75. $libsDir = __DIR__ . '/libs';
  76. set_include_path(
  77. get_include_path() . PATH_SEPARATOR .
  78. $libsDir . '/pear' . PATH_SEPARATOR .
  79. $libsDir);
  80. // Set the autoloader
  81. $loader = (require $libsDir . '/autoload.php');
  82. $loader->add('Console_', $libsDir . '/pear');
  83. $loader->add('Log_', $libsDir . '/pear');
  84. // Set error handling.
  85. switch ($profile)
  86. {
  87. case 'web':
  88. {
  89. set_error_handler('piecrust_error_handler');
  90. register_shutdown_function('piecrust_shutdown_function');
  91. break;
  92. }
  93. case 'chef':
  94. default:
  95. {
  96. ini_set('display_errors', true);
  97. error_reporting(E_ALL);
  98. set_error_handler('piecrust_error_handler');
  99. register_shutdown_function('chef_shutdown_function');
  100. break;
  101. }
  102. case 'test':
  103. {
  104. ini_set('display_errors', true);
  105. ini_set('display_startup_errors', true);
  106. error_reporting(E_ALL | E_STRICT);
  107. set_error_handler('piecrust_error_handler');
  108. break;
  109. }
  110. }
  111. }
  112. /**
  113. * Setups and runs a new PieCrust app with the given parameters, requesting the given URI.
  114. */
  115. function piecrust_run($parameters = array(), $uri = null, $profile = 'web')
  116. {
  117. piecrust_setup($profile);
  118. $parameters = PieCrust\Runner\PieCrustRunner::getPieCrustParameters($parameters);
  119. $pieCrust = new PieCrust\PieCrust($parameters);
  120. $runner = new PieCrust\Runner\PieCrustRunner($pieCrust);
  121. $runner->run($uri);
  122. }
  123. /**
  124. * Setups and runs a new instance of Chef.
  125. */
  126. function piecrust_chef($userArgc = null, $userArgv = null, $profile = 'chef')
  127. {
  128. piecrust_setup($profile);
  129. $chef = new PieCrust\Chef\Chef();
  130. return $chef->run($userArgc, $userArgv);
  131. }