/** * command.post_command * @param sfEvent $event */ public static function listenToCommandPostCommandEvent(sfEvent $event) { if (aToolkitEvents::$once) { return; } aToolkitEvents::$once = true; $task = $event->getSubject(); if ($task->getFullName() === 'project:permissions') { $writable = aFiles::getWritableDataFolder(); $task->getFilesystem()->chmod($writable, 0777); $dirFinder = sfFinder::type('dir'); $fileFinder = sfFinder::type('file'); $task->getFilesystem()->chmod($dirFinder->in($writable), 0777); $task->getFilesystem()->chmod($fileFinder->in($writable), 0666); } if ($task->getFullName() === 'cache:clear') { aAssets::clearAssetCache($task->getFilesystem()); // Clear the page cache on symfony cc if (sfConfig::get('app_a_page_cache_enabled', false)) { echo "Clearing Apostrophe page cache\n"; $cache = aCacheFilter::getCache(); $cache->clean(); } else { // Cache not enabled for this environment. Too many tasks // invoke symfony cc with no environment, so let's not print // anything needlessly worrying here } } }
function _a_get_assets_body($type, $assets) { $gzip = sfConfig::get('app_a_minify_gzip', false); sfConfig::set('symfony.asset.' . $type . '_included', true); $html = ''; // We need our own copy of the trivial case here because we rewrote the asset list // for stylesheets after LESS compilation, and there is no way to // reset the list in the response object if (!sfConfig::get('app_a_minify', false)) { // This branch is seen only for CSS, because javascript calls the original Symfony // functionality when minify is off foreach ($assets as $file => $options) { $html .= stylesheet_tag($file, $options); } return $html; } $sets = array(); foreach ($assets as $file => $options) { if (preg_match('/^http(s)?:/', $file) || isset($options['data-minify']) && $options['data-minify'] === 0) { // Nonlocal URL or minify was explicitly shut off. // Don't get cute with it, otherwise things // like Addthis and ckeditor don't work if ($type === 'stylesheets') { $html .= stylesheet_tag($file, $options); } else { $html .= javascript_include_tag($file, $options); } continue; } /* * * Guts borrowed from stylesheet_tag and javascript_tag. We still do a tag if it's * a conditional stylesheet * */ $absolute = false; if (isset($options['absolute']) && $options['absolute']) { unset($options['absolute']); $absolute = true; } $condition = null; if (isset($options['condition'])) { $condition = $options['condition']; unset($options['condition']); } if (!isset($options['raw_name'])) { if ($type === 'stylesheets') { $file = stylesheet_path($file, $absolute); } else { $file = javascript_path($file, $absolute); } } else { unset($options['raw_name']); } if (is_null($options)) { $options = array(); } if ($type === 'stylesheets') { $options = array_merge(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'screen', 'href' => $file), $options); } else { $options = array_merge(array('type' => 'text/javascript', 'src' => $file), $options); } if (null !== $condition) { $tag = tag('link', $options); $tag = comment_as_conditional($condition, $tag); $html .= $tag . "\n"; } else { unset($options['href'], $options['src']); $optionGroupKey = json_encode($options); $set[$optionGroupKey][] = $file; } // echo($file); // $html .= "<style>\n"; // $html .= file_get_contents(sfConfig::get('sf_web_dir') . '/' . $file); // $html .= "</style>\n"; } // CSS files with the same options grouped together to be loaded together foreach ($set as $optionsJson => $files) { $groupFilename = aAssets::getGroupFilename($files); $groupFilename .= $type === 'stylesheets' ? '.css' : '.js'; if ($gzip) { $groupFilename .= 'gz'; } $dir = aFiles::getUploadFolder(array('asset-cache')); if (!file_exists($dir . '/' . $groupFilename)) { $content = ''; foreach ($files as $file) { $path = null; if (sfConfig::get('app_a_stylesheet_cache_http', false)) { $url = sfContext::getRequest()->getUriPrefix() . $file; $fileContent = file_get_contents($url); } else { $path = sfConfig::get('sf_web_dir') . $file; $fileContent = file_get_contents($path); } if ($type === 'stylesheets') { $options = array(); if (!is_null($path)) { // Rewrite relative URLs in CSS files. // This trick is available only when we don't insist on // pulling our CSS files via http rather than the filesystem // dirname would resolve symbolic links, we don't want that $fdir = preg_replace('/\\/[^\\/]*$/', '', $path); $options['currentDir'] = $fdir; $options['docRoot'] = sfConfig::get('sf_web_dir'); } if (sfConfig::get('app_a_minify', false)) { $fileContent = Minify_CSS::minify($fileContent, $options); } } else { // Trailing carriage return makes behavior more consistent with // JavaScript's behavior when loading separate files. For instance, // a missing trailing semicolon should be tolerated to the same // degree it would be with separate files. The minifier is not // a lint tool and should not surprise you with breakage $fileContent = JSMin::minify($fileContent) . "\n"; } $content .= $fileContent; } if ($gzip) { _gz_file_put_contents($dir . '/' . $groupFilename . '.tmp', $content); } else { file_put_contents($dir . '/' . $groupFilename . '.tmp', $content); } @rename($dir . '/' . $groupFilename . '.tmp', $dir . '/' . $groupFilename); } $options = json_decode($optionsJson, true); // Use stylesheet_path and javascript_path so we can respect relative_root_dir if ($type === 'stylesheets') { $options['href'] = stylesheet_path(sfConfig::get('app_a_assetCacheUrl', '/uploads/asset-cache') . '/' . $groupFilename); $html .= tag('link', $options); } else { $options['src'] = javascript_path(sfConfig::get('app_a_assetCacheUrl', '/uploads/asset-cache') . '/' . $groupFilename); $html .= content_tag('script', '', $options); } } return $html; }
public static function clearAssetCache(sfFilesystem $fileSystem) { $assetDir = aFiles::getUploadFolder(array('asset-cache')); $fileSystem->remove(sfFinder::type('file')->in($assetDir)); $cache = aAssets::getCache(); $cache->clean(); }