Beispiel #1
0
 protected function setUp()
 {
     $this->application = new Application();
     $this->application->unitTesting = true;
     $this->application->context()->simulateNonCli = false;
     $this->application->registerModule(new UnitTestingModule());
     $this->application->initialiseModules();
     ExceptionHandler::disableExceptionTrapping();
 }
<?php

namespace Your\WebApp;

use Rhubarb\Crown\Context;
use Rhubarb\Crown\Exceptions\Handlers\ExceptionHandler;
use Rhubarb\Crown\Logging\Log;
use Rhubarb\Crown\Logging\PhpLog;
use Rhubarb\Stem\StemSettings;
$dbSettings = new StemSettings();
$dbSettings->Host = "localhost";
$dbSettings->Username = "******";
$dbSettings->Password = "";
$dbSettings->Database = "ogredb";
// Add a PHP logger
Log::attachLog(new PhpLog(Log::ALL));
$con = new Context();
$con->DeveloperMode = true;
// Switch off exception trapping. You should have this on in the production environment.
ExceptionHandler::disableExceptionTrapping();
 public function testDisablingTrapping()
 {
     ExceptionHandler::disableExceptionTrapping();
     try {
         // Enable layouts for this test as proof the URL handler has intercepted the response.
         LayoutModule::enableLayout();
         $request = new WebRequest();
         $request->UrlPath = "/test-exception/";
         $response = Module::generateResponseForRequest($request);
         $this->fail("Without exception trapping this line should not be reached.");
     } catch (RhubarbException $er) {
     }
     ExceptionHandler::setExceptionHandlerClassName('\\Rhubarb\\Crown\\Tests\\Exceptions\\Handlers\\UnitTestDisobedientExceptionHandler');
     try {
         // Enable layouts for this test as proof the URL handler has intercepted the response.
         LayoutModule::enableLayout();
         $request = new WebRequest();
         $request->UrlPath = "/test-exception/";
         $response = Module::generateResponseForRequest($request);
     } catch (RhubarbException $er) {
         $this->fail("The extended exception handler should force handling of exceptions even if trapping is disabled.");
     }
     ExceptionHandler::enableExceptionTrapping();
     LayoutModule::disableLayout();
 }
 public function testHeadItems()
 {
     // Reenable this as it was disabled in the previous test.
     ExceptionHandler::enableExceptionTrapping();
     LayoutModule::addHeadItem("this is some html");
     LayoutModule::addHeadItem("this is more html");
     $head = LayoutModule::getHeadItemsAsHtml();
     $this->assertEquals("this is some html\nthis is more html", $head);
 }
Beispiel #5
0
<?php

/**
 * Copyright (c) 2016 RhubarbPHP.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * A bootstrapper to setup the Rhubarb platform when running scripts from a terminal
 */
// Initiate our bootstrap script to boot all libraries required.
require_once __DIR__ . "/boot-application.php";
// Disable exception trapping as there will be no valid URL handler able to return a sensible
// interpretation of the exception details. CLI scripts are never seen publicly so it is more
// useful to have the real exception text and isn't a security risk.
\Rhubarb\Crown\Exceptions\Handlers\ExceptionHandler::disableExceptionTrapping();
if (isset($argv[1])) {
    $script = $argv[1];
    /** @noinspection PhpIncludeInspection */
    include $script;
}
Beispiel #6
0
 public final function __construct()
 {
     global $unitTesting;
     parent::__construct();
     $this->phpContext = new PhpContext();
     // $unitTesting is set in execute-test.php
     $this->unitTesting = isset($unitTesting) && $unitTesting ? true : false;
     $this->developerMode = false;
     $this->live = false;
     $this->applicationRootPath = APPLICATION_ROOT_DIR;
     $this->setAsRunningApplication();
     ExceptionHandler::setProviderClassName(DefaultExceptionHandler::class);
     SessionProvider::setProviderClassName(PhpSessionProvider::class);
     ResourceDeploymentProvider::setProviderClassName(RelocationResourceDeploymentProvider::class);
     $this->registerModule($this);
 }
Beispiel #7
0
 /**
  * Generates the response content for the client.
  *
  * This is normally called by platform/execute-http.php and must be called after all
  * modules have been registered to guarantee the correct output.
  *
  * @static
  * @param Request\Request $request
  * @return string
  */
 public static function generateResponseForRequest(Request\Request $request)
 {
     // Set the current request to be this one.
     $context = new Context();
     $context->Request = $request;
     $additionalData = [];
     if ($request instanceof WebRequest) {
         if (!empty($request->GetData)) {
             $additionalData = $request->GetData;
         }
     }
     Log::createEntry(Log::PERFORMANCE_LEVEL | Log::DEBUG_LEVEL, function () use($request) {
         if ($request instanceof WebRequest) {
             return "Generating response for url " . $request->UrlPath;
         }
         if ($request instanceof CliRequest) {
             return "Starting CLI response";
         }
         return "";
     }, "ROUTER", $additionalData);
     Log::indent();
     $handlers = self::getAllUrlHandlers();
     // an empty-string Response to fall back on if nothing else is generated
     $response = new HtmlResponse();
     $response->SetContent('');
     $filterResponse = true;
     try {
         // Iterate over each handler and ask them to generate a response.
         // If they do return a response we return that and exit the loop.
         // If they return false then we assume they couldn't handle the URL
         // and continue to the next handler.
         foreach ($handlers as $handler) {
             $generatedResponse = $handler->generateResponse($request);
             if ($generatedResponse !== false) {
                 Log::Debug(function () use($handler) {
                     return ["Handler `" . get_class($handler) . "` generated response.", []];
                 }, "ROUTER");
                 // it should be preferred for a handler to return a Response object,
                 // but checking this here retains the option for them to just return
                 // their output
                 if ($generatedResponse instanceof Response) {
                     $response = $generatedResponse;
                 } else {
                     $response->setContent($generatedResponse);
                 }
                 break;
             }
         }
     } catch (ForceResponseException $er) {
         $response = $er->getResponse();
         $filterResponse = false;
     } catch (StopGeneratingResponseException $er) {
         $filterResponse = false;
     } catch (RhubarbException $er) {
         $response = ExceptionHandler::processException($er);
     } catch (\Exception $er) {
         $response = ExceptionHandler::processException(new NonRhubarbException($er));
     }
     if ($filterResponse) {
         Log::createEntry(Log::PERFORMANCE_LEVEL | Log::DEBUG_LEVEL, "Output filters started", "ROUTER");
         Log::indent();
         $filters = self::getAllResponseFilters();
         foreach ($filters as $filter) {
             $response = $filter->processResponse($response);
         }
         Log::createEntry(Log::PERFORMANCE_LEVEL | Log::DEBUG_LEVEL, "Output filters finished", "ROUTER");
         Log::outdent();
     }
     Log::performance("Response generated", "ROUTER");
     Log::outdent();
     return $response;
 }
Beispiel #8
0
use Rhubarb\Crown\Exceptions\Handlers\ExceptionHandler;
use Rhubarb\Crown\Module;
error_reporting(E_ALL | E_STRICT);
// As we preform our own exception handling we need to stop fatal errors from showing stack traces.
ini_set("display_errors", "off");
// Include the composer autoloader
/** @noinspection PhpIncludeInspection */
include "vendor/autoload.php";
// Initially we don't have an auto loader as this is handled by the modules. We need to load this first
// module 'core' so that we have an auto loader for subsequent modules. There are also some other classes
// that might be needed by this booting script so we load them aswell.
include __DIR__ . "/../src/Module.php";
include __DIR__ . "/../src/Exceptions/ImplementationException.php";
include __DIR__ . "/../src/Exceptions/Handlers/ExceptionHandler.php";
// Register to handle exceptions and PHP errors. However we don't do this if we are unit testing. It's
// best to let the exceptions report unhindered to phpunit.
if (!isset($unitTesting) || !$unitTesting) {
    ExceptionHandler::EnableExceptionTrapping();
}
$appName = "app";
// Is there an app environment setting? This allows the same project to serve multiple solutions
// with one code base (e.g. tenant and landlord together). This is very rare in production systems, however
// for the initial project phase this can be very useful.
if ($envAppSetting = getenv("rhubarb_app")) {
    $appName .= "-" . $envAppSetting;
}
if (file_exists("settings/" . $appName . ".config.php")) {
    include "settings/" . $appName . ".config.php";
}
// Now auto loaders are in place we can initialise the modules properly.
Module::InitialiseModules();
 public function testDisablingTrapping()
 {
     ExceptionHandler::disableExceptionTrapping();
     try {
         // Enable layouts for this test as proof the URL handler has intercepted the response.
         LayoutModule::enableLayout();
         $request = new WebRequest();
         $request->urlPath = "/test-exception/";
         $response = $this->application->generateResponseForRequest($request);
         $this->fail("Without exception trapping this line should not be reached.");
     } catch (RhubarbException $er) {
     }
     $this->application->container()->registerClass(ExceptionHandler::class, UnitTestDisobedientExceptionHandler::class);
     try {
         // Enable layouts for this test as proof the URL handler has intercepted the response.
         LayoutModule::enableLayout();
         $request = new WebRequest();
         $request->urlPath = "/test-exception/";
         $response = $this->application->generateResponseForRequest($request);
     } catch (RhubarbException $er) {
         $this->fail("The extended exception handler should force handling of exceptions even if trapping is disabled.");
     }
     ExceptionHandler::enableExceptionTrapping();
     LayoutModule::disableLayout();
 }