as3/post-process-bundle

提供在Symfony框架终止前执行可调用代码的集中支持。

安装次数: 12,697

依赖者: 0

建议者: 0

安全: 0

星标: 1

关注者: 4

分支: 1

开放问题: 0

类型:symfony-bundle

1.0.8 2017-03-16 15:26 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:35:24 UTC


README

Scrutinizer Code Quality Build Status Packagist SensioLabsInsight

提供在Symfony框架终止前执行可调用代码的集中支持

安装

使用Composer安装包

要使用composer安装此包,请执行以下命令:composer require as3/post-process-bundle ^1.0

注册包

安装完成后,在您的AppKernel.php中注册此包。

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new As3\Bundle\PostProcessBundle\As3PostProcessBundle(),
    );

    // ...
}

使用方法

要使用PostProcessBundle,您必须首先创建一个符合Task\TaskInterfacePlugins\PluginInterface的类。任务是在发送响应后执行的过程(在响应发送后),而插件是在发送响应之前运行的过程(允许您修改它。)

任务

任务可以在向用户发送响应后执行逻辑,允许您触发需要完成但用户不需要等待的长运行过程。

示例

use As3\Bundle\PostProcessBundle\TaskInterface;

class SleepTestTask implements TaskInterface
{
    /**
     * {@inhericDoc}
     */
    public function run()
    {
        // Some process that takes 5 minutes
        sleep(300);
    }
}

要注册您的任务,请调用任务管理器服务的addTask方法(as3_post_process.task.manager

    $manager = $this->get('as3_post_process.task.manager');
    $manager->addTask(new SleepTestTask(), 5);

当任务添加到管理器时,可以为它们设置priority(优先级)-- 默认情况下,新任务添加的优先级为0。任务按其优先级升序执行。

如果您的任务应该在每次请求上运行,您也可以使用标签as3_post_process.task注册服务。

# src\MyBundle\Resources\services.yml
services:
my_app.my_cool_task:
    class: MyCoolTask
    tags:
        - { name: as3_post_process.task, priority: 5 }

插件

插件可以用来在将响应返回给用户之前修改它。

示例

use Symfony\Component\HttpFoundation\Response;

/**
 * Integration with New Relic End User Monitoring services
 */
class NewRelicInjector extends PluginInterface
{
    /**
     * Handles injection of NREUM Javascript
     */
    public function filterResponse(Response $response)
    {
        if (extension_loaded('newrelic')) {
            newrelic_disable_autorum();

            $content = $response->getContent();

            if (false != strpos($content, '</head>')) {
                $content = str_replace('</head>', sprintf("\n%s\n</head>", newrelic_get_browser_timing_header()), $content);
            }

            if (false != strpos($content, '</body>')) {
                $content = str_replace('</body>', sprintf("\n%s\n</body>", newrelic_get_browser_timing_footer()), $content);
            }

            $response->headers->set('X-NREUM', 'Enabled');

            // If we modified the content, set it on the response.
            if ($content !== $response->getContent()) {
                $response->setContent($content);
            }

            return $response;
        }
    }
}

此插件将禁用NewRelic终端用户监控javascript的自动注入。要为所有请求启用此功能,请添加以下服务定义

    my_app.my_bundle.new_relic_injector:
        class: MyApp\MyBundle\NewRelicPlugin
        tags:
            - { name: as3_post_process.plugin }