Skip to content

Commit cfe2869

Browse files
authored
Make sure the menu has a database (#37630)
1 parent b738942 commit cfe2869

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

libraries/src/Application/CMSApplication.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Joomla\CMS\Language\Text;
2626
use Joomla\CMS\Log\Log;
2727
use Joomla\CMS\Menu\AbstractMenu;
28+
use Joomla\CMS\Menu\MenuFactoryInterface;
2829
use Joomla\CMS\Pathway\Pathway;
2930
use Joomla\CMS\Plugin\PluginHelper;
3031
use Joomla\CMS\Profiler\Profiler;
@@ -128,6 +129,15 @@ abstract class CMSApplication extends WebApplication implements ContainerAwareIn
128129
*/
129130
protected $authenticationPluginType = 'authentication';
130131

132+
/**
133+
* The menu factory
134+
*
135+
* @var MenuFactoryInterface
136+
*
137+
* @since __DEPLOY_VERSION__
138+
*/
139+
private $menuFactory;
140+
131141
/**
132142
* Class constructor.
133143
*
@@ -515,7 +525,12 @@ public function getMenu($name = null, $options = array())
515525
$options['app'] = $this;
516526
}
517527

518-
return AbstractMenu::getInstance($name, $options);
528+
if ($this->menuFactory === null)
529+
{
530+
$this->menuFactory = $this->getContainer()->get(MenuFactoryInterface::class);
531+
}
532+
533+
return $this->menuFactory->createMenu($name, $options);
519534
}
520535

521536
/**
@@ -1450,4 +1465,18 @@ private function setupLogging(): void
14501465
Log::addLogger(['text_file' => 'custom-logging.php'], $priority, $categories, $mode);
14511466
}
14521467
}
1468+
1469+
/**
1470+
* Sets the internal menu factory.
1471+
*
1472+
* @param MenuFactoryInterface $menuFactory The menu factory
1473+
*
1474+
* @return void
1475+
*
1476+
* @since __DEPLOY_VERSION__
1477+
*/
1478+
public function setMenuFactory(MenuFactoryInterface $menuFactory): void
1479+
{
1480+
$this->menuFactory = $menuFactory;
1481+
}
14531482
}

libraries/src/Menu/MenuFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
\defined('_JEXEC') or die;
1212

1313
use Joomla\CMS\Language\Text;
14+
use Joomla\Database\DatabaseAwareTrait;
1415

1516
/**
1617
* Default factory for creating Menu objects
@@ -19,6 +20,8 @@
1920
*/
2021
class MenuFactory implements MenuFactoryInterface
2122
{
23+
use DatabaseAwareTrait;
24+
2225
/**
2326
* Creates a new Menu object for the requested format.
2427
*
@@ -40,6 +43,11 @@ public function createMenu(string $client, array $options = []): AbstractMenu
4043
throw new \InvalidArgumentException(Text::sprintf('JLIB_APPLICATION_ERROR_MENU_LOAD', $client), 500);
4144
}
4245

46+
if (!array_key_exists('db', $options))
47+
{
48+
$options['db'] = $this->getDatabase();
49+
}
50+
4351
return new $classname($options);
4452
}
4553
}

libraries/src/Menu/SiteMenu.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ public function __construct($options = array())
6363
{
6464
// Extract the internal dependencies before calling the parent constructor since it calls $this->load()
6565
$this->app = isset($options['app']) && $options['app'] instanceof CMSApplication ? $options['app'] : Factory::getApplication();
66-
$this->db = isset($options['db']) && $options['db'] instanceof DatabaseDriver ? $options['db'] : Factory::getDbo();
6766
$this->language = isset($options['language']) && $options['language'] instanceof Language ? $options['language'] : Factory::getLanguage();
6867

68+
if (!isset($options['db']) || !($options['db'] instanceof DatabaseDriver))
69+
{
70+
@trigger_error(sprintf('Database will be mandatory in 5.0.'), E_USER_DEPRECATED);
71+
$options['db'] = Factory::getContainer()->get(DatabaseDriver::class);
72+
}
73+
74+
$this->db = $options['db'];
75+
6976
parent::__construct($options);
7077
}
7178

libraries/src/Service/Provider/Application.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Joomla\CMS\Console\UpdateCoreCommand;
3737
use Joomla\CMS\Factory;
3838
use Joomla\CMS\Language\LanguageFactoryInterface;
39+
use Joomla\CMS\Menu\MenuFactoryInterface;
3940
use Joomla\CMS\User\UserFactoryInterface;
4041
use Joomla\Console\Application as BaseConsoleApplication;
4142
use Joomla\Console\Loader\LoaderInterface;
@@ -82,6 +83,7 @@ function (Container $container)
8283
$app->setLogger($container->get(LoggerInterface::class));
8384
$app->setSession($container->get(SessionInterface::class));
8485
$app->setUserFactory($container->get(UserFactoryInterface::class));
86+
$app->setMenuFactory($container->get(MenuFactoryInterface::class));
8587

8688
return $app;
8789
},
@@ -105,6 +107,7 @@ function (Container $container)
105107
$app->setLogger($container->get(LoggerInterface::class));
106108
$app->setSession($container->get(SessionInterface::class));
107109
$app->setUserFactory($container->get(UserFactoryInterface::class));
110+
$app->setMenuFactory($container->get(MenuFactoryInterface::class));
108111

109112
return $app;
110113
},
@@ -192,6 +195,7 @@ function (Container $container) {
192195
$app->setDispatcher($container->get('Joomla\Event\DispatcherInterface'));
193196
$app->setLogger($container->get(LoggerInterface::class));
194197
$app->setSession($container->get('Joomla\Session\SessionInterface'));
198+
$app->setMenuFactory($container->get(MenuFactoryInterface::class));
195199

196200
return $app;
197201
},

libraries/src/Service/Provider/Menu.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use Joomla\CMS\Menu\MenuFactory;
1414
use Joomla\CMS\Menu\MenuFactoryInterface;
15+
use Joomla\Database\DatabaseInterface;
1516
use Joomla\DI\Container;
1617
use Joomla\DI\ServiceProviderInterface;
1718

@@ -39,7 +40,10 @@ public function register(Container $container)
3940
MenuFactoryInterface::class,
4041
function (Container $container)
4142
{
42-
return new MenuFactory;
43+
$factory = new MenuFactory;
44+
$factory->setDatabase($container->get(DatabaseInterface::class));
45+
46+
return $factory;
4347
},
4448
true
4549
);

0 commit comments

Comments
 (0)