Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Add Commands to be Run for Dev Environment

Tags:

php

symfony

How can I make Symfony2 run certain Commands every time the page loads when it is in the Dev environment?

We use TypeScript and I have build a command which will grab all of my TypeScript and compile it. 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).

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.

like image 453
samanime Avatar asked Jan 18 '26 17:01

samanime


1 Answers

If you are set on using Symfony to compile your TypeScript I would try using an event listener

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

# 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.

Create the class you referenced above and add in your code to compile the TypeScript.

//Class
class TypescriptCompiler extends controller
{
    public function onKernelRequest(GetResponseEvent $event)
    {       
        // compile your TypeScript here
    }
}
like image 137
greg Avatar answered Jan 20 '26 06:01

greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!