Ejemplo n.º 1
0
 /**
  * 
  * @param Exception|Throwable $exception
  */
 private function reportAndThrow($exception)
 {
     if (headers_sent() === false && DebugR::isEnabled()) {
         if ($exception->getFile()) {
             $file = preg_replace('/^' . preg_quote(dirname(\Sledgehammer\VENDOR_DIR), '/') . '/', '', $exception->getFile());
             $location = "\nin " . $file . ' on line ' . $exception->getLine();
         } else {
             $location = '';
         }
         DebugR::warning(get_class($exception) . "\n" . $exception->getMessage() . $location);
     }
     throw $exception;
 }
Ejemplo n.º 2
0
 /**
  * Send a response based on the request.
  */
 public function handleRequest()
 {
     // Build document
     $document = $this->generateDocument();
     if (!defined('Sledgehammer\\GENERATED')) {
         define('Sledgehammer\\GENERATED', microtime(true));
     }
     // Send headers
     $headers = $document->getHeaders();
     \Sledgehammer\send_headers($headers['http']);
     // Send the sledgehammer-statusbar as DebugR header.
     if (DebugR::isEnabled()) {
         ob_start();
         statusbar();
         DebugR::send('sledgehammer-statusbar', ob_get_clean(), true);
     }
     // Send the contents
     $document->render();
 }
Ejemplo n.º 3
0
 /**
  * Inspect the worpress database and.
  */
 public static function initialize()
 {
     static $initialized = false;
     if ($initialized) {
         return;
     }
     $initialized = true;
     if (function_exists('the_post') === false) {
         throw new Exception('Wordpress is not yet initialized');
     }
     // Lazy database connection
     Connection::$instances['wordpress'] = function () {
         if (defined('DB_USER') === false) {
             throw new Exception('No database configured');
         }
         $connection = new Connection('mysql://' . DB_USER . ':' . DB_PASSWORD . '@' . DB_HOST . '/' . DB_NAME);
         if (empty(Logger::$instances['Database[wordpress]'])) {
             $index = array_search($connection->logger, Logger::$instances);
             unset(Logger::$instances[$index]);
             Logger::$instances['Database[wordpress]'] = $connection->logger;
         }
         return $connection;
     };
     if (empty(Connection::$instances['default'])) {
         Connection::$instances['default'] = 'wordpress';
     }
     // Sledgehammer ORM configuration
     Repository::configureDefault(function ($repo) {
         $repo->registerBackend(new WordpressRepositoryBackend());
     });
     if (defined('Sledgehammer\\WEBPATH') === false) {
         $url = new Url(WP_HOME);
         $path = $url->path === '/' ? '/' : $url->path . '/';
         define('Sledgehammer\\WEBPATH', $path);
         define('Sledgehammer\\WEBROOT', $path);
     }
     define('WEBPATH', \Sledgehammer\WEBPATH);
     if (WP_DEBUG || !empty($_GET[\Sledgehammer\DEBUG_VAR])) {
         add_action('admin_enqueue_scripts', function () {
             wp_enqueue_style('sh-debug', '/../core/css/debug.css');
         });
         add_action('admin_footer', [self::class, 'statusbar']);
         add_action('wp_footer', [self::class, 'statusbar']);
         add_action('send_headers', function () {
             if (DebugR::isEnabled()) {
                 ob_start();
                 echo $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'];
                 \Sledgehammer\statusbar();
                 DebugR::send('sledgehammer-statusbar', ob_get_clean(), true);
             }
         }, PHP_INT_MAX);
         add_filter('template_include', function ($template) {
             if (empty(ErrorHandler::$instances['default'])) {
                 ErrorHandler::enable();
             }
             if (defined('Sledgehammer\\GENERATED') === false) {
                 define('Sledgehammer\\GENERATED', microtime(true));
             }
             return $template;
         }, PHP_INT_MAX);
     }
 }
Ejemplo n.º 4
0
 /**
  * Reports the error/exception to the ErrorHandler and returns the error as Json object.
  * The javascript client should detect and report the error to the user:
  *   if (result.success !== true) { alert(result.error); }.
  *
  * @param string|Exception $error The error message or Exception
  * @param int              $http  The HTTP status code (defaults to 400 Bad Request)
  *
  * @return Json
  */
 public static function error($error, $http = 400)
 {
     if (headers_sent() === false && DebugR::isEnabled()) {
         ErrorHandler::instance()->html = false;
     }
     if ($error instanceof Exception) {
         \Sledgehammer\report_exception($error);
         $error = $error->getMessage();
     } else {
         \Sledgehammer\warning($error);
     }
     return new self(array('success' => false, 'error' => $error), array('http' => array('Status' => $http . ' ' . Framework::$statusCodes[$http], 'Content-Type' => ErrorHandler::instance()->html ? 'text/html;  charset=utf-8' : 'application/json')));
 }