<p>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.</p>

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

        parent::init();
    }

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

        if (is_array($folders)) {
            foreach ($folders as $idx =&gt; $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 =&gt; $stylesheet) {
                        craft()-&gt;templates-&gt;includeCss(IOHelper::getFileContents($stylesheet));
                        $this-&gt;getSpritesFromStylesheet($stylesheet);
                    }
                }

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

                if(!empty($svgs)) {
                    foreach ($svgs as $svgIdx =&gt; $svg) {
                        $this-&gt;getSpritesFromSvg($svg);
                    }
                }
            }
        }
    }
}
</code></pre>

<p>I was originally calling <code>craft()-&gt;svgIcons-&gt;createResources();</code> from the plugin <code>init</code> method.</p>

CC BY-SA 3.0
2016-10-12T06:24:07.723Z
2016-10-13T00:24:54.837Z
2016-10-13T00:24:54.837Z
Fyrebase
<p>I have a scenario in a plugin where I need to create / check data obtained by reading files. This currently happens on plugin <code>init</code> but isn't completed in time to make use of by other functions.</p>

<p>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.</p>

<p>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.</p>

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

CC BY-SA 3.0
2016-10-12T04:10:24.170Z
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
CC BY-SA 3.0
2016-10-12T04:18:38.100Z
Thanks for the tip @Lindsey, I'll be sure to move to the service init instead
Fyrebase
CC BY-SA 3.0
2016-10-12T06:21:08.683Z
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
CC BY-SA 3.0
2016-10-12T17:53:55.587Z
@Fyrebase Can you add what you ended up going with as an official answer?
Brad Bell
CC BY-SA 3.0
2016-10-13T00:28:48.940Z
Done and done @Brad
Fyrebase
<plugin-development>
Parse files on plugin init
# Parse files on plugin init

(Asked by: username_0 on 2016-10-12)

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?

**Tags:** plugin-development

> **Comments:**
> **username_1**: 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.  
> **username_0**: Thanks for the tip @Lindsey, I'll be sure to move to the service init instead  
> **username_0**: 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.  
> **username_2**: @username_0 Can you add what you ended up going with as an official answer?  
> **username_0**: Done and done @Brad  

---

## 1 answers

### Answer by username_0 on 2016-10-12. Score: 3 | Accepted answer

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.


---

