Skip to content

Module structure

ProcessEight edited this page Jan 14, 2021 · 2 revisions

Module structure

A module must have, at the very least:

  • module.xml
  • registration.php

A module may also have an optional composer.json file.

The role of registration.php

Upon running bin/magento module:enable Vendor_Module:

#11 registration.php:5, require_once()
#10 NonComposerComponentRegistration.php:29, Magento\NonComposerComponentRegistration\{closure:/var/www/html/m2/research/m23-example-modules/html/app/etc/NonComposerComponentRegistration.php:29-29}()
#9 NonComposerComponentRegistration.php:29, array_map()
#8 NonComposerComponentRegistration.php:29, Magento\NonComposerComponentRegistration\{closure:/var/www/html/m2/research/m23-example-modules/html/app/etc/NonComposerComponentRegistration.php:18-31}()
#7 NonComposerComponentRegistration.php:33, require()
#6 autoload_real.php:73, composerRequirefe24ed7cfcf7e56742d967c8a9b0a516()
#5 autoload_real.php:63, ComposerAutoloaderInitfe24ed7cfcf7e56742d967c8a9b0a516::getLoader()
#4 autoload.php:7, include()
#3 autoload.php:30, require_once()
#2 bootstrap.php:33, require()
#1 magento:14, {main}()

Starting at the bottom with stack frame:

  1. #1, bin/magento is the entry point.
  2. #2 initialises the Magento 2 environment.
  3. #3 registers an autoloader and validates that composer install has been run.
  4. These next three lines all call logic inside composer (and outside of Magento). They are all about loading autoloaders.
  5. This line includes a composer autoloader.
  6. This line includes a composer autoloader.
  7. Back in the Magento core, NonComposerComponentRegistration is a special file which contains a single function (it's not a method because it's not in a class).
  8. This method, called main, globs a list of components which cannot be autoloaded. These components are listed in the file it requires, html/app/etc/registration_globlist.php.
  9. The list of glob()bed files is iterated over with array_map.
  10. For each file in the glob()bed list, Magento 2 includes it with require_once.
  11. This is how the registration.php file is read by Magento 2.

Each registration.php file calls \Magento\Framework\Component\ComponentRegistrar::register, which checks that the component is one of several pre-determined types (defined in \Magento\Framework\Component\ComponentRegistrar::$paths). That property eventually contains a list of all the components (modules, themes, etc) registered by Magento 2.

\Magento\Setup\Console\Command\AbstractModuleManageCommand::execute checks to see if there are any modules registered whose 'Status' has changed.

\Magento\Framework\Module\Status::checkConstraints checks to see if the module depends on any other modules. \Magento\Framework\Module\Status::setIsEnabled enables the changed modules. \Magento\Framework\App\Cache is cleaned. \Magento\Framework\App\Filesystem\DirectoryList::GENERATED_CODE and \Magento\Framework\App\Filesystem\DirectoryList::GENERATED_METADATA folders are cleared. Regeneration of generated files is requested by creating the flag .regenerate in var/

Clone this wiki locally