Css symfony 2动态更新配色方案

Css symfony 2动态更新配色方案,css,symfony,dynamic,less,assetic,Css,Symfony,Dynamic,Less,Assetic,我在一个使用Symfony 2的项目中工作, 我使用的是带重写和较少过滤器的Assetic,它运行良好, 现在我打算让管理员(连接用户)控制css中的一些功能,如字体和主颜色。 我面临的问题是: -如何将这些css更改从实体集成到css管理中 我不能让assetic使用路由规则来包含自定义css 如果我成功地完成了这项工作,每次我对自定义css进行更改时,我都必须将资产安装到web文件夹,并从控制器中进行资产:转储和清除缓存 如果您(或其他人)仍然需要: 我解决了这个问题,像往常一样将所有通用

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

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

  • 我不能让assetic使用路由规则来包含自定义css
  • 如果我成功地完成了这项工作,每次我对自定义css进行更改时,我都必须将资产安装到web文件夹,并从控制器中进行资产:转储清除缓存
如果您(或其他人)仍然需要:


我解决了这个问题,像往常一样将所有通用CSS放在由Assetic处理的资产中,并将动态CSS生成放在控制器操作中,然后使用Twig呈现CSS。

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

但现在您可能会遇到css的这一部分是对symfony应用程序的完整请求,而不是增加服务器负载的css(HTTP 302等)

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

  • 实现将当前输出缓存到web/additional.css的服务
  • 编写并注册RequestListener以定期更新css
  • 扩展所有可能通过服务调用对css进行更改的控制器操作
示例(假设您使用条令,并且有一个包含一些颜色信息的实体):

服务

<?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);
    }
}
RequestListener

<?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
        }
    }
}
控制器动作

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

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

在这一行
公共函数\uu构造(EntityManager$em,$templating)
没有编译,方法“\uu构造()”的
参数“$templating”没有类型提示,您应该显式配置它的值,
,我找不到类型
#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);
}