From 27cef1b0aff12d67144a8121fd609003469251c5 Mon Sep 17 00:00:00 2001 From: Benjamin Trenkle Date: Fri, 25 Apr 2025 21:16:54 +0200 Subject: [PATCH 1/3] Add new InstallerScriptTrait --- .../installation/install-process.md | 61 ++++++++++++------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/docs/building-extensions/install-update/installation/install-process.md b/docs/building-extensions/install-update/installation/install-process.md index 92f23fcd..e454f09c 100644 --- a/docs/building-extensions/install-update/installation/install-process.md +++ b/docs/building-extensions/install-update/installation/install-process.md @@ -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 @@ -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; @@ -84,9 +85,39 @@ return new class () implements ServiceProviderInterface { $container->get(AdministratorApplication::class), $container->get(DatabaseInterface::class) ) implements InstallerScriptInterface { + use InstallerScriptTrait; + 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; @@ -114,33 +145,21 @@ return new class () implements ServiceProviderInterface { return true; } - public function preflight(string $type, InstallerAdapter $parent): bool + public function customPreflight(string $type, InstallerAdapter $parent): bool { - return true; - } + // Your custom preflight code - public function postflight(string $type, InstallerAdapter $parent): bool - { - $this->deleteUnexistingFiles(); + // Custom Dashboard Menu Module + // $this->addDashboardMenuModule('example', 'example'); return true; } - private function deleteUnexistingFiles() + public function customPostflight(string $type, InstallerAdapter $parent): bool { - $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) . '
'; - } - } + // Your custom postflight code + + return true; } } ); From fd69391242249236555951ca09f48c3c96d04e12 Mon Sep 17 00:00:00 2001 From: Benjamin Trenkle Date: Fri, 25 Apr 2025 21:40:17 +0200 Subject: [PATCH 2/3] Update install-process.md --- .../install-update/installation/install-process.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/building-extensions/install-update/installation/install-process.md b/docs/building-extensions/install-update/installation/install-process.md index e454f09c..2d8bc2f3 100644 --- a/docs/building-extensions/install-update/installation/install-process.md +++ b/docs/building-extensions/install-update/installation/install-process.md @@ -102,7 +102,7 @@ return new class () implements ServiceProviderInterface { * * @var string */ - protected $minimumJoomla = '6.0.0; + protected $minimumJoomla = '6.0.0'; /** * A list of files to be deleted From 471980f47a089da4675f265a5a4180c1bb46a5ff Mon Sep 17 00:00:00 2001 From: Benjamin Trenkle Date: Sat, 26 Apr 2025 23:20:59 +0200 Subject: [PATCH 3/3] Update install-process.md --- .../installation/install-process.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/building-extensions/install-update/installation/install-process.md b/docs/building-extensions/install-update/installation/install-process.md index 2d8bc2f3..9f05c51e 100644 --- a/docs/building-extensions/install-update/installation/install-process.md +++ b/docs/building-extensions/install-update/installation/install-process.md @@ -85,7 +85,10 @@ return new class () implements ServiceProviderInterface { $container->get(AdministratorApplication::class), $container->get(DatabaseInterface::class) ) implements InstallerScriptInterface { - use InstallerScriptTrait; + use InstallerScriptTrait { + InstallerScriptTrait::preflight as iScriptTraitPre; + InstallerScriptTrait::postflight as iScriptTraitPost; + }; private AdministratorApplication $app; private DatabaseInterface $db; @@ -145,8 +148,11 @@ return new class () implements ServiceProviderInterface { return true; } - public function customPreflight(string $type, InstallerAdapter $parent): bool + public function preflight(string $type, InstallerAdapter $parent): bool { + // Run trait preflight code + $this->iScriptTraitPre(); + // Your custom preflight code // Custom Dashboard Menu Module @@ -155,8 +161,11 @@ return new class () implements ServiceProviderInterface { return true; } - public function customPostflight(string $type, InstallerAdapter $parent): bool + public function postflight(string $type, InstallerAdapter $parent): bool { + // Run trait postflight code + $this->iScriptTraitPost(); + // Your custom postflight code return true;