Skip to content

Instantly share code, notes, and snippets.

@davidalger
Created April 3, 2020 13:56
Show Gist options
  • Save davidalger/9b46df9900fc2bb4089685bf113a871c to your computer and use it in GitHub Desktop.
Save davidalger/9b46df9900fc2bb4089685bf113a871c to your computer and use it in GitHub Desktop.
Magento 2 OM Debugging

Sometimes console commands use DI in a way that instantiates objects which have side-effects or do a lot of work to initialize themselves in the __construct method. This generally will surface in one of two ways:

a) Errors when running bin/magento due to application scope not being set

b) Inability to run setup:install from-scratch due to a module instantiating an object which relies on tables which do not exist yet creating errors such as the following:

Table 'magento_integration_tests.flag' doesn't exist

Appling this patch will cause Magento to output a list of all shared instances instantiated by the ObjectManager during the DI process. This can be useful in debugging the above issues. Inspect the output for Command classes, and then trace down what is instantiated following the last Command class, and you may find the class that is at fault. Once you have this information, a bit of trial and error may be used such as disabling modules or appending \Proxy to injected classes (remember to flush cache and clean the generated directory) to defer their loading.

Usually adding \Proxy permanently to the classes that are injected is sufficient to solve these errors. This is a wrapper class which will be injected by the Object Manager, only instantiating the class that's needed when it is first used. More info on those here: https://devdocs.magento.com/guides/v2.3/extension-dev-guide/proxies.html

--- vendor/magento/framework/ObjectManager/ObjectManager.php 2020-04-03 08:39:25.000000000 -0500
+++ vendor/magento/framework/ObjectManager/ObjectManager.php 2020-04-03 08:47:53.000000000 -0500
@@ -67,6 +67,7 @@
$type = ltrim($type, '\\');
$type = $this->_config->getPreference($type);
if (!isset($this->_sharedInstances[$type])) {
+ echo "At fault : $type\n";
$this->_sharedInstances[$type] = $this->_factory->create($type);
}
return $this->_sharedInstances[$type];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment