bangpound/legacy-php-http-kernel

支持控制器退出或返回null的Symfony HttpKernel。

1.1.1 2014-04-08 08:51 UTC

This package is auto-updated.

Last update: 2024-09-12 03:31:59 UTC


README

Build Status

在旧的PHP应用程序中,控制器不返回Symfony响应对象,它们可能返回空或甚至退出。这些低级别的Symfony组件提供了一个简单的桥梁到旧的PHP应用程序。

使用方法

使用 HttpKernel 类并将 ShutdownListenerOutputBufferListener 订阅者添加到 EventDispatcher

工作示例 #1

此组件用于在 Drufony 中运行Drupal 7。

工作示例 #2

  • 下载并展开 WordPress 的最新版本。
  • 在WordPress目录中创建 composer.json
{
	"require": {
		"bangpound/legacy-php-http-kernel": "1.0.*"
	}
}
  • 运行 composer install
  • 替换WordPress的 index.php 前端控制器
<?php
require "vendor/autoload.php";

use Bangpound\LegacyPhp\EventListener\OutputBufferListener;
use Bangpound\LegacyPhp\EventListener\ShutdownListener;
use Bangpound\LegacyPhp\HttpKernel;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

// Use a request attribute to identify requests which are legacy.
$matcher = new RequestMatcher();
$matcher->matchAttribute('_legacy', 'true');

$dispatcher = new EventDispatcher();

// Event listeners make Symfony reponses from output buffer and
// register a shutdown handler.
$dispatcher->addSubscriber(new OutputBufferListener($matcher));
$dispatcher->addSubscriber(new ShutdownListener($matcher));

// Add a listener that modifies every request to have a controller
// that imitates Wordpress index.php.
$dispatcher->addListener(KernelEvents::REQUEST,

    function (GetResponseEvent $event) {
        $request = $event->getRequest();
        $request->attributes->add(array(
            '_legacy' => 'true',
            '_controller' => function () {
                define('WP_USE_THEMES', true);
                require( dirname( __FILE__ ) . '/wp-blog-header.php' );
            },
        ));
    }

);

// This HttpKernel has a shutdown method which completes the kernel
// response cyle when a controller exits or dies.
$kernel = new HttpKernel($dispatcher, new ControllerResolver());

// Symfony front controller code
call_user_func(function () use ($kernel) {
    $request = Request::createFromGlobals();
    $response = $kernel->handle($request);
    $response->send();
    $kernel->terminate($request, $response);
});
  • 运行 php -S localhost:8000