0

我正在一个使用 Symfony 2 的项目中工作,我正在使用带有重写和更少过滤器的 Assetic,它工作正常,现在我打算让管理员(连接用户)控制 CSS 中的一些功能,如字体和主颜色. 我面临的问题是:

- 我如何继续将这些 css 更改从实体集成到 css 管理

  • 我不能让资产使用路由规则来包含自定义 css
  • 即使我成功地完成了这项工作,每次我对自定义 css 进行更改时,我都必须将资产安装到 web 文件夹制作资产:转储并从控制器清除缓存。
4

2 回答 2

1

如果您(或其他人)仍然需要这个:

我通过像往常一样将所有通用 CSS 放入 Assetic 处理的资产中并将动态 CSS 生成放入 Controller 操作并使用 Twig 渲染 CSS 来解决这个问题。

于 2014-01-24T18:46:49.710 回答
1

正如 Steffen 所建议的,您应该将动态 CSS 放在 Twig 模板中。

但是现在你可能会因为 css 的那一部分是对 symfony 应用程序的完整请求而不是 css(HTTP 302 等)而受到影响,这会增加服务器负载。

这就是为什么我会建议你做 3 件事(如果你的 css 在没有交互的情况下不会改变,你可以跳过第 2 步,例如基于日期):

  • 实现一个将当前输出缓存到例如 web/additional.css 的服务。
  • 编写并注册一个 RequestListener 以定期更新 css
  • 通过服务调用扩展所有可能对 CSS 进行更改的控制器操作

示例(假设您使用 Doctrine 并且有一个带有一些颜色信息的实体):

服务

<?php 
//Acme\DemoBundle\Service\CSSDeployer.php
namespace Acme\DemoBundle\Service;

use Doctrine\ORM\EntityManager;

class CSSDeployer
{
    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * Twig Templating Service
     */
    protected $templating;

    public function __construct(EntityManager $em, $templating)
    {
        $this->em = $em;
        $this->templating = $templating;
    }

    public function deployStyle($filepath)
    {
        $entity = $this->em->getRepository('AcmeDemoBundle:Color')->findBy(/* your own logic here */);
        if(!$entity) {
            // your error handling
        }

        if(!file_exists($filepath)) {
            // your error handling, be aware of the case where this service is run the first time though
        }

        $content = $this->templating->render('AcmeDemoBundle:CSS:additional.css.twig', array(
            'data' => $entity
        ));

        //Maybe you need to wrap below in a try-catch block
        file_put_contents($filepath, $content);
    }
}

服务注册

#Acme\DemoBundle\Resources\config\services.yml
services:
    #...
    css_deployer:
        class: Acme\DemoBundle\Service\CSSDeployer
        arguments: [ @doctrine.orm.entity_manager, @templating ]

请求监听器

<?php
//Acme\DemoBundle\EventListener\RequestListener.php
namespace Acme\DemoBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Debug\Exception\ContextErrorException;
use \DateTime;
use Doctrine\ORM\EntityManager;

class RequestListener
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var EntityManager
     */
    protected $em;

    public function __construct(ContainerInterface $container, $em)
    {
        $this->container = $container;
        $this->em        = $em;
    }

    /**
     * Checks filemtime (File modification time) of web/additional.css
     * If it is not from today it will be redeployed.
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $kernel     = $event->getKernel();
        $container  = $this->container;

        $path = $container->get('kernel')->getRootDir().'/../web'.'/additional.css';

        $time = 1300000000;

        try {
            $time = @filemtime($path);
        } catch(ContextErrorException $ex) {
            //Ignore
        } catch(\Exception $ex) {
            //will never get here
            if(in_array($container->getParameter("kernel.environment"), array("dev","test"))) {
                throw $ex;
            }
        }

        if($time === FALSE || $time == 1300000000) {
            file_put_contents($path, "/*Leer*/");
            $time = 1300000000;
        }

        $modified   = new \DateTime();
        $modified->setTimestamp($time);
        $today      = new \DateTime();

        if($modified->format("Y-m-d")!= $today->format("Y-m-d")) {
            //UPDATE CSS
            try {                    
                $container->get('css_deployer')->deployStyle($path);
            } catch(\Exception $ex) {
                if(in_array($container->getParameter("kernel.environment"), array("dev","test"))){
                    throw $ex;
                }
            }
        } else {
            //DO NOTHING
        }
    }
}

请求监听器注册

#Acme\DemoBundle\Resources\config\services.yml
acme_style_update_listener.request:
    class: Acme\DemoBundle\EventListener\RequestListener
    arguments: [ @service_container, @doctrine.orm.entity_manager ]
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

控制器动作

public function updateAction()
{
    // Stuff
    $path = '....';
    $this->get('css_deployer')->deployStyle($path);
}

希望这对将来的某人有所帮助。

于 2014-09-09T11:52:59.050 回答