/**
* Method to get a singleton controller instance.
*
* @param string $prefix The prefix for the controller.
* @param array $config An array of optional constructor options.
*
* @return static
*
* @since 3.0
* @deprecated 5.0 Get the controller through the MVCFactory instead
* @throws \Exception if the controller cannot be loaded.
*/
public static function getInstance($prefix, $config = array())
{
if (\is_object(self::$instance)) {
return self::$instance;
}
@trigger_error(sprintf('%1$s::getInstance() is deprecated. Load it through the MVC factory.', self::class), E_USER_DEPRECATED);
$app = Factory::getApplication();
$input = $app->input;
// Get the environment configuration.
$basePath = \array_key_exists('base_path', $config) ? $config['base_path'] : JPATH_COMPONENT;
$format = $input->getWord('format');
$command = $input->get('task', 'display');
// Check for array format.
$filter = InputFilter::getInstance();
if (\is_array($command)) {
$keys = array_keys($command);
$command = $filter->clean(array_pop($keys), 'cmd');
} else {
$command = $filter->clean($command, 'cmd');
}
// Check for a controller.task command.
if (strpos($command, '.') !== false) {
// Explode the controller.task command.
list($type, $task) = explode('.', $command);
// Define the controller filename and path.
$file = self::createFileName('controller', array('name' => $type, 'format' => $format));
$path = $basePath . '/controllers/' . $file;
$backuppath = $basePath . '/controller/' . $file;
// Reset the task without the controller context.
$input->set('task', $task);
} else {
// Base controller.
$type = '';
// Define the controller filename and path.
$file = self::createFileName('controller', array('name' => 'controller', 'format' => $format));
$path = $basePath . '/' . $file;
$backupfile = self::createFileName('controller', array('name' => 'controller'));
$backuppath = $basePath . '/' . $backupfile;
}
// Get the controller class name.
$class = ucfirst($prefix) . 'Controller' . ucfirst($type);
// Include the class if not present.
if (!class_exists($class)) {
// If the controller file path exists, include it.
if (is_file($path)) {
require_once $path;
} elseif (isset($backuppath) && is_file($backuppath)) {
require_once $backuppath;
} else {
throw new \InvalidArgumentException(Text::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER', $type, $format));
}
}
// Instantiate the class.
if (!class_exists($class)) {
throw new \InvalidArgumentException(Text::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER_CLASS', $class));
}
// Check for a possible service from the container otherwise manually instantiate the class
if (Factory::getContainer()->has($class)) {
self::$instance = Factory::getContainer()->get($class);
} else {
self::$instance = new $class($config, null, $app, $input);
}
return self::$instance;
}