Skip to content

Add new InstallerScriptTrait (PR #44381) #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ When you go to install extensions and select a zip file of an extension to insta
## Example Script File

The easiest way to write a script file is to use the \Joomla\CMS\Installer\InstallerScriptInterface definition in libraries/src/Installer/InstallerScriptInterface.php.
You simply have to return an instance of a class which implements the 5 installer functions.
You simply have to return an instance of a class which implements the 5 installer functions. You can use the \Joomla\CMS\Installer\InstallerScriptTrait trait from libraries/src/Installer/InstallerScriptTrait.php to benefit from pre-defined functionality like a PHP and Joomla minimum version check or creating a dashboard preset menu module.

You can either
1. Return an anonymous Script File class which implements InstallerScriptInterface, or
Expand All @@ -64,6 +64,7 @@ For an example of the first approach see the [Module Tutorial Step 6](../../modu
use Joomla\CMS\Application\AdministratorApplication;
use Joomla\CMS\Installer\InstallerAdapter;
use Joomla\CMS\Installer\InstallerScriptInterface;
use Joomla\CMS\Installer\InstallerScriptTrait;
use Joomla\CMS\Language\Text;
use Joomla\Database\DatabaseInterface;
use Joomla\DI\Container;
Expand All @@ -84,9 +85,42 @@ return new class () implements ServiceProviderInterface {
$container->get(AdministratorApplication::class),
$container->get(DatabaseInterface::class)
) implements InstallerScriptInterface {
use InstallerScriptTrait {
InstallerScriptTrait::preflight as iScriptTraitPre;
InstallerScriptTrait::postflight as iScriptTraitPost;
};

private AdministratorApplication $app;
private DatabaseInterface $db;

/**
* Minimum PHP version required to install the extension
*
* @var string
*/
protected $minimumPhp = '8.4';

/**
* Minimum Joomla! version required to install the extension
*
* @var string
*/
protected $minimumJoomla = '6.0.0';

/**
* A list of files to be deleted
*
* @var array
*/
protected $deleteFiles = [];

/**
* A list of folders to be deleted
*
* @var array
*/
protected $deleteFolders = [];

public function __construct(AdministratorApplication $app, DatabaseInterface $db)
{
$this->app = $app;
Expand Down Expand Up @@ -116,31 +150,25 @@ return new class () implements ServiceProviderInterface {

public function preflight(string $type, InstallerAdapter $parent): bool
{
// Run trait preflight code
$this->iScriptTraitPre();

// Your custom preflight code

// Custom Dashboard Menu Module
// $this->addDashboardMenuModule('example', 'example');

return true;
}

public function postflight(string $type, InstallerAdapter $parent): bool
{
$this->deleteUnexistingFiles();
// Run trait postflight code
$this->iScriptTraitPost();

return true;
}
// Your custom postflight code

private function deleteUnexistingFiles()
{
$files = []; // overwrite this line with your files to delete

if (empty($files)) {
return;
}

foreach ($files as $file) {
try {
File::delete(JPATH_ROOT . $file);
} catch (\FilesystemException $e) {
echo Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $file) . '<br>';
}
}
return true;
}
}
);
Expand Down