We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Where to create a file for custom filter css code in phalcon 4?



77.7k
Accepted
answer

The error message is pretty clear:

Fatal error: Declaration of Frontend\Helpers\CssYUICompressor::filter($contents) must be compatible with Phalcon\Assets\FilterInterface::filter(string $content): string in /home/devmass/web/dev.loc/public_html/apps/frontend/helpers/CssYUICompressor.php on line 12

Taking a look at the definition of FIlterInterface: https://github.com/phalcon/cphalcon/blob/master/phalcon/Assets/FilterInterface.zep

You can see the input parameter MUST be a string, so you should refactor your filter class to match that:

namespace Frontend\Helpers;

use Phalcon\Assets\FilterInterface;

/**
 * Filters CSS content using YUI
 *
 * @param string $contents
 * @return string
 */
class CssYUICompressor implements FilterInterface
{
    protected $options;

    /**
     * CssYUICompressor constructor
     *
     * @param array $options
     */
    public function __construct(array $options)
    {
        $this->options = $options;
    }

    /**
     * @param string $contents
     *
     * @return string
     */
    public function filter(string $contents): string
    {
        // Write the string contents into a temporal file
        file_put_contents('temp/my-temp-1.css', $contents);

        system(
            $this->options['java-bin'] .
            ' -jar ' .
            $this->options['yui'] .
            ' --type css ' .
            'temp/my-temp-file-1.css ' .
            $this->options['extra-options'] .
            ' -o temp/my-temp-file-2.css'
        );

        // Return the contents of file
        return file_get_contents('temp/my-temp-file-2.css');
    }
}

Lajos Bencz, Many thanks! =)

It works now. But I noticed that if you connect several files to minimize, the user filter does not work correctly. The filter is applied as many times as there are files in the input, and not to the overall result after combining the files.

Is there a way to make the same filter work as in Phalcon 3.4 with Cssmin?