0

The install script can delete obsolete files by creating as a protected property an array called $deleteFiles containing relative paths, and calling the inherited method removeFiles().

However, this ignores any entries that are not files. Is there a way to remove folders, including their contents?

2 Answers 2

1

It sounds like you're talking about Joomla\CMS\Installer\InstallerScript class. If so, it does have $deleteFolders class property which you can populate with directories. Directories and their contents will be deleted when removeFiles() method is called.

0
0

Is the class and property @Sharky comment before. But, in my case I never get this property work, not matter what I put in those array.

So, using the preflight method of the Joomla\CMS\Installer\InstallerScript class, I copy the exact logic of the removeFiles method and it work. This always confuse me, but is what so far I need do for deal with this.

So, at the end, the method look like this:

/**
     * 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   4.2.0
     */
    public function preflight($type, $adapter): bool
    {
        if (!empty($this->deleteFiles)) {
            foreach ($this->deleteFiles as $file) {
                if (file_exists(JPATH_ROOT . $file) && !File::delete(JPATH_ROOT . $file)) {
                    echo Text::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $file) . '<br>';
                }
            }
        }

        if (!empty($this->deleteFolders)) {
            foreach ($this->deleteFolders as $folder) {
                if (Folder::exists(JPATH_ROOT . $folder) && !Folder::delete(JPATH_ROOT . $folder)) {
                    echo Text::sprintf('JLIB_INSTALLER_ERROR_FILE_FOLDER', $folder) . '<br>';
                }
            }
        }
        
        return true;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.