diff --git a/libraries/src/Application/CMSApplication.php b/libraries/src/Application/CMSApplication.php index 3cd35d59e9fab..542e06b880790 100644 --- a/libraries/src/Application/CMSApplication.php +++ b/libraries/src/Application/CMSApplication.php @@ -129,6 +129,14 @@ abstract class CMSApplication extends WebApplication implements ContainerAwareIn */ protected $authenticationPluginType = 'authentication'; + /** + * Menu instances container. + * + * @var AbstractMenu[] + * @since __DEPLOY_VERSION__ + */ + protected $menus = []; + /** * The menu factory * @@ -525,12 +533,23 @@ public function getMenu($name = null, $options = array()) $options['app'] = $this; } + if (array_key_exists($name, $this->menus)) + { + return $this->menus[$name]; + } + if ($this->menuFactory === null) { + @trigger_error('Menu factory must be set in 5.0', E_USER_DEPRECATED); $this->menuFactory = $this->getContainer()->get(MenuFactoryInterface::class); } - return $this->menuFactory->createMenu($name, $options); + $this->menus[$name] = $this->menuFactory->createMenu($name, $options); + + // Make sure the abstract menu has the instance too, is needed for BC and will be removed with version 5 + AbstractMenu::$instances[$name] = $this->menus[$name]; + + return $this->menus[$name]; } /** diff --git a/libraries/src/Menu/AbstractMenu.php b/libraries/src/Menu/AbstractMenu.php index 180f5d0135c48..f3a7b2f499986 100644 --- a/libraries/src/Menu/AbstractMenu.php +++ b/libraries/src/Menu/AbstractMenu.php @@ -55,8 +55,10 @@ abstract class AbstractMenu * @var AbstractMenu[] * * @since 1.7 + * + * @deprecated 5.0 Use the MenuFactoryInterface from the container instead */ - protected static $instances = array(); + public static $instances = array(); /** * User object to check access levels for