719-286-0751 [email protected]

Magento 2 : Automatically Regenerate Interceptors, Factories, and var/generation

This article covers an experimental app we’ve created to deal with the need to delete entries in the var/generation folder. For our article on code generation in Magento 2, click here. 

To view the source code, click here: https://github.com/cadencelabs/m2-regenerate

cadence/m2-regenerate

This application uses grunt to watch the Magento 2 file system for changes, and handles flushing files from the var/generation folder when that happens.

As we mention in our other article, when your Magento 2 application is in developer mode, you still need to delete certain folders when you make changes to your Dependency Injection (DI) configuration, or your DI method signatures. For a full list of when folders need to be cleared, see the notes from Magento.

While clearing the folders does not take as long as setup:di:compile, it’s still annoying to have to consistently do this when you’re making changes to your Magento 2 application. For that reason we created the regenerate task in grunt.

How does it work?

Regenerate works by watching the below files for changes:

  • app/etc/config.php
  • app/etc/di.xml
  • app/code/**/di.xml
  • app/code/**/*.php

It then removes files from the var/generation folder based on this logic:

  1. If config.php or di.xml was changed, delete the entire folder.
  2. If a php file was changed, attempt to clear any Interceptor or Factory class related to that file.

As long as Magento 2 is running in developer mode (and you have deleted all .ser files in var/di) Magento should automatically regenerate these files as needed.

How do I use it?

See the instructions on the github here: https://github.com/cadencelabs/m2-regenerate

Limitations

This module does not attempt to remove generated code related to extension attributes or API repositories.

About Cadence Labs

Cadence Labs is an ecommerce design and development agency located in Boulder, Colorado.

If you would like assistance, or for a free consulation, call (719)-286-0751 or visit our contact page to have a real person contact you today!

Alan Barber is the Lead Web Developer at Cadence Labs and a Magento Certified developer.

1 Comment

Submit a Comment

Your email address will not be published. Required fields are marked *

Install our webapp on your iPhone! Tap and then Add to homescreen.
Share This