2

I have a scenario in a plugin where I need to create / check data obtained by reading files. This currently happens on plugin init but isn't completed in time to make use of by other functions.

My plugin needs to parse a few SVG's and stylesheets and create data accordingly. I'am currently checking against a last modified date to avoid extraneous work.

The SVG's and stylesheets are from the website public directory and can be modified by the developer at any time and I would like to avoid any manual interaction within the CP when it comes to parsing these files.

What would be an acceptable way to ensure this data is generated before my plugin attempts to use it?

5
  • 2
    Make sure you're not using the main plugin file's init... that file gets run on pretty much every request. You're better off putting it in the init of a Service file instead.
    – Lindsey D
    Oct 12 '16 at 4:10
  • Thanks for the tip @Lindsey, I'll be sure to move to the service init instead
    – Fyrebase
    Oct 12 '16 at 4:18
  • After some reordering of my logic and moving code to the init method of the plugins service I have been able to solve this problem.
    – Fyrebase
    Oct 12 '16 at 6:21
  • @Fyrebase Can you add what you ended up going with as an official answer?
    – Brad Bell
    Oct 12 '16 at 17:53
  • Done and done @Brad
    – Fyrebase
    Oct 13 '16 at 0:28
3

After some reordering of my logic and moving code to the init method of the plugins service I have been able to solve this problem with the below.

class SvgIconsService extends BaseApplicationComponent
{
    public function init()
    {
        $this->createResources(); // Method call to perform file operations

        parent::init();
    }

    private function createResources()
    {
        $folders = IOHelper::getFolderContents(craft()->config->get('iconSetsPath', 'svgicons'), false);

        if (is_array($folders)) {
            foreach ($folders as $idx => $f)
            {
                $iconSets[IOHelper::getFolderName($f) . IOHelper::getFolderName($f, false)] = IOHelper::getFolderName($f, false);

                // Create sprite sheet resources from Stylesheet
                $stylesheets = IOHelper::getFolderContents($f, false, '\-sprites.css');

                if(!empty($stylesheets)) {
                    foreach ($stylesheets as $stylesheetIdx => $stylesheet) {
                        craft()->templates->includeCss(IOHelper::getFileContents($stylesheet));
                        $this->getSpritesFromStylesheet($stylesheet);
                    }
                }

                // Create sprite sheet resources from SVG
                $svgs = IOHelper::getFolderContents($f, false, '\-sprites.svg');

                if(!empty($svgs)) {
                    foreach ($svgs as $svgIdx => $svg) {
                        $this->getSpritesFromSvg($svg);
                    }
                }
            }
        }
    }
}

I was originally calling craft()->svgIcons->createResources(); from the plugin init method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.