Symfony3 Profiler存储

克里斯·韦斯特

在文档中

http://symfony.com/doc/master/cookbook/profiler/storage.html

您仍然可以找到有关Profiler存储的信息。我只是检查了代码,却找不到如何设置自定义存储的任何线索。除了2.8中的原始来源中的一些@legacy注释外,我也没有找到任何说明此事的文档。

是否有理由将其删除?我正在使用redis来存储这些数据,其寿命为eta 1小时。现在,我需要运行手动清理以鞭打该目录中的所有文件。如果有人有任何帮助我解决此问题的线索或提示,请^^

克里斯

克里斯·韦斯特

多亏了Matteo的技巧,我才能够灵活地解决这个问题。Symfony团队删除了此内容,因为它已硬编码到Profiler子系统中。不必通过添加类参数来解决此问题,我必须解决它。:)

好的,这是代码,如果有人需要的话。

首先,我们需要Symfony 2.7中的原始类(至少我重用了它们,因为我只需要Redis选项(我使用它,因为我可以使用igbinary压缩数据))

接下来,您需要实现Compiler Pass。

        namespace AcmeBunlde\DependencyInjection\CompilerPass;

        use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
        use Symfony\Component\DependencyInjection\ContainerBuilder;

        class ProfilerCompilerPass implements CompilerPassInterface
        {

            /**
             * You can modify the container here before it is dumped to PHP code.
             *
             * @param ContainerBuilder $container
             */
            public function process(ContainerBuilder $container)
            {
                $definition = $container->getDefinition('profiler');
                $definition->addArgument('%acmebundle.profiler.defaultEnabled%');
                $definition->addArgument('%acmebundle.profiler.class%');
                $definition->addArgument('%acmebundle.profiler.dsn%');
                $definition->addArgument('%acmebundle.profiler.username%');
                $definition->addArgument('%acmebundle.profiler.password%');
                $definition->addArgument('%acmebundle.profiler.ttl%');
                $definition->setClass('acmebundle\Profiler\Profiler');
            }
        }

这需要在Bundle Loader中加载:

    public function build(ContainerBuilder $container)
    {
        ...
        $container->addCompilerPass(new ProfilerCompilerPass());
    }

此后,我们需要在DependencyInjection文件夹中添加新Profiler存储的配置。

    namespace AcmeBundle\DependencyInjection;

    use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
    use Symfony\Component\Config\Definition\Builder\TreeBuilder;
    use Symfony\Component\Config\Definition\ConfigurationInterface;

    /**
     * This is the class that validates and merges configuration from your app/config files
     *
     * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
     * @author Chris
     */
    class Configuration implements ConfigurationInterface
        /**
         * {@inheritdoc}
         */
        public function getConfigTreeBuilder()
        {
            $treeBuilder = new TreeBuilder();
            $rootNode = $treeBuilder->root('library');

            $rootNode
                ->children()
                    ->arrayNode('profiler')
                        ->addDefaultsIfNotSet()
                        ->children()
                            ->booleanNode('defaultStorage')
                                ->defaultTrue()
                            ->end()
                            ->scalarNode('class')
                                ->defaultValue('')
                            ->end()
                            ->scalarNode('dsn')
                                ->defaultValue('')
                            ->end()
                            ->scalarNode('username')
                                ->defaultValue('')
                            ->end()
                            ->scalarNode('password')
                                ->defaultValue('')
                            ->end()
                            ->scalarNode('ttl')
                                ->defaultValue('3600')
                            ->end()
                        ->end()
                    ->end();
            return $treeBuilder();
        }
    }

现在在“依赖项注入捆绑程序加载器”中设置默认值

    <?php

    namespace AcmeBundle\DependencyInjection;

    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\Config\FileLocator;
    use Symfony\Component\HttpKernel\DependencyInjection\Extension;
    use Symfony\Component\DependencyInjection\Loader;
    use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;

    /**
     * This is the class that loads and manages your bundle configuration
     *
     * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
     * @author Chris 
     */
    class AcmeExtension extends Extension
    {
        /**
         * {@inheritdoc}
         */
        public function load(array $configs, ContainerBuilder $container)
        {

            $configuration = new Configuration();
            $config = $this->processConfiguration($configuration, $configs);
            ...
            $container->setParameter('acmebundle.profiler.defaultEnabled',$config['profiler']['defaultStorage']);
            $container->setParameter('acmebundle.profiler.class',$config['profiler']['class']);
            $container->setParameter('acmebundle.profiler.dsn',$config['profiler']['dsn']);
            $container->setParameter('acmebundle.profiler.username',$config['profiler']['username']);
            $container->setParameter('acmebundle.profiler.password',$config['profiler']['password']);
            $container->setParameter('acmebundle.profiler.ttl',$config['profiler']['ttl']);
            ...
        }
        ...
    }

作为最后一步,您需要构建一个基本容器来添加新的Profiler Handler。我选择实现它不复杂:

    <?php

        namespace AcmeBundle\Profiler;
        use Psr\Log\LoggerInterface;
        use \Symfony\Component\HttpKernel\Profiler\Profiler as ProfilerSrc;
        use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;

        /**
         * Profiler.
         */
        class Profiler extends ProfilerSrc
        {
            public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger, $defaultEnabled=true,$class=null,$dsn=null,$username=null,$password=null,$ttl=3600)
            {
                if($defaultEnabled!==true)
                {
                    $storage = new $class($dsn,$username,$password,$ttl);
                }
                parent::__construct($storage , $logger);
            }
        }

我还添加了一个库来定义存储接口的构造函数。

    <?php

        namespace AcmeBundle\Profiler;

        use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface as ProfilerStorageSource;

        interface ProfilerStorageInterface extends ProfilerStorageSource
        {
            /**
             * ProfilerStorageInterface constructor.
             *
             * @param $dsn
             * @param $username
             * @param $password
             * @param $ttl
             */
            public function __construct($dsn,$username,$password,$ttl);
        }

现在您要做的就是在config_dev.yml文件中定义一些选项。

acmebundle:
    profiler:
        defaultEnabled: false
        class:CLASSNAME INCLUDING NAMESPACE
        dsn: redis://localhost/1
        username: 
        password
        ttl: 3600

如果defaultEnabled = true,则可以重新启用原始处理程序。其余的是,我相信自我解释。用户名+密码来自原始功能集。(ttl ==生命周期)

我希望这对其他人也有帮助:)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章