From 6d1889e8d2b416ed024f7d2c8a1e7b1748e8c999 Mon Sep 17 00:00:00 2001 From: wilsonge Date: Sun, 29 Mar 2020 03:50:13 +0100 Subject: [PATCH] Use CMSWebApplicationInterface in controllers --- .../src/MVC/Controller/AdminController.php | 34 ++-- .../src/MVC/Controller/ApiController.php | 27 ++- .../src/MVC/Controller/BaseController.php | 162 +++++++++++++++--- .../src/MVC/Controller/FormController.php | 75 ++++---- libraries/src/MVC/Factory/MVCFactory.php | 23 ++- 5 files changed, 237 insertions(+), 84 deletions(-) diff --git a/libraries/src/MVC/Controller/AdminController.php b/libraries/src/MVC/Controller/AdminController.php index 6f455f220a3c1..459db92861cd3 100644 --- a/libraries/src/MVC/Controller/AdminController.php +++ b/libraries/src/MVC/Controller/AdminController.php @@ -10,7 +10,7 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\CMSWebApplicationInterface; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Factory\MVCFactoryInterface; @@ -30,6 +30,14 @@ */ class AdminController extends BaseController { + /** + * The Application. Redeclared to show this class requires a web application. + * + * @var CMSWebApplicationInterface + * @since __DEPLOY_VERSION__ + */ + protected $app; + /** * The URL option for the component. * @@ -57,16 +65,17 @@ class AdminController extends BaseController /** * Constructor. * - * @param array $config An optional associative array of configuration settings. - * Recognized key values include 'name', 'default_task', 'model_path', and - * 'view_path' (this list is not meant to be comprehensive). - * @param MVCFactoryInterface $factory The factory. - * @param CMSApplication $app The Application for the dispatcher - * @param Input $input The Input object for the request + * @param array $config An optional associative array of configuration settings. + * Recognized key values include 'name', 'default_task', + * 'model_path', and 'view_path' (this list is not meant to be + * comprehensive). + * @param MVCFactoryInterface $factory The factory. + * @param CMSWebApplicationInterface $app The Application for the dispatcher + * @param Input $input The Input object for the request * * @since 3.0 */ - public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null) + public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSWebApplicationInterface $app = null, ?Input $input = null) { parent::__construct($config, $factory, $app, $input); @@ -135,7 +144,7 @@ public function delete() if (!\is_array($cid) || \count($cid) < 1) { - $this->app->getLogger()->warning(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), array('category' => 'jerror')); + $this->getLogger()->warning(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), array('category' => 'jerror')); } else { @@ -202,7 +211,7 @@ public function publish() if (empty($cid)) { - $this->app->getLogger()->warning(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), array('category' => 'jerror')); + $this->getLogger()->warning(Text::_($this->text_prefix . '_NO_ITEM_SELECTED'), array('category' => 'jerror')); } else { @@ -223,7 +232,10 @@ public function publish() { if ($errors) { - $this->app->enqueueMessage(Text::plural($this->text_prefix . '_N_ITEMS_FAILED_PUBLISHING', \count($cid)), 'error'); + $this->app->enqueueMessage( + Text::plural($this->text_prefix . '_N_ITEMS_FAILED_PUBLISHING', \count($cid)), + CMSWebApplicationInterface::MSG_ERROR + ); } else { diff --git a/libraries/src/MVC/Controller/ApiController.php b/libraries/src/MVC/Controller/ApiController.php index 208a6021486c0..1c354b1548d48 100644 --- a/libraries/src/MVC/Controller/ApiController.php +++ b/libraries/src/MVC/Controller/ApiController.php @@ -11,7 +11,7 @@ \defined('JPATH_PLATFORM') or die; use Joomla\CMS\Access\Exception\NotAllowed; -use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\CMSWebApplicationInterface; use Joomla\CMS\Component\ComponentHelper; use Joomla\CMS\Form\Form; use Joomla\CMS\Language\Text; @@ -33,6 +33,14 @@ */ class ApiController extends BaseController { + /** + * The Application. Redeclared to show this class requires a web application. + * + * @var CMSWebApplicationInterface + * @since __DEPLOY_VERSION__ + */ + protected $app; + /** * The content type of the item. * @@ -82,17 +90,18 @@ class ApiController extends BaseController /** * Constructor. * - * @param array $config An optional associative array of configuration settings. - * Recognized key values include 'name', 'default_task', 'model_path', and - * 'view_path' (this list is not meant to be comprehensive). - * @param MVCFactoryInterface $factory The factory. - * @param CMSApplication $app The Application for the dispatcher - * @param Input $input Input + * @param array $config An optional associative array of configuration settings. + * Recognized key values include 'name', 'default_task', + * 'model_path', and 'view_path' (this list is not meant to be + * comprehensive). + * @param MVCFactoryInterface $factory The factory. + * @param CMSWebApplicationInterface $app The Application for the dispatcher + * @param Input $input Input * - * @since 4.0.0 * @throws \Exception + * @since 4.0.0 */ - public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null) + public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSWebApplicationInterface $app = null, ?Input $input = null) { $this->modelState = new CMSObject; diff --git a/libraries/src/MVC/Controller/BaseController.php b/libraries/src/MVC/Controller/BaseController.php index 31e28dcd99f27..de679e5758417 100644 --- a/libraries/src/MVC/Controller/BaseController.php +++ b/libraries/src/MVC/Controller/BaseController.php @@ -10,7 +10,9 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Application\CMSApplication; +use Joomla\Application\AbstractApplication; +use Joomla\CMS\Application\CMSApplicationInterface; +use Joomla\CMS\Application\CMSWebApplicationInterface; use Joomla\CMS\Cache\Exception\CacheExceptionInterface; use Joomla\CMS\Factory; use Joomla\CMS\Filesystem\Path; @@ -25,6 +27,10 @@ use Joomla\CMS\Session\Session; use Joomla\CMS\Uri\Uri; use Joomla\Input\Input; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerAwareTrait; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; /** * Base class for a Joomla Controller @@ -34,8 +40,10 @@ * * @since 2.5.5 */ -class BaseController implements ControllerInterface +class BaseController implements ControllerInterface, LoggerAwareInterface { + use LoggerAwareTrait; + /** * The base path of the controller * @@ -167,7 +175,7 @@ class BaseController implements ControllerInterface /** * The Application * - * @var CMSApplication|null + * @var CMSApplicationInterface * @since 4.0.0 */ protected $app; @@ -357,16 +365,17 @@ public static function getInstance($prefix, $config = array()) /** * Constructor. * - * @param array $config An optional associative array of configuration settings. - * Recognized key values include 'name', 'default_task', 'model_path', and - * 'view_path' (this list is not meant to be comprehensive). - * @param MVCFactoryInterface $factory The factory. - * @param CMSApplication $app The Application for the dispatcher - * @param Input $input Input + * @param array $config An optional associative array of configuration settings. + * Recognized key values include 'name', 'default_task', + * 'model_path', and 'view_path' (this list is not meant to be + * comprehensive). + * @param MVCFactoryInterface $factory The factory. + * @param CMSApplicationInterface $app The Application for the dispatcher + * @param Input $input Input * * @since 3.0 */ - public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null) + public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSApplicationInterface $app = null, ?Input $input = null) { $this->methods = array(); $this->message = null; @@ -375,8 +384,18 @@ public function __construct($config = array(), MVCFactoryInterface $factory = nu $this->redirect = null; $this->taskMap = array(); - $this->app = $app ? $app : Factory::getApplication(); - $this->input = $input ? $input : $this->app->input; + $this->app = $app ?: Factory::getApplication(); + $this->input = $input ?: $this->app->getInput(); + + /** + * @deprecated This is to maintain b/c with the J4.0 implementation of BaseController. In Joomla 5 this will be + * removed and instead the logger should be injected by the MVCFactory using + * BaseController::setLogger() + */ + if ($this->app instanceof AbstractApplication) + { + $this->setLogger($this->app->getLogger()); + } if (\defined('JDEBUG') && JDEBUG) { @@ -542,6 +561,18 @@ public function addViewPath($path) */ protected function checkEditId($context, $id) { + if (!($this->app instanceof CMSWebApplicationInterface)) + { + throw new \Exception( + sprintf( + 'The %s method requires an instance of %s but instead %s was supplied', + __METHOD__, + CMSWebApplicationInterface::class, + get_class($this->app) + ) + ); + } + if ($id) { $values = (array) $this->app->getUserState($context . '.id'); @@ -550,7 +581,7 @@ protected function checkEditId($context, $id) if (\defined('JDEBUG') && JDEBUG) { - $this->app->getLogger()->info( + $this->getLogger()->info( sprintf( 'Checking edit ID %s.%s: %d %s', $context, @@ -633,6 +664,18 @@ protected function createView($name, $prefix = '', $type = '', $config = array() */ public function display($cachable = false, $urlparams = array()) { + if (!($this->app instanceof CMSWebApplicationInterface)) + { + throw new \Exception( + sprintf( + 'The %s method requires an instance of %s but instead %s was supplied', + __METHOD__, + CMSWebApplicationInterface::class, + get_class($this->app) + ) + ); + } + $document = $this->app->getDocument(); $viewType = $document->getType(); $viewName = $this->input->get('view', $this->default_view); @@ -650,14 +693,12 @@ public function display($cachable = false, $urlparams = array()) $view->document = $document; // Display the view - if ($cachable && $viewType !== 'feed' && Factory::getApplication()->get('caching') >= 1) + if ($cachable && $viewType !== 'feed' && $this->app->get('caching') >= 1) { $option = $this->input->get('option'); if (\is_array($urlparams)) { - $this->app = Factory::getApplication(); - if (!empty($this->app->registeredurlparams)) { $registeredurlparams = $this->app->registeredurlparams; @@ -777,15 +818,18 @@ public function getModel($name = '', $prefix = '', $config = array()) return $model; } - // Let's get the application object and set menu information if it's available - $menu = Factory::getApplication()->getMenu(); - - if (\is_object($menu) && $item = $menu->getActive()) + if ($this->app instanceof CMSWebApplicationInterface) { - $params = $menu->getParams($item->id); + // Let's get the application object and set menu information if it's available + $menu = $this->app->getMenu(); - // Set default state data - $model->setState('parameters.menu', $params); + if (\is_object($menu) && $item = $menu->getActive()) + { + $params = $menu->getParams($item->id); + + // Set default state data + $model->setState('parameters.menu', $params); + } } } @@ -914,6 +958,18 @@ public function getView($name = '', $type = '', $prefix = '', $config = array()) */ protected function holdEditId($context, $id) { + if (!($this->app instanceof CMSWebApplicationInterface)) + { + throw new \Exception( + sprintf( + 'The %s method requires an instance of %s but instead %s was supplied', + __METHOD__, + CMSWebApplicationInterface::class, + get_class($this->app) + ) + ); + } + $values = (array) $this->app->getUserState($context . '.id'); // Add the id to the list if non-zero. @@ -925,7 +981,7 @@ protected function holdEditId($context, $id) if (\defined('JDEBUG') && JDEBUG) { - $this->app->getLogger()->info( + $this->getLogger()->info( sprintf( 'Holding edit ID %s.%s %s', $context, @@ -947,6 +1003,18 @@ protected function holdEditId($context, $id) */ public function redirect() { + if (!($this->app instanceof CMSWebApplicationInterface)) + { + throw new \Exception( + sprintf( + 'The %s method requires an instance of %s but instead %s was supplied', + __METHOD__, + CMSWebApplicationInterface::class, + get_class($this->app) + ) + ); + } + if ($this->redirect) { // Enqueue the redirect message @@ -1023,6 +1091,18 @@ public function unregisterTask($task) */ protected function releaseEditId($context, $id) { + if (!($this->app instanceof CMSWebApplicationInterface)) + { + throw new \Exception( + sprintf( + 'The %s method requires an instance of %s but instead %s was supplied', + __METHOD__, + CMSWebApplicationInterface::class, + get_class($this->app) + ) + ); + } + $values = (array) $this->app->getUserState($context . '.id'); // Do a strict search of the edit list values. @@ -1035,7 +1115,7 @@ protected function releaseEditId($context, $id) if (\defined('JDEBUG') && JDEBUG) { - $this->app->getLogger()->info( + $this->getLogger()->info( sprintf( 'Releasing edit ID %s.%s %s', $context, @@ -1048,6 +1128,24 @@ protected function releaseEditId($context, $id) } } + /** + * Get the logger. + * + * @return LoggerInterface + * + * @since __DEPLOY_VERSION__ + */ + public function getLogger() + { + // If a logger hasn't been set, use NullLogger + if (!($this->logger instanceof LoggerInterface)) + { + $this->setLogger(new NullLogger); + } + + return $this->logger; + } + /** * Sets the internal message that is passed with a redirect * @@ -1101,6 +1199,18 @@ protected function setPath($type, $path) */ public function checkToken($method = 'post', $redirect = true) { + if (!($this->app instanceof CMSWebApplicationInterface)) + { + throw new \Exception( + sprintf( + 'The %s method requires an instance of %s but instead %s was supplied', + __METHOD__, + CMSWebApplicationInterface::class, + get_class($this->app) + ) + ); + } + $valid = Session::checkToken($method); if (!$valid && $redirect) @@ -1112,7 +1222,7 @@ public function checkToken($method = 'post', $redirect = true) $referrer = 'index.php'; } - $this->app->enqueueMessage(Text::_('JINVALID_TOKEN_NOTICE'), 'warning'); + $this->app->enqueueMessage(Text::_('JINVALID_TOKEN_NOTICE'), CMSWebApplicationInterface::MSG_WARNING); $this->app->redirect($referrer); } diff --git a/libraries/src/MVC/Controller/FormController.php b/libraries/src/MVC/Controller/FormController.php index 17f904df0fce9..329a65a0afa91 100644 --- a/libraries/src/MVC/Controller/FormController.php +++ b/libraries/src/MVC/Controller/FormController.php @@ -10,9 +10,8 @@ \defined('JPATH_PLATFORM') or die; -use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Application\CMSWebApplicationInterface; use Joomla\CMS\Component\ComponentHelper; -use Joomla\CMS\Factory; use Joomla\CMS\Form\FormFactoryAwareInterface; use Joomla\CMS\Form\FormFactoryAwareTrait; use Joomla\CMS\Form\FormFactoryInterface; @@ -33,6 +32,14 @@ class FormController extends BaseController implements FormFactoryAwareInterface { use FormFactoryAwareTrait; + /** + * The Application. Redeclared to show this class requires a web application. + * + * @var CMSWebApplicationInterface + * @since __DEPLOY_VERSION__ + */ + protected $app; + /** * The context for storing internal data, e.g. record. * @@ -76,17 +83,19 @@ class FormController extends BaseController implements FormFactoryAwareInterface /** * Constructor. * - * @param array $config An optional associative array of configuration settings. - * Recognized key values include 'name', 'default_task', 'model_path', and - * 'view_path' (this list is not meant to be comprehensive). - * @param MVCFactoryInterface $factory The factory. - * @param CMSApplication $app The Application for the dispatcher - * @param Input $input Input - * @param FormFactoryInterface $formFactory The form factory. + * + * @param array $config An optional associative array of configuration settings. + * Recognized key values include 'name', 'default_task', + * 'model_path', and 'view_path' (this list is not meant to be + * comprehensive). + * @param MVCFactoryInterface $factory The factory. + * @param CMSWebApplicationInterface $app The Application for the dispatcher + * @param Input $input Input + * @param FormFactoryInterface $formFactory The form factory. * * @since 3.0 */ - public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSApplication $app = null, ?Input $input = null, + public function __construct($config = array(), MVCFactoryInterface $factory = null, ?CMSWebApplicationInterface $app = null, ?Input $input = null, FormFactoryInterface $formFactory = null ) { @@ -176,7 +185,7 @@ public function add() } // Clear the record edit information from the session. - Factory::getApplication()->setUserState($context . '.data', null); + $this->app->setUserState($context . '.data', null); // Redirect to the edit screen. $this->setRedirect( @@ -202,7 +211,7 @@ public function add() */ protected function allowAdd($data = array()) { - $user = Factory::getUser(); + $user = $this->app->getIdentity(); return $user->authorise('core.create', $this->option) || \count($user->getAuthorisedCategories($this->option, 'core.create')); } @@ -221,7 +230,7 @@ protected function allowAdd($data = array()) */ protected function allowEdit($data = array(), $key = 'id') { - return Factory::getUser()->authorise('core.edit', $this->option); + return $this->app->getIdentity()->authorise('core.edit', $this->option); } /** @@ -332,7 +341,7 @@ public function cancel($key = null) // Clean the session data and redirect. $this->releaseEditId($context, $recordId); - Factory::getApplication()->setUserState($context . '.data', null); + $this->app->setUserState($context . '.data', null); $url = 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(); @@ -365,7 +374,7 @@ public function cancel($key = null) public function edit($key = null, $urlVar = null) { // Do not cache the response to this, its a redirect, and mod_expires and google chrome browser bugs cache it forever! - Factory::getApplication()->allowCache(false); + $this->app->allowCache(false); $model = $this->getModel(); $table = $model->getTable(); @@ -422,7 +431,7 @@ public function edit($key = null, $urlVar = null) { // Check-out succeeded, push the new record id into the session. $this->holdEditId($context, $recordId); - Factory::getApplication()->setUserState($context . '.data', null); + $this->app->setUserState($context . '.data', null); $this->setRedirect( Route::_( @@ -556,7 +565,6 @@ public function save($key = null, $urlVar = null) // Check for request forgeries. $this->checkToken(); - $app = Factory::getApplication(); $model = $this->getModel(); $table = $model->getTable(); $data = $this->input->post->get('jform', array(), 'array'); @@ -627,14 +635,14 @@ public function save($key = null, $urlVar = null) if (!$form) { - $app->enqueueMessage($model->getError(), 'error'); + $this->app->enqueueMessage($model->getError(), CMSWebApplicationInterface::MSG_ERROR); return false; } // Send an object which can be modified through the plugin event $objData = (object) $data; - $app->triggerEvent( + $this->app->triggerEvent( 'onContentNormaliseRequestData', array($this->option . '.' . $this->context, $objData, $form) ); @@ -654,11 +662,11 @@ public function save($key = null, $urlVar = null) { if ($errors[$i] instanceof \Exception) { - $app->enqueueMessage($errors[$i]->getMessage(), 'warning'); + $this->app->enqueueMessage($errors[$i]->getMessage(), CMSWebApplicationInterface::MSG_WARNING); } else { - $app->enqueueMessage($errors[$i], 'warning'); + $this->app->enqueueMessage($errors[$i], CMSWebApplicationInterface::MSG_WARNING); } } @@ -683,7 +691,7 @@ public function save($key = null, $urlVar = null) } // Save the data in the session. - $app->setUserState($context . '.data', $data); + $this->app->setUserState($context . '.data', $data); // Redirect back to the edit screen. $this->setRedirect( @@ -705,7 +713,7 @@ public function save($key = null, $urlVar = null) if (!$model->save($validData)) { // Save the data in the session. - $app->setUserState($context . '.data', $validData); + $this->app->setUserState($context . '.data', $validData); // Redirect back to the edit screen. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()), 'error'); @@ -724,7 +732,7 @@ public function save($key = null, $urlVar = null) if ($checkin && $model->checkin($validData[$key]) === false) { // Save the data in the session. - $app->setUserState($context . '.data', $validData); + $this->app->setUserState($context . '.data', $validData); // Check-in failed, so go back to the record and display a notice. $this->setMessage(Text::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()), 'error'); @@ -739,10 +747,10 @@ public function save($key = null, $urlVar = null) return false; } - $langKey = $this->text_prefix . ($recordId === 0 && $app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS'; - $prefix = Factory::getLanguage()->hasKey($langKey) ? $this->text_prefix : 'JLIB_APPLICATION'; + $langKey = $this->text_prefix . ($recordId === 0 && $this->app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS'; + $prefix = $this->app->getLanguage()->hasKey($langKey) ? $this->text_prefix : 'JLIB_APPLICATION'; - $this->setMessage(Text::_($prefix . ($recordId === 0 && $app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS')); + $this->setMessage(Text::_($prefix . ($recordId === 0 && $this->app->isClient('site') ? '_SUBMIT' : '') . '_SAVE_SUCCESS')); // Redirect the user and adjust session state based on the chosen task. switch ($task) @@ -751,7 +759,7 @@ public function save($key = null, $urlVar = null) // Set the record data in the session. $recordId = $model->getState($this->context . '.id'); $this->holdEditId($context, $recordId); - $app->setUserState($context . '.data', null); + $this->app->setUserState($context . '.data', null); $model->checkout($recordId); // Redirect back to the edit screen. @@ -766,7 +774,7 @@ public function save($key = null, $urlVar = null) case 'save2new': // Clear the record id and data from the session. $this->releaseEditId($context, $recordId); - $app->setUserState($context . '.data', null); + $this->app->setUserState($context . '.data', null); // Redirect back to the edit screen. $this->setRedirect( @@ -780,7 +788,7 @@ public function save($key = null, $urlVar = null) default: // Clear the record id and data from the session. $this->releaseEditId($context, $recordId); - $app->setUserState($context . '.data', null); + $this->app->setUserState($context . '.data', null); $url = 'index.php?option=' . $this->option . '&view=' . $this->view_list . $this->getRedirectToListAppend(); @@ -819,7 +827,6 @@ public function reload($key = null, $urlVar = null) // Check for request forgeries. $this->checkToken(); - $app = Factory::getApplication(); $model = $this->getModel(); $data = $this->input->post->get('jform', array(), 'array'); @@ -883,7 +890,7 @@ public function reload($key = null, $urlVar = null) } // Save the data in the session. - $app->setUserState($this->option . '.edit.' . $this->context . '.data', $data); + $this->app->setUserState($this->option . '.edit.' . $this->context . '.data', $data); $this->setRedirect($redirectUrl); $this->redirect(); @@ -901,11 +908,9 @@ public function reload($key = null, $urlVar = null) public function editAssociations() { // Initialise variables. - $app = Factory::getApplication(); - $input = $app->input; $model = $this->getModel(); - $data = $input->get('jform', array(), 'array'); + $data = $this->input->get('jform', array(), 'array'); $model->editAssociations($data); } } diff --git a/libraries/src/MVC/Factory/MVCFactory.php b/libraries/src/MVC/Factory/MVCFactory.php index 5dff90dc6cb56..74dfa2d13f725 100644 --- a/libraries/src/MVC/Factory/MVCFactory.php +++ b/libraries/src/MVC/Factory/MVCFactory.php @@ -16,6 +16,8 @@ use Joomla\CMS\Form\FormFactoryAwareTrait; use Joomla\CMS\MVC\Model\ModelInterface; use Joomla\Input\Input; +use Psr\Log\LoggerAwareInterface; +use Psr\Log\LoggerInterface; /** * Factory to create MVC objects based on a namespace. @@ -34,17 +36,27 @@ class MVCFactory implements MVCFactoryInterface, FormFactoryAwareInterface */ private $namespace; + /** + * The namespace to create the objects from. + * + * @var LoggerInterface + * @since 4.0.0 + */ + private $logger; + /** * The namespace must be like: * Joomla\Component\Content * - * @param string $namespace The namespace + * @param string $namespace The namespace + * @param LoggerInterface|null $logger A logging instance to inject into the controller if required * * @since 4.0.0 */ - public function __construct($namespace) + public function __construct($namespace, LoggerInterface $logger = null) { $this->namespace = $namespace; + $this->logger = $logger; } /** @@ -56,7 +68,7 @@ public function __construct($namespace) * @param CMSApplicationInterface $app The app * @param Input $input The input * - * @return \Joomla\CMS\MVC\Controller\ControllerInterface + * @return \Joomla\CMS\MVC\Controller\ControllerInterface|null * * @since 3.10.0 * @throws \Exception @@ -77,6 +89,11 @@ public function createController($name, $prefix, array $config, CMSApplicationIn $controller = new $className($config, $this, $app, $input); $this->setFormFactoryOnObject($controller); + if ($controller instanceof LoggerAwareInterface && $this->logger !== null) + { + $controller->setLogger($this->logger); + } + return $controller; }