dongsheng8664
dongsheng8664
2016-08-30 11:16

触发Symfony缓存,资产,样式刷新HTTP

已采纳

I am working on a Symfony 2 WebApp. The WebApp has been online for about two years, now I would like to update the design. This work should be outsourced to a 3rd party designer.

The designer should be able to work an a live version of the WebApp (= actually running on my server instead of just plain files) so that design changes become visible instantly when refreshing the browser. Thus the designer needs to be able to change/add files directly on the server and to refresh the cache, assets and styles (using SASS + Compass) when ever needed.

At the same time, the designer should not have general access to neither the WebApp code nor the server itself.

Giving access to the design files only is already solved: I have moved all necessary files/folders from the Symfony installation to a separate folder that is accessible by FTP by the designer. Within the Symfony installation the files/folders have been replaced by symlinks (see my question here).

This works great. Only problem is, currently cache/asset/style refresh can currently only be triggered by direct access via SSH:

$ php app/console cache:clear --env=prod --no-debug
$ php app/console assetic:dump --env=prod --no-debug
$ compass compile --output-style compressed --force

Is it somehow possible to expose these commands via HTTP(S)? Of course the designer will be working on a dedicated Symfony installation. Thus changes will not have any effect on the live version.

Problem is, that app/console... is outside the domain root of the WebApp. Of course I could set another domain to point to app/console... But this way all other files and folders below this dir would be accessible as well.

Additionally I am not sure, if compass compile... can be run from HTTP at all. How can this be done?

I am aware, that refreshing cache and assets is not absolutely necessary when using the dev front controller (.../app_dev.php/...) but without able to refresh / re-compile the Compass and SASS files, style changes will not become visible.

So: Can this be done by HTTP? Maybe using some proxy-script that is called by PHP?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • douhao3562 douhao3562 5年前

    To expose these commands via HTTP(S), you can add for your designer a route calling an action in a controller to run the PHP commands, as explained in the doc:

    // src/AppBundle/Controller/CommandController.php
    namespace AppBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Console\Application;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\Console\Input\ArrayInput;
    use Symfony\Component\Console\Output\NullOutput;
    use Symfony\Component\HttpFoundation\Response;
    
    class CommandController extends Controller
    {
        public function refreshAction()
        {
            $kernel = $this->get('kernel');
            $application = new Application($kernel);
            $application->setAutoExit(false);
    
            $input = new ArrayInput(
                array(
                    'command' => 'cache:clear',
                    '--env' => 'prod',
                ),
                array(
                    'command' => 'assetic:dump',
                    '--env' => 'prod',
                ),
            );
    
            // You don't need the output
            $output = new NullOutput();
            $application->run($input, $output);
    
            $content = "Refreshed";
    
            return new Response($content);
        }
    }
    

    For the compass command, you can use assetic:watch as mentioned in giorgio comment.

    点赞 评论 复制链接分享
  • dpswo40440 dpswo40440 5年前

    I use SVN for that, the repo is on our server, the website is a checkout folder and the designer has also a checkout folder.

    Make a cron that will make a svn update . and a cache:clear

    With that you can grant him access only to the views and maybe controller. You hide parameters and services and entity from him.

    And this is for sure NOT IN PRODUCTION but in integration server

    点赞 评论 复制链接分享
  • doutanggun9816 doutanggun9816 5年前

    I think that it would be best to hide as many things as possible to make it simpler for 3rd party to work with your project. Therefore I'll suggest to handle that automatically behind the scenes.

    Therefore:

    • SASS - I would run Gulp/Grunt watch task server-side that would recompile SASS files on each save. That should work flowlessly. (I'm LESS user, but I guess that works pretty much the same in SASS)

    • cache:clear - I would recommend you to simply turn off the cache on the instace that the 3rd party will work on. It can be done in a few ways. You can set app_dev.php as entry point instead of app.php, but then you should turn off profiler. You can create separate environment if you don't want to change dev one. Also you probably can change prod configuration.

    • assetic:dump - I personally don't like Assetic and don't use it, so I may be wrong in this case. Anyway, as I read here, in dev environment there's no need to dump assets as they are delivered dynamically, so the solution for cache issue should work here too.

    点赞 评论 复制链接分享

相关推荐