Skip to content

Commit

Permalink
AdminController: Use dependency injection (pimcore#13881)
Browse files Browse the repository at this point in the history
  • Loading branch information
blankse authored Jan 2, 2023
1 parent 9350ff8 commit f14d52d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 24 deletions.
4 changes: 2 additions & 2 deletions bundles/AdminBundle/Controller/Admin/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ protected function addRuntimePerspective(array &$templateParams, User $user)
*/
protected function addPluginAssets(array &$templateParams)
{
$templateParams['pluginJsPaths'] = $this->getBundleManager()->getJsPaths();
$templateParams['pluginCssPaths'] = $this->getBundleManager()->getCssPaths();
$templateParams['pluginJsPaths'] = $this->bundleManager->getJsPaths();
$templateParams['pluginCssPaths'] = $this->bundleManager->getCssPaths();

return $this;
}
Expand Down
6 changes: 3 additions & 3 deletions bundles/AdminBundle/Controller/Admin/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public function onKernelControllerEvent(ControllerEvent $event)
}
}

if ($this->getTranslator() instanceof LocaleAwareInterface) {
$this->getTranslator()->setLocale($locale);
if ($this->translator instanceof LocaleAwareInterface) {
$this->translator->setLocale($locale);
}
}

Expand Down Expand Up @@ -274,7 +274,7 @@ protected function buildLoginPageViewParams(Config $config): array
{
return [
'config' => $config,
'pluginCssPaths' => $this->getBundleManager()->getCssPaths(),
'pluginCssPaths' => $this->bundleManager->getCssPaths(),
];
}

Expand Down
64 changes: 58 additions & 6 deletions bundles/AdminBundle/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Translation\TranslatorInterface;

abstract class AdminController extends Controller implements AdminControllerInterface
Expand All @@ -46,6 +47,24 @@ abstract class AdminController extends Controller implements AdminControllerInte
*/
protected $bundleManager;

#[Required]
public function setTranslator(TranslatorInterface $translator): void
{
$this->translator = $translator;
}

#[Required]
public function setBundleManager(PimcoreBundleManager $bundleManager): void
{
$this->bundleManager = $bundleManager;
}

#[Required]
public function setTokenResolver(TokenStorageUserResolver $tokenResolver): void
{
$this->tokenResolver = $tokenResolver;
}

/**
* @return string[]
*/
Expand Down Expand Up @@ -76,19 +95,52 @@ public function needsStorageDoubleAuthenticationCheck()
return true;
}

/**
* @deprecated
*
* @return TranslatorInterface
*/
public function getTranslator()
{
return $this->container->get('translator');
trigger_deprecation(
'pimcore/pimcore',
'10.6',
sprintf('%s is deprecated, please use $this->translator instead. Will be removed in Pimcore 11', __METHOD__)
);

return $this->translator;
}

/**
* @deprecated
*
* @return PimcoreBundleManager
*/
public function getBundleManager()
{
return $this->container->get(PimcoreBundleManager::class);
trigger_deprecation(
'pimcore/pimcore',
'10.6',
sprintf('%s is deprecated, please use $this->bundleManager instead. Will be removed in Pimcore 11', __METHOD__)
);

return $this->bundleManager;
}

/**
* @deprecated
*
* @return TokenStorageUserResolver
*/
public function getTokenResolver()
{
return $this->container->get(TokenStorageUserResolver::class);
trigger_deprecation(
'pimcore/pimcore',
'10.6',
sprintf('%s is deprecated, please use $this->tokenResolver instead. Will be removed in Pimcore 11', __METHOD__)
);

return $this->tokenResolver;
}

/**
Expand All @@ -101,10 +153,10 @@ public function getTokenResolver()
protected function getAdminUser($proxyUser = false)
{
if ($proxyUser) {
return $this->getTokenResolver()->getUserProxy();
return $this->tokenResolver->getUserProxy();
}

return $this->getTokenResolver()->getUser();
return $this->tokenResolver->getUser();
}

/**
Expand Down Expand Up @@ -280,6 +332,6 @@ protected function adminJson($data, $status = 200, $headers = [], $context = [],
*/
public function trans($id, array $parameters = [], $domain = 'admin', $locale = null)
{
return $this->getTranslator()->trans($id, $parameters, $domain, $locale);
return $this->translator->trans($id, $parameters, $domain, $locale);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function updateExtensionsAction(Request $request)
$updates[$id] = $options;
}

$this->getBundleManager()->setStates($updates);
$this->bundleManager->setStates($updates);

return $this->adminJson([
'extensions' => $this->getBundleList(array_keys($updates)),
Expand Down Expand Up @@ -154,7 +154,7 @@ public function toggleExtensionStateAction(
];

if ($type === 'bundle') {
$this->getBundleManager()->setState($id, ['enabled' => $enable]);
$this->bundleManager->setState($id, ['enabled' => $enable]);
$reload = true;

$message = $this->installAssets($assetsInstaller, $enable);
Expand Down Expand Up @@ -248,17 +248,17 @@ public function uninstallAction(Request $request)
private function handleInstallation(Request $request, $install = true)
{
try {
$bundle = $this->getBundleManager()->getActiveBundle($request->get('id'), false);
$bundle = $this->bundleManager->getActiveBundle($request->get('id'), false);

if ($install) {
$this->getBundleManager()->install($bundle);
$this->bundleManager->install($bundle);
} else {
$this->getBundleManager()->uninstall($bundle);
$this->bundleManager->uninstall($bundle);
}

$data = [
'success' => true,
'reload' => $this->getBundleManager()->needsReloadAfterInstall($bundle),
'reload' => $this->bundleManager->needsReloadAfterInstall($bundle),
];

if (!empty($message = $this->getInstallerOutput($bundle))) {
Expand Down Expand Up @@ -286,7 +286,7 @@ private function handleInstallation(Request $request, $install = true)
*/
private function getBundleList(array $filter = [])
{
$bm = $this->getBundleManager();
$bm = $this->bundleManager;

$results = [];
foreach ($bm->getEnabledBundleNames() as $className) {
Expand Down Expand Up @@ -375,7 +375,7 @@ private function buildBundleInstance($bundleName)
*/
private function buildBundleInfo(PimcoreBundleInterface $bundle, $enabled = false, $installed = false)
{
$bm = $this->getBundleManager();
$bm = $this->bundleManager;

$state = $bm->getState($bundle);

Expand Down Expand Up @@ -474,11 +474,11 @@ private function buildBrickInfo(AreabrickInterface $brick)

private function getInstallerOutput(PimcoreBundleInterface $bundle, bool $decorated = false)
{
if (!$this->getBundleManager()->isEnabled($bundle)) {
if (!$this->bundleManager->isEnabled($bundle)) {
return null;
}

$installer = $this->getBundleManager()->getInstaller($bundle);
$installer = $this->bundleManager->getInstaller($bundle);
if (null !== $installer) {
$output = $installer->getOutput();
if ($output instanceof BufferedOutput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ class AdminOrderController extends AdminController implements KernelControllerEv
public function onKernelControllerEvent(ControllerEvent $event)
{
// set language
$user = $this->getTokenResolver()->getUser();
$user = $this->tokenResolver->getUser();

if ($user) {
if ($this->getTranslator() instanceof LocaleAwareInterface) {
$this->getTranslator()->setLocale($user->getLanguage());
if ($this->translator instanceof LocaleAwareInterface) {
$this->translator->setLocale($user->getLanguage());
}
$event->getRequest()->setLocale($user->getLanguage());
}
Expand Down

0 comments on commit f14d52d

Please sign in to comment.