Skip to content

Commit 27cef1b

Browse files
authored
Add new InstallerScriptTrait
1 parent 52be53b commit 27cef1b

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

docs/building-extensions/install-update/installation/install-process.md

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ When you go to install extensions and select a zip file of an extension to insta
4848
## Example Script File
4949

5050
The easiest way to write a script file is to use the \Joomla\CMS\Installer\InstallerScriptInterface definition in libraries/src/Installer/InstallerScriptInterface.php.
51-
You simply have to return an instance of a class which implements the 5 installer functions.
51+
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.
5252

5353
You can either
5454
1. Return an anonymous Script File class which implements InstallerScriptInterface, or
@@ -64,6 +64,7 @@ For an example of the first approach see the [Module Tutorial Step 6](../../modu
6464
use Joomla\CMS\Application\AdministratorApplication;
6565
use Joomla\CMS\Installer\InstallerAdapter;
6666
use Joomla\CMS\Installer\InstallerScriptInterface;
67+
use Joomla\CMS\Installer\InstallerScriptTrait;
6768
use Joomla\CMS\Language\Text;
6869
use Joomla\Database\DatabaseInterface;
6970
use Joomla\DI\Container;
@@ -84,9 +85,39 @@ return new class () implements ServiceProviderInterface {
8485
$container->get(AdministratorApplication::class),
8586
$container->get(DatabaseInterface::class)
8687
) implements InstallerScriptInterface {
88+
use InstallerScriptTrait;
89+
8790
private AdministratorApplication $app;
8891
private DatabaseInterface $db;
8992

93+
/**
94+
* Minimum PHP version required to install the extension
95+
*
96+
* @var string
97+
*/
98+
protected $minimumPhp = '8.4';
99+
100+
/**
101+
* Minimum Joomla! version required to install the extension
102+
*
103+
* @var string
104+
*/
105+
protected $minimumJoomla = '6.0.0;
106+
107+
/**
108+
* A list of files to be deleted
109+
*
110+
* @var array
111+
*/
112+
protected $deleteFiles = [];
113+
114+
/**
115+
* A list of folders to be deleted
116+
*
117+
* @var array
118+
*/
119+
protected $deleteFolders = [];
120+
90121
public function __construct(AdministratorApplication $app, DatabaseInterface $db)
91122
{
92123
$this->app = $app;
@@ -114,33 +145,21 @@ return new class () implements ServiceProviderInterface {
114145
return true;
115146
}
116147

117-
public function preflight(string $type, InstallerAdapter $parent): bool
148+
public function customPreflight(string $type, InstallerAdapter $parent): bool
118149
{
119-
return true;
120-
}
150+
// Your custom preflight code
121151

122-
public function postflight(string $type, InstallerAdapter $parent): bool
123-
{
124-
$this->deleteUnexistingFiles();
152+
// Custom Dashboard Menu Module
153+
// $this->addDashboardMenuModule('example', 'example');
125154

126155
return true;
127156
}
128157

129-
private function deleteUnexistingFiles()
158+
public function customPostflight(string $type, InstallerAdapter $parent): bool
130159
{
131-
$files = []; // overwrite this line with your files to delete
132-
133-
if (empty($files)) {
134-
return;
135-
}
136-
137-
foreach ($files as $file) {
138-
try {
139-
File::delete(JPATH_ROOT . $file);
140-
} catch (\FilesystemException $e) {
141-
echo Text::sprintf('FILES_JOOMLA_ERROR_FILE_FOLDER', $file) . '<br>';
142-
}
143-
}
160+
// Your custom postflight code
161+
162+
return true;
144163
}
145164
}
146165
);

0 commit comments

Comments
 (0)