Commit c4dd26c0 authored by webchick's avatar webchick

Issue #2077513 by tim.plunkett, jibran, ParisLiakos, msonnabaum: Refactor...

Issue #2077513 by tim.plunkett, jibran, ParisLiakos, msonnabaum: Refactor ControllerBase to be more consistent with FormBase.
parent 6f76e45a
......@@ -7,7 +7,6 @@
namespace Drupal\Core\Controller;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\RedirectResponse;
/**
......@@ -29,7 +28,70 @@
*
* @see \Drupal\Core\DependencyInjection\ContainerInjectionInterface
*/
abstract class ControllerBase extends ContainerAware {
abstract class ControllerBase {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* The translation manager.
*
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $translationManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\Config
*/
protected $configFactory;
/**
* The key-value storage.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $keyValue;
/**
* The url generator.
*
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* The current user service.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The state service.
*
* @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected $stateService;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Retrieves the entity manager service.
......@@ -38,7 +100,10 @@ abstract class ControllerBase extends ContainerAware {
* The entity manager service.
*/
protected function entityManager() {
return $this->container->get('entity.manager');
if (!$this->entityManager) {
$this->entityManager = $this->container()->get('entity.manager');
}
return $this->entityManager;
}
/**
......@@ -52,7 +117,7 @@ protected function entityManager() {
* The cache object associated with the specified bin.
*/
protected function cache($bin = 'cache') {
return $this->container->get('cache.' . $bin);
return $this->container()->get('cache.' . $bin);
}
/**
......@@ -72,7 +137,10 @@ protected function cache($bin = 'cache') {
* A configuration object.
*/
protected function config($name) {
return $this->container->get('config.factory')->get($name);
if (!$this->configFactory) {
$this->configFactory = $this->container()->get('config.factory')->get($name);
}
return $this->configFactory;
}
/**
......@@ -84,7 +152,10 @@ protected function config($name) {
* @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected function keyValue($collection) {
return $this->container->get('keyvalue')->get($collection);
if (!$this->keyValue) {
$this->keyValue = $this->container()->get('keyvalue')->get($collection);
}
return $this->keyValue;
}
/**
......@@ -99,7 +170,10 @@ protected function keyValue($collection) {
* @return \Drupal\Core\KeyValueStore\KeyValueStoreInterface
*/
protected function state() {
return $this->container->get('state');
if (!$this->stateService) {
$this->stateService = $this->container()->get('state');
}
return $this->stateService;
}
/**
......@@ -108,7 +182,10 @@ protected function state() {
* @return \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected function moduleHandler() {
return $this->container->get('module_handler');
if (!$this->moduleHandler) {
$this->moduleHandler = $this->container()->get('module_handler');
}
return $this->moduleHandler;
}
/**
......@@ -118,20 +195,23 @@ protected function moduleHandler() {
* The URL generator service.
*/
protected function urlGenerator() {
return $this->container->get('url_generator');
if (!$this->urlGenerator) {
$this->urlGenerator = $this->container()->get('url_generator');
}
return $this->urlGenerator;
}
/**
* Renders a link to a route given a route name and its parameters.
*
* @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
* @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
* on the arguments, usage, and possible exceptions.
*
* @return string
* An HTML string containing a link to the given route and parameters.
*/
public function l($text, $route_name, array $parameters = array(), array $options = array()) {
return $this->container->get('link_generator')->generate($text, $route_name, $parameters, $options);
return $this->container()->get('link_generator')->generate($text, $route_name, $parameters, $options);
}
/**
......@@ -141,7 +221,10 @@ public function l($text, $route_name, array $parameters = array(), array $option
* The current user.
*/
protected function currentUser() {
return $this->container->get('current_user');
if (!$this->currentUser) {
$this->currentUser = $this->container()->get('current_user');
}
return $this->currentUser;
}
/**
......@@ -150,7 +233,20 @@ protected function currentUser() {
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->container->get('string_translation')->translate($string, $args, $options);
return $this->translationManager()->translate($string, $args, $options);
}
/**
* Returns the translation manager.
*
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function translationManager() {
if (!$this->translationManager) {
$this->translationManager = $this->container()->get('string_translation');
}
return $this->translationManager;
}
/**
......@@ -160,7 +256,20 @@ protected function t($string, array $args = array(), array $options = array()) {
* The language manager.
*/
protected function languageManager() {
return $this->container->get('language_manager');
if (!$this->languageManager) {
$this->languageManager = $this->container()->get('language_manager');
}
return $this->languageManager;
}
/**
* Returns the service container.
*
* @return \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container.
*/
protected function container() {
return \Drupal::getContainer();
}
/**
......@@ -176,7 +285,7 @@ protected function languageManager() {
* A redirect response object that may be returned by the controller.
*/
public function redirect($route_name, array $route_parameters = array(), $status = 302) {
$url = $this->container->get('url_generator')->generate($route_name, $route_parameters, TRUE);
$url = $this->urlGenerator()->generate($route_name, $route_parameters, TRUE);
return new RedirectResponse($url, $status);
}
......@@ -190,7 +299,7 @@ public function redirect($route_name, array $route_parameters = array(), $status
* The generated URL for the given route.
*/
public function url($route_name, $route_parameters = array(), $options = array()) {
return $this->container->get('url_generator')->generateFromRoute($route_name, $route_parameters, $options);
return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
}
}
......@@ -219,7 +219,7 @@ public function render() {
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->getTranslationManager()->translate($string, $args, $options);
return $this->translationManager()->translate($string, $args, $options);
}
/**
......@@ -228,7 +228,7 @@ protected function t($string, array $args = array(), array $options = array()) {
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function getTranslationManager() {
protected function translationManager() {
if (!$this->translationManager) {
$this->translationManager = \Drupal::translation();
}
......
......@@ -60,7 +60,7 @@ public function validateForm(array &$form, array &$form_state) {
* See the t() documentation for details.
*/
protected function t($string, array $args = array(), array $options = array()) {
return $this->getTranslationManager()->translate($string, $args, $options);
return $this->translationManager()->translate($string, $args, $options);
}
/**
......@@ -73,7 +73,7 @@ protected function t($string, array $args = array(), array $options = array()) {
* The generated URL for the given route.
*/
public function url($route_name, $route_parameters = array(), $options = array()) {
return $this->getUrlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
return $this->urlGenerator()->generateFromRoute($route_name, $route_parameters, $options);
}
/**
......@@ -82,9 +82,9 @@ public function url($route_name, $route_parameters = array(), $options = array()
* @return \Drupal\Core\StringTranslation\TranslationInterface
* The translation manager.
*/
protected function getTranslationManager() {
protected function translationManager() {
if (!$this->translationManager) {
$this->translationManager = \Drupal::translation();
$this->translationManager = $this->container()->get('string_translation');
}
return $this->translationManager;
}
......@@ -111,7 +111,7 @@ public function setTranslationManager(TranslationInterface $translation_manager)
*/
protected function getRequest() {
if (!$this->request) {
$this->request = \Drupal::request();
$this->request = $this->container()->get('request');
}
return $this->request;
}
......@@ -132,7 +132,7 @@ public function setRequest(Request $request) {
* @return \Drupal\Core\Session\AccountInterface
* The current user.
*/
protected function getCurrentUser() {
protected function currentUser() {
return $this->getRequest()->attributes->get('_account');
}
......@@ -142,7 +142,7 @@ protected function getCurrentUser() {
* @return \Drupal\Core\Routing\UrlGeneratorInterface
* The URL generator.
*/
protected function getUrlGenerator() {
protected function urlGenerator() {
if (!$this->urlGenerator) {
$this->urlGenerator = \Drupal::urlGenerator();
}
......@@ -159,4 +159,14 @@ public function setUrlGenerator(UrlGeneratorInterface $url_generator) {
$this->urlGenerator = $url_generator;
}
/**
* Returns the service container.
*
* @return \Symfony\Component\DependencyInjection\ContainerInterface $container
* The service container.
*/
protected function container() {
return \Drupal::getContainer();
}
}
......@@ -139,7 +139,7 @@ public function form(array $form, array &$form_state) {
// this entire visibility settings section probably needs a separate user
// interface in the near future.
$visibility = $entity->get('visibility');
$access = $this->getCurrentUser()->hasPermission('use PHP for settings');
$access = $this->currentUser()->hasPermission('use PHP for settings');
if (!empty($visibility['path']['visibility']) && $visibility['path']['visibility'] == BLOCK_VISIBILITY_PHP && !$access) {
$form['visibility']['path']['visibility'] = array(
'#type' => 'value',
......
......@@ -44,12 +44,10 @@ class ConfigController implements ContainerInjectionInterface {
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$file_download = new FileDownloadController();
$file_download->setContainer($container);
return new static(
$container->get('config.storage'),
$container->get('config.storage.staging'),
$file_download
new FileDownloadController()
);
}
......
......@@ -51,8 +51,8 @@ public function checkTranslation() {
*/
public function translatePage() {
return array(
'filter' => drupal_get_form(TranslateFilterForm::create($this->container)),
'form' => drupal_get_form(TranslateEditForm::create($this->container)),
'filter' => drupal_get_form(TranslateFilterForm::create($this->container())),
'form' => drupal_get_form(TranslateEditForm::create($this->container())),
);
}
......
......@@ -102,7 +102,7 @@ public function getFormID() {
* {@inheritdoc}
*/
public function buildForm(array $form, array &$form_state) {
$account = $this->getCurrentUser()->id();
$account = $this->currentUser()->id();
$this->modules = $this->keyValueExpirable->get($account);
// Redirect to the modules list page if the key value store is empty.
......@@ -140,7 +140,7 @@ public function buildForm(array $form, array &$form_state) {
*/
public function submitForm(array &$form, array &$form_state) {
// Remove the key value store entry.
$account = $this->getCurrentUser()->id();
$account = $this->currentUser()->id();
$this->keyValueExpirable->delete($account);
// Gets list of modules prior to install process.
......
......@@ -395,7 +395,7 @@ public function submitForm(array &$form, array &$form_state) {
// dependencies that are not enabled yet, redirect to the confirmation form.
if (!empty($modules['dependencies']) || !empty($modules['missing'])) {
// Write the list of changed module states into a key value store.
$account = $this->getCurrentUser()->id();
$account = $this->currentUser()->id();
$this->keyValueExpirable->setWithExpire($account, $modules, 60);
// Redirect to the confirmation form.
......
......@@ -103,7 +103,7 @@ public function getFormID() {
*/
public function buildForm(array $form, array &$form_state) {
// Retrieve the list of modules from the key value store.
$account = $this->getCurrentUser()->id();
$account = $this->currentUser()->id();
$this->modules = $this->keyValueExpirable->get($account);
// Prevent this page from showing when the module list is empty.
......@@ -128,7 +128,7 @@ public function buildForm(array $form, array &$form_state) {
*/
public function submitForm(array &$form, array &$form_state) {
// Clear the key value store entry.
$account = $this->getCurrentUser()->id();
$account = $this->currentUser()->id();
$this->keyValueExpirable->delete($account);
// Uninstall the modules.
......
......@@ -140,7 +140,7 @@ public function submitForm(array &$form, array &$form_state) {
// Save all the values in an expirable key value store.
$modules = $form_state['values']['uninstall'];
$uninstall = array_keys(array_filter($modules));
$account = $this->getCurrentUser()->id();
$account = $this->currentUser()->id();
$this->keyValueExpirable->setWithExpire($account, $uninstall, 60);
// Redirect to the confirm form.
......
......@@ -42,7 +42,7 @@ public function triggerException() {
*/
public function triggerPDOException() {
define('SIMPLETEST_COLLECT_ERRORS', FALSE);
$this->container->get('database')->query('SELECT * FROM bananas_are_awesome');
$this->container()->get('database')->query('SELECT * FROM bananas_are_awesome');
}
}
......@@ -84,7 +84,7 @@ public function buildForm(array $form, array &$form_state) {
),
);
// Allow logged in users to request this also.
$user = $this->getCurrentUser();
$user = $this->currentUser();
if ($user->isAuthenticated()) {
$form['name']['#type'] = 'value';
$form['name']['#value'] = $user->getEmail();
......
......@@ -26,7 +26,7 @@ protected function actions(array $form, array &$form_state) {
$account = $this->entity;
// The user doing the editing.
$user = $this->getCurrentUser();
$user = $this->currentUser();
$element['delete']['#type'] = 'submit';
$element['delete']['#value'] = $this->t('Cancel account');
$element['delete']['#submit'] = array(array($this, 'editCancelSubmit'));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment