Magento – Why the merge CSS files not populating

cachecssmagento-1.9server-setup

I have recently migrated a Magento website which had been using merge_css_files successfully.

On the new server, however, I cannot get it to work and would very much appreciate any advice on the matter. The system will create the CSS files in media/css but they are always blank (unpopulated). I suspect that this might be something to do with the server itself as it worked previously on the old server.

I have already toggled Merge CSS on and off, flushed the caches multiple times, deleted the files from the media/css folder, updated all file and folder permissions, checked the ownership in FTP.

Perhaps you have experienced the same or could advise on a possible solution?

Best Answer

Simply put the cms is installed on a shared/cloud hosting server

FWIW, that statement says two different things. You did do a great debugging thing by checking with a vanilla install.

The only thing you can do is debug Mage_Core_Model_Design_Package::_mergeFiles() and/or Mage_Core_Helper_Data::mergeFiles(). I believe the latter of these is where execution is breaking out.

Also, ensure that display_errors and developer mode are enabled (I would just do this in index.php temporarily given the environment issues). Also ensure to check error logs in var/log/. You may want to verify that session & cache files are able to write to the var directory as well.

Edit:

Before you debug, confirm that the site looks as you would expect with merging disabled.

Since you are using FTP you want to keep debug iterations to a minimum. So, you should use Magento's native logging:

Mage::log(__LINE__,null,'merge.log',true);

You'll want to sprinkle this throughout the methods in question. I'd start with the mergeFiles() method from the core helper:

public function mergeFiles(array $srcFiles, $targetFile = false, $mustMerge = false,
    $beforeMergeCallback = null, $extensionsFilter = array())
{
Mage::log(__METHOD__,null,'merge.log',true); //for some context
    try {
        // check whether merger is required
        $shouldMerge = $mustMerge || !$targetFile;
        if (!$shouldMerge) {
Mage::log(__LINE__,null,'merge.log',true);
            if (!file_exists($targetFile)) {
                $shouldMerge = true;
            } else {
                $targetMtime = filemtime($targetFile);
                foreach ($srcFiles as $file) {
                    if (!file_exists($file) || @filemtime($file) > $targetMtime) {
                        $shouldMerge = true;
                        break;
                    }
                }
            }
        }

        // merge contents into the file
        if ($shouldMerge) {
Mage::log(__LINE__,null,'merge.log',true);
            if ($targetFile && !is_writeable(dirname($targetFile))) {
                // no translation intentionally
                throw new Exception(sprintf('Path %s is not writeable.', dirname($targetFile)));
            }

            // filter by extensions
            if ($extensionsFilter) {
                if (!is_array($extensionsFilter)) {
                    $extensionsFilter = array($extensionsFilter);
                }
                if (!empty($srcFiles)){
                    foreach ($srcFiles as $key => $file) {
                        $fileExt = strtolower(pathinfo($file, PATHINFO_EXTENSION));
                        if (!in_array($fileExt, $extensionsFilter)) {
                            unset($srcFiles[$key]);
                        }
                    }
                }
            }
            if (empty($srcFiles)) {
                // no translation intentionally
                throw new Exception('No files to compile.');
            }

            $data = '';
            foreach ($srcFiles as $file) {
                if (!file_exists($file)) {
                    continue;
                }
                $contents = file_get_contents($file) . "\n";
                if ($beforeMergeCallback && is_callable($beforeMergeCallback)) {
                    $contents = call_user_func($beforeMergeCallback, $file, $contents);
                }
                $data .= $contents;
            }
            if (!$data) {
                // no translation intentionally
                throw new Exception(sprintf("No content found in files:\n%s", implode("\n", $srcFiles)));
            }
            if ($targetFile) {
                file_put_contents($targetFile, $data, LOCK_EX);
            } else {
                return $data; // no need to write to file, just return data
            }
        }

        return true; // no need in merger or merged into file successfully
    } catch (Exception $e) {
        Mage::logException($e);
    }
    return false;
}

If you do not see var/log/merge.log being created, first drop that call in at the very end of index.php; if you still don't see it then there is something amiss with your environment and you will need your host to help. If you do see it but it contains no content from the helper, then you need to step back to the _mergeFiles() call in the design package model. Have fun!

Related Topic