简体   繁体   English

Symfony2添加要为Dev Environment运行的命令

[英]Symfony2 Add Commands to be Run for Dev Environment

How can I make Symfony2 run certain Commands every time the page loads when it is in the Dev environment? 如果在Dev环境中每次加载页面时,如何让Symfony2运行某些命令?

We use TypeScript and I have build a command which will grab all of my TypeScript and compile it. 我们使用TypeScript,我已经构建了一个命令,它将获取我的所有TypeScript并进行编译。 The only problem is, I have to do this manually every time I upload some new code, which slows things down when debugging (I'm perfectly fine doing it this way for Production though). 唯一的问题是,每次上传一些新代码时我都必须手动执行此操作,这会在调试时减慢速度(尽管这样做对于Production来说完全没问题)。

I'd like it to call that Command every page load (if some of the files have changed since it last compiled... all logic I'll built out). 我希望它在每个页面加载时调用该命令(如果自上次编译以来某些文件已经改变了......我将构建所有逻辑)。 I just need to know where I should hook in to. 我只需要知道我应该在哪里接触。

Thanks. 谢谢。

If you are set on using Symfony to compile your TypeScript I would try using an event listener 如果您开始使用Symfony来编译TypeScript,我会尝试使用事件监听器

You could try something like this (untested): 你可以尝试这样的事情(未经测试):

Add a service to the config_dev.yml file so that the listener only runs in the dev environment 将服务添加到config_dev.yml文件,以便侦听器仅在dev环境中运行

# app/config/config_dev.yml 
services:        
      compileTypeScript:
        class: myApp\TypescriptBundle\Controller\TypescriptComiler
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 64 }

I'm not exactly sure how priority works, but 64 seemed to hit the sweet spot for me. 我不确定优先级是如何工作的,但64似乎对我来说是个好消息。

Create the class you referenced above and add in your code to compile the TypeScript. 创建上面引用的类,并添加代码以编译TypeScript。

//Class
class TypescriptCompiler extends controller
{
    public function onKernelRequest(GetResponseEvent $event)
    {       
        // compile your TypeScript here
    }
}

Rather than use Symfony to compile every time (slow), you could setup a grunt.js watch task to watch the file then compile every time there is a change. 您可以设置grunt.js 监视任务来观察文件,然后在每次更改时编译,而不是每次都使用Symfony进行编译(慢速)。 To do this you would have to be comfortable with setting up node.js on your development system. 要做到这一点,您必须熟悉在开发系统上设置node.js. All in all, I find it is kind of nice to completely decouple the client and the server. 总而言之,我发现完全解耦客户端和服务器是一件好事。

We are looking at triggering grunt when we do a prod deployment - to perform the optimisations. 当我们进行prod部署时,我们正在考虑触发grunt - 执行优化。

Dev uses the raw files and client side implementations. Dev使用原始文件和客户端实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM