Visualizing Magento 2 Module Dependencies

Magento 2 is working on improving the module dependencies problem from Magento 1.  If you look in app/code/*/*/etc/module.xml you will notice there are now dependency declarations in these files.  Knowing full well WORK IN THIS AREA IS NO WHERE NEAR FINISHED and the current files were probably generated automatically by grep commands or similar (so any comment mentioning a module name probably snuck into the dependency file), I still thought it would be kinda fun to plot the dependency graph.

Below is a quick script I knocked up to convert the dependency information into GraphViz input.  I then loaded this with gvedit and rendered the result with DOT.

moduleDependencies

Wow!

Looking for a few moments quickly found lots of dubious dependencies, so I don’t actually believe the diagram above is a correct representation.  There are fewer real dependencies than shown.

It did however raise an interesting question.  Does it matter that there are so many dependencies?  I mean it looks scary, but is that a sign of necessary or unnecessary complexity?   Is the above actually bad?

Yes!

It is bad because it indicates there is a lot of coupling between modules.  This makes it harder to configure or replace a part of an installation.

And No!

There are lots of ways to configure Magento.  The fact that different parts of the system interact is not intrinsically a problem.  Having the full source code available means its not hard to adjust dependencies if it is necessary to change some part of an installation.

Another interesting question: The diagram had 101 modules.  Should some modules be merged, which would reduce the complexity of the graph automatically?  Or should only subsets of the graph be drawn at a time? Any diagram with 101 nodes is going to be confusing.  (If you have an opinion, feel free to comment on this post!)

Also interesting was there are clearly hubs in the diagram. A quick browse identified these as modules like Customer and Sales, which are clearly central and important modules.

Personally I do not read too much into the diagram at this stage of Magento 2 development, but it is an interesting baseline. It will be interesting to see the diagram complexity closer to the final Magento 2 release. With 101 modules, the diagram may never look “nice”. However it will be interesting to see how much shorter the dependency file will become (currently 908 lines long). Every line shorter means one less cross module dependency.

Disclaimer: I work at eBay (parent company of Magento), but posts I put here are my own personal opinions and not necessarily that of my employer.

<?php

# Usage:
# Save this code to a file in a directory that is a sibling of the 'app' directory, or adjust
# the 'glob()' path, so that all the module.xml files are located.

# Find all modules, load up their 'module.xml' files, and work out the dependencies of each module.
# Then output a ".gv" file that graphviz can display which shows all the module dependencies.

/** @var $graphTxt string */
$graphTxt = "digraph graphname {\n";

$numModules = 0;

foreach (glob('../app/code/*/*/etc/module.xml') as $moduleXml) {

    echo "\n" . $moduleXml . "\n";

    $numModules += 1;

    $xml = file_get_contents($moduleXml);
    $dom = new DOMDocument();
    $dom->loadXML($xml);

    /** @var $moduleNode DOMElement */
    $moduleNode = $dom->getElementsByTagName('config')->item(0)->getElementsByTagName('module')->item(0);

    /** @var $moduleName string */
    $moduleName = $moduleNode->getAttribute('name');
    echo "Module: " . $moduleName . "\n";

    /** @var $dependencies DOMNodeList */
    $dependList = $moduleNode->getElementsByTagName('depends');
    if ($dependList->length > 0) {

        $dependencies = $dependList->item(0)->getElementsByTagname('module');

        /** @var $dependency DOMElement */
        foreach ($dependencies as $dependency) {

            /** @var $dependency string */
            $dependsOn = $dependency->getAttribute('name');
            echo "    ".$dependsOn."\n";

            $graphTxt .= "  ".$moduleName." -> ".$dependsOn.";\n";
        }
    }
}
$graphTxt .= "}\n";

echo $numModules." modules\n";

file_put_contents('moduleDependencyGraph.gv', $graphTxt);

3 comments

  1. Reblogged this on Magento Tips and Fundamental Solutions and commented:
    Must Read!! Thanks for sharing!

  2. This is a great post it shows some information and best advice I needs for a blog. Thanks and keep on posting.

  3. As a new developer I have learned a lot through this post about magento 2.0. Thank you for sharing your knowledge with us. It helps me a lot.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.