如果文件未更改,如何让Assetic停止运行过滤器?

时间:2013-04-22 04:18:51

标签: php zend-framework2 assetic

我正在使用Zend Framework 2,我正在使用以下函数与我的Abstract控制器对模块中的所有资源进行分组,然后将它们写入/缩小为缓存文件,然后提供要写入的文件路径页面通过视图助手。

protected function prepareAssets() {

    // Prepare FilterManager with all filters that will be used
    $FilterManager = new FilterManager();
    $FilterManager->set('scss', new ScssphpFilter());
    $FilterManager->set('cssmin', new MinifyCssFilter());
    $FilterManager->set('jsmin', new JSMinFilter());

    $AssetManager = new AssetManager();
    // Prepare the AssetManager with all core assets
    $AssetManager->set('coreCss', new AssetCollection(array(
        new FileAsset('public/vendor/bootstrap/css/bootstrap.min.css'),
        new FileAsset('public/vendor/bootstrap/css/bootstrap-responsive.min.css'),
        new FileAsset('module/application/assets/css/fonts.css'),
        new FileAsset('module/application/assets/css/main.scss'),
    )));
    $AssetManager->set('coreJs', new AssetCollection(array(
        new FileAsset('public/vendor/modernizr/modernizr-2.6.2.js'),
        new FileAsset('public/vendor/respond/respond.min.js'),
        new FileAsset('public/vendor/jquery/jquery-1.9.1.min.js'),
        new FileAsset('public/vendor/bootstrap/js/bootstrap.min.js'),           
    )));

    // Add the controller specific assets
    $controllerCss = new AssetCollection();
    foreach ($this->assets['css'] as $file) {
        $controllerCss->add(new FileAsset($file));
    }
    $AssetManager->set('controllerCss', $controllerCss);
    $controllerJs = new AssetCollection();
    foreach ($this->assets['js'] as $file) {
        $controllerJs->add(new FileAsset($file));
    }
    $AssetManager->set('controllerJs', $controllerJs);

    // Setup factory to create the assets with cachebusting
    $factory = new AssetFactory('public/cache');
    $factory->setAssetManager($AssetManager);
    $factory->setFilterManager($FilterManager);
    //$factory->setDebug(true);
    $factory->addWorker(new CacheBustingWorker(CacheBustingWorker::STRATEGY_CONTENT));

    // Create the assets
    $coreCss = $factory->createAsset(array(
        '@coreCss',         
    ), array(
        'scss',
        '?cssmin',                  
    ));

    $controllerCss = $factory->createAsset(array(
        '@controllerCss',           
    ), array(
        'scss',
        '?cssmin',
    ));

    $coreJs = $factory->createAsset(array(
        '@coreJs',
    ), array(
        '?jsmin',
    ));

    $controllerJs = $factory->createAsset(array(
        '@controllerJs',            
    ), array(
        '?jsmin'
    ));

    // Write the assets to cache
    $writer = new AssetWriter('public/cache');
    $writer->writeAsset($coreCss);
    $writer->writeAsset($controllerCss);
    $writer->writeAsset($coreJs);
    $writer->writeAsset($controllerJs);

    // rename files with proper extensions to be served correctly
    $coreCssPath = $coreCss->getTargetPath(); rename(getcwd().'/public/cache/'.$coreCssPath, getcwd().'/public/cache/'.$coreCssPath.'.css');
    $controllerCssPath = $controllerCss->getTargetPath(); rename(getcwd().'/public/cache/'.$controllerCssPath, getcwd().'/public/cache/'.$controllerCssPath.'.css');
    $coreJsPath = $coreJs->getTargetPath(); rename(getcwd().'/public/cache/'.$coreJsPath, getcwd().'/public/cache/'.$coreJsPath.'.js');
    $controllerJsPath = $controllerJs->getTargetPath(); rename(getcwd().'/public/cache/'.$controllerJsPath, getcwd().'/public/cache/'.$controllerJsPath.'.js');

    // add paths to view model
    $view = $e->getParam('application')->getMvcEvent()->getViewModel();
    $view->coreCssPath = $coreCssPath.'.css';
    $view->controllerCssPath = $controllerCssPath.'.css';
    $view->coreJsPath = $coreJsPath.'.js';
    $view->controllerJsPath = $controllerJsPath.'.js';

}

然而,在运行时,无论原始文件是否已更改,每个页面加载都需要几秒钟,这告诉我每次都通过过滤器处理文件,尽管缓存文件本身没有更新。

我使用CacheBustingWorker是错误的,还是我错过了其他的东西?

0 个答案:

没有答案