示例#1
0
 public function testExecutableOrder()
 {
     // Create the Tier application
     $app = new TierHTTPApp(new Injector());
     $callCount = 0;
     $fn = function ($stage, $expectedCallCount, $returnValue) use(&$callCount) {
         return function () use(&$callCount, $stage, $expectedCallCount, $returnValue) {
             if ($callCount !== $expectedCallCount) {
                 throw new \Exception("Wrong call count for {$stage}");
             }
             $callCount++;
             return $returnValue;
         };
     };
     $app->addExecutable(10, $fn('Initial', 0, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(20, $fn('BeforeGenerate', 1, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(30, $fn('GenerateBody', 2, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(40, $fn('AfterGenerateBody', 3, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(50, $fn('BeforeGenerate', 4, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(60, $fn('GenerateResponse', 5, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(70, $fn('AfterGenerate', 6, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(80, $fn('BeforeSend', 7, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(90, $fn('Send', 8, TierApp::PROCESS_CONTINUE));
     $app->addExecutable(100, $fn('After', 9, TierApp::PROCESS_END));
     $request = new CLIRequest('/', "example.com");
     $app->execute($request);
 }
示例#2
0
文件: index.php 项目: danack/tier
$injectionParams = (require_once "injectionParams.php");
// Contains helper functions for the application.
require_once "appFunctions.php";
require_once "routes.php";
if (strcasecmp(PHP_SAPI, 'cli') === 0) {
    $request = new CLIRequest('/cleanupException', 'example.com');
} else {
    $request = HTTPFunction::createRequestFromGlobals();
}
// Create the first Tier that needs to be run.
$routingExecutable = new Executable(['Tier\\Bridge\\FastRouter', 'routeRequest'], null, null, 'Room11\\HTTP\\Body');
// Create the Tier application
$injector = new Injector();
/** @var $injectionParams \Tier\InjectionParams */
$injectionParams->addToInjector($injector);
$app = new TierHTTPApp($injector);
// Make the body that is generated be shared by TierApp
$app->addExpectedProduct('Room11\\HTTP\\Body');
define('TIER_ROUTING', 10);
define('TIER_GENERATE_RESPONSE', 10);
define('TIER_BEFORE_SEND', 80);
define('TIER_SEND', 90);
$app->addExecutable(TIER_GENERATE_RESPONSE, $routingExecutable);
$app->addExecutable(TIER_SEND, ['Tier\\HTTPFunction', 'sendBodyResponse']);
try {
    // Run it
    $app->execute($request);
} catch (\Exception $e) {
    $body = new TextBody("Exception: '" . $e->getMessage() . "'", 500);
    HTTPFunction::sendRawBodyResponse($body);
}