diff --git a/libraries/src/Application/CMSApplication.php b/libraries/src/Application/CMSApplication.php index 8649ac997b537..3cd35d59e9fab 100644 --- a/libraries/src/Application/CMSApplication.php +++ b/libraries/src/Application/CMSApplication.php @@ -25,6 +25,7 @@ use Joomla\CMS\Language\Text; use Joomla\CMS\Log\Log; use Joomla\CMS\Menu\AbstractMenu; +use Joomla\CMS\Menu\MenuFactoryInterface; use Joomla\CMS\Pathway\Pathway; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Profiler\Profiler; @@ -128,6 +129,15 @@ abstract class CMSApplication extends WebApplication implements ContainerAwareIn */ protected $authenticationPluginType = 'authentication'; + /** + * The menu factory + * + * @var MenuFactoryInterface + * + * @since __DEPLOY_VERSION__ + */ + private $menuFactory; + /** * Class constructor. * @@ -515,7 +525,12 @@ public function getMenu($name = null, $options = array()) $options['app'] = $this; } - return AbstractMenu::getInstance($name, $options); + if ($this->menuFactory === null) + { + $this->menuFactory = $this->getContainer()->get(MenuFactoryInterface::class); + } + + return $this->menuFactory->createMenu($name, $options); } /** @@ -1450,4 +1465,18 @@ private function setupLogging(): void Log::addLogger(['text_file' => 'custom-logging.php'], $priority, $categories, $mode); } } + + /** + * Sets the internal menu factory. + * + * @param MenuFactoryInterface $menuFactory The menu factory + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function setMenuFactory(MenuFactoryInterface $menuFactory): void + { + $this->menuFactory = $menuFactory; + } } diff --git a/libraries/src/Menu/MenuFactory.php b/libraries/src/Menu/MenuFactory.php index 22d3fa41c77f2..b1e423184e622 100644 --- a/libraries/src/Menu/MenuFactory.php +++ b/libraries/src/Menu/MenuFactory.php @@ -11,6 +11,7 @@ \defined('_JEXEC') or die; use Joomla\CMS\Language\Text; +use Joomla\Database\DatabaseAwareTrait; /** * Default factory for creating Menu objects @@ -19,6 +20,8 @@ */ class MenuFactory implements MenuFactoryInterface { + use DatabaseAwareTrait; + /** * Creates a new Menu object for the requested format. * @@ -40,6 +43,11 @@ public function createMenu(string $client, array $options = []): AbstractMenu throw new \InvalidArgumentException(Text::sprintf('JLIB_APPLICATION_ERROR_MENU_LOAD', $client), 500); } + if (!array_key_exists('db', $options)) + { + $options['db'] = $this->getDatabase(); + } + return new $classname($options); } } diff --git a/libraries/src/Menu/SiteMenu.php b/libraries/src/Menu/SiteMenu.php index e0a2d7ee52ca5..8ad2c06ccb707 100644 --- a/libraries/src/Menu/SiteMenu.php +++ b/libraries/src/Menu/SiteMenu.php @@ -63,9 +63,16 @@ public function __construct($options = array()) { // Extract the internal dependencies before calling the parent constructor since it calls $this->load() $this->app = isset($options['app']) && $options['app'] instanceof CMSApplication ? $options['app'] : Factory::getApplication(); - $this->db = isset($options['db']) && $options['db'] instanceof DatabaseDriver ? $options['db'] : Factory::getDbo(); $this->language = isset($options['language']) && $options['language'] instanceof Language ? $options['language'] : Factory::getLanguage(); + if (!isset($options['db']) || !($options['db'] instanceof DatabaseDriver)) + { + @trigger_error(sprintf('Database will be mandatory in 5.0.'), E_USER_DEPRECATED); + $options['db'] = Factory::getContainer()->get(DatabaseDriver::class); + } + + $this->db = $options['db']; + parent::__construct($options); } diff --git a/libraries/src/Service/Provider/Application.php b/libraries/src/Service/Provider/Application.php index d9dac4adababa..949462608f504 100644 --- a/libraries/src/Service/Provider/Application.php +++ b/libraries/src/Service/Provider/Application.php @@ -36,6 +36,7 @@ use Joomla\CMS\Console\UpdateCoreCommand; use Joomla\CMS\Factory; use Joomla\CMS\Language\LanguageFactoryInterface; +use Joomla\CMS\Menu\MenuFactoryInterface; use Joomla\CMS\User\UserFactoryInterface; use Joomla\Console\Application as BaseConsoleApplication; use Joomla\Console\Loader\LoaderInterface; @@ -82,6 +83,7 @@ function (Container $container) $app->setLogger($container->get(LoggerInterface::class)); $app->setSession($container->get(SessionInterface::class)); $app->setUserFactory($container->get(UserFactoryInterface::class)); + $app->setMenuFactory($container->get(MenuFactoryInterface::class)); return $app; }, @@ -105,6 +107,7 @@ function (Container $container) $app->setLogger($container->get(LoggerInterface::class)); $app->setSession($container->get(SessionInterface::class)); $app->setUserFactory($container->get(UserFactoryInterface::class)); + $app->setMenuFactory($container->get(MenuFactoryInterface::class)); return $app; }, @@ -192,6 +195,7 @@ function (Container $container) { $app->setDispatcher($container->get('Joomla\Event\DispatcherInterface')); $app->setLogger($container->get(LoggerInterface::class)); $app->setSession($container->get('Joomla\Session\SessionInterface')); + $app->setMenuFactory($container->get(MenuFactoryInterface::class)); return $app; }, diff --git a/libraries/src/Service/Provider/Menu.php b/libraries/src/Service/Provider/Menu.php index a9964c1c35ecc..06d9bc1a64efe 100644 --- a/libraries/src/Service/Provider/Menu.php +++ b/libraries/src/Service/Provider/Menu.php @@ -12,6 +12,7 @@ use Joomla\CMS\Menu\MenuFactory; use Joomla\CMS\Menu\MenuFactoryInterface; +use Joomla\Database\DatabaseInterface; use Joomla\DI\Container; use Joomla\DI\ServiceProviderInterface; @@ -39,7 +40,10 @@ public function register(Container $container) MenuFactoryInterface::class, function (Container $container) { - return new MenuFactory; + $factory = new MenuFactory; + $factory->setDatabase($container->get(DatabaseInterface::class)); + + return $factory; }, true );