- md5
- 84b16b9973fe96f3dfbdc628bf185487
- sha1
- 96e720263f9dd5a844abcbf5b669ae90c258b5bc
- sha256
- e1ee1e7fbd2c3a647143eff5091b7e39d8d9098afa9ae77d00bf7b4535b620b5
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
<?php
/**
* Joomla! Content Management System
*
* @copyright (C) 2025 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\Installer;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Application\CMSApplicationInterface;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Log\Log;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\Path;
trait InstallerScriptTrait
{
/**
* The extension name. This should be set in the installer script.
*
* @var string
* @since 6.0.0
*/
protected string $extension;
/**
* Minimum PHP version required to install the extension
*
* @var string
* @since 6.0.0
*/
protected string $minimumPhp;
/**
* Minimum Joomla! version required to install the extension
*
* @var string
* @since 6.0.0
*/
protected string $minimumJoomla;
/**
* Allow downgrades of your extension
*
* Use at your own risk as if there is a change in functionality people may wish to downgrade.
*
* @var boolean
* @since 6.0.0
*/
protected bool $allowDowngrades = false;
/**
* A list of files to be deleted
*
* @var array
* @since 6.0.0
*/
protected array $deleteFiles = [];
/**
* A list of folders to be deleted
*
* @var array
* @since 6.0.0
*/
protected array $deleteFolders = [];
/**
* The application object
*
* @var CMSApplicationInterface
*
* @since 6.0.0
*/
private CMSApplicationInterface $application;
/**
* Function called after the extension is installed.
*
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 6.0.0
*/
public function install(InstallerAdapter $adapter): bool
{
return true;
}
/**
* Function called after the extension is updated.
*
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 6.0.0
*/
public function update(InstallerAdapter $adapter): bool
{
return true;
}
/**
* Function called after the extension is uninstalled.
*
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 6.0.0
*/
public function uninstall(InstallerAdapter $adapter): bool
{
return true;
}
/**
* Function called before extension installation/update/removal procedure commences.
*
* @param string $type The type of change (install or discover_install, update, uninstall)
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 6.0.0
*/
public function preflight(string $type, InstallerAdapter $adapter): bool
{
$this->extension = $adapter->getName();
if (!$this->checkCompatibility($type, $adapter)) {
return false;
}
if (!$this->checkDowngrade($type, $adapter)) {
return false;
}
return true;
}
/**
* Function called after extension installation/update/removal procedure commences.
*
* @param string $type The type of change (install or discover_install, update, uninstall)
* @param InstallerAdapter $adapter The adapter calling this method
*
* @return boolean True on success
*
* @since 6.0.0
*/
public function postflight(string $type, InstallerAdapter $adapter): bool
{
$this->removeFiles();
return true;
}
/**
* Check if the extension passes the minimum requirements to be installed
*
* @return boolean True on success
*
* @since 6.0.0
*/
protected function checkCompatibility(string $type, InstallerAdapter $adapter): bool
{
// Check for the minimum PHP version before continuing
if (!empty($this->minimumPhp) && version_compare(PHP_VERSION, $this->minimumPhp, '<')) {
Log::add(Text::sprintf('JLIB_INSTALLER_MINIMUM_PHP', $this->minimumPhp), Log::WARNING, 'jerror');
return false;
}
// Check for the minimum Joomla version before continuing
if (!empty($this->minimumJoomla) && version_compare(JVERSION, $this->minimumJoomla, '<')) {
Log::add(Text::sprintf('JLIB_INSTALLER_MINIMUM_JOOMLA', $this->minimumJoomla), Log::WARNING, 'jerror');
return false;
}
return true;
}
/**
* Check if the extension is allowed to be downgraded
*
* @return boolean False when downgrade not allowed and new version is lower than old version otherwise true
*
* @since 6.0.0
*/
protected function checkDowngrade(string $type, InstallerAdapter $adapter): bool
{
if ($type !== 'update' || $this->allowDowngrades) {
return true;
}
$oldManifest = $this->getOldManifest($adapter);
if ($oldManifest !== null && $oldManifest->version && version_compare($oldManifest->version, $adapter->getManifest()->version, '>')) {
Log::add(Text::_('JLIB_INSTALLER_ERROR_DOWNGRADE'), Log::WARNING, 'jerror');
return false;
}
return true;
}
/**
* Returns the manifest file if it exists or null
*
* @param InstallerAdapter $adapter
*
* @return SimpleXMLElement|null
*
* @since 6.0.0
*/
protected function getOldManifest(InstallerAdapter $adapter): ?\SimpleXMLElement
{
$client = ApplicationHelper::getClientInfo('administrator', true);
$pathname = 'extension_' . $client->name;
$manifestPath = $adapter->getParent()->getPath($pathname) . '/' . $adapter->getParent()->getPath('manifest');
return is_file($manifestPath) ? $adapter->getParent()->isManifest($manifestPath) : null;
}
/**
* Remove the files and folders in the given array from
*
* @return void
*
* @since 6.0.0
*/
protected function removeFiles(): void
{
if (!empty($this->deleteFiles)) {
foreach ($this->deleteFiles as $file) {
if (is_file(Path::check(JPATH_ROOT . $file)) && !File::delete(JPATH_ROOT . $file)) {
Log::add(Text::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $file));
}
}
}
if (!empty($this->deleteFolders)) {
foreach ($this->deleteFolders as $folder) {
if (is_dir(Path::check(JPATH_ROOT . $folder)) && !Folder::delete(JPATH_ROOT . $folder)) {
Log::add(Text::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $folder));
}
}
}
}
/**
* Returns the internal application or null when not set.
*
* @return CMSApplicationInterface|null
*
* @since 6.0.0
*/
protected function getApplication(): ?CMSApplicationInterface
{
return $this->application;
}
/**
* Sets the application to use.
*
* @param CMSApplicationInterface $application The application
*
* @return void
*
* @since 6.0.0
*/
public function setApplication(CMSApplicationInterface $application): void
{
$this->application = $application;
}
/**
* Creates the dashboard menu module
*
* @param string $dashboard The name of the dashboard
* @param string $preset The name of the menu preset
*
* @return void
*
* @throws \Exception
* @since 6.0.0
*/
protected function addDashboardMenuModule(string $dashboard, string $preset)
{
$model = $this->getApplication()->bootComponent('com_modules')->getMVCFactory()->createModel('Module', 'Administrator', ['ignore_request' => true]);
$module = [
'id' => 0,
'asset_id' => 0,
'language' => '*',
'note' => '',
'published' => 1,
'assignment' => 0,
'client_id' => 1,
'showtitle' => 0,
'content' => '',
'module' => 'mod_submenu',
'position' => 'cpanel-' . $dashboard,
];
// Try to get a translated module title, otherwise fall back to a fixed string.
$titleKey = strtoupper('COM_' . $this->extension . '_DASHBOARD_' . $dashboard . '_TITLE');
$title = Text::_($titleKey);
$module['title'] = ($title === $titleKey) ? ucfirst($dashboard) . ' Dashboard' : $title;
$module['access'] = (int) $this->getApplication()->get('access', 1);
$module['params'] = [
'menutype' => '*',
'preset' => $preset,
'style' => 'System-none',
];
if (!$model->save($module)) {
$this->getApplication()->enqueueMessage(Text::sprintf('JLIB_INSTALLER_ERROR_COMP_INSTALL_FAILED_TO_CREATE_DASHBOARD', $model->getError()));
}
}
}
The same content is in 47 releases, Joomla 6.0.0-beta1 to 6.2.1-rc1. See them all, or every version of this file.