Back to Installer class

Method parseMedia

public bool
parseMedia
(\SimpleXMLElement $element, mixed $cid = 0)
Method to parse through a media element of the installation manifest and take appropriate action.
Parameters
  • \SimpleXMLElement $element The XML node to process
  • int $cid Application ID of application to install to
Returns
  • bool True on success
Since
  • 3.1
Class: Installer
Project: Joomla

Method parseMedia - Source code

/**
 * Method to parse through a media element of the installation manifest and take appropriate
 * action.
 *
 * @param   \SimpleXMLElement  $element  The XML node to process
 * @param   integer            $cid      Application ID of application to install to
 *
 * @return  boolean     True on success
 *
 * @since   3.1
 */
public function parseMedia(\SimpleXMLElement $element, $cid = 0)
{
    if (!$element || !\count($element->children())) {
        // Either the tag does not exist or has no children therefore we return zero files processed.
        return 0;
    }
    $copyfiles = array();
    // Here we set the folder we are going to copy the files to.
    // Default 'media' Files are copied to the JPATH_BASE/media folder
    $folder = (string) $element->attributes()->destination ? '/' . $element->attributes()->destination : null;
    $destination = Path::clean(JPATH_ROOT . '/media' . $folder);
    // Here we set the folder we are going to copy the files from.
    /*
     * Does the element have a folder attribute?
     * If so this indicates that the files are in a subdirectory of the source
     * folder and we should append the folder attribute to the source path when
     * copying files.
     */
    $folder = (string) $element->attributes()->folder;
    if ($folder && file_exists($this->getPath('source') . '/' . $folder)) {
        $source = $this->getPath('source') . '/' . $folder;
    } else {
        $source = $this->getPath('source');
    }
    // Process each file in the $files array (children of $tagName).
    foreach ($element->children() as $file) {
        $path['src'] = $source . '/' . $file;
        $path['dest'] = $destination . '/' . $file;
        // Is this path a file or folder?
        $path['type'] = $file->getName() === 'folder' ? 'folder' : 'file';
        /*
         * Before we can add a file to the copyfiles array we need to ensure
         * that the folder we are copying our file to exists and if it doesn't,
         * we need to create it.
         */
        if (basename($path['dest']) !== $path['dest']) {
            $newdir = \dirname($path['dest']);
            if (!Folder::create($newdir)) {
                Log::add(Text::sprintf('JLIB_INSTALLER_ABORT_CREATE_DIRECTORY', Text::_('JLIB_INSTALLER_INSTALL'), $newdir), Log::WARNING, 'jerror');
                return false;
            }
        }
        // Add the file to the copyfiles array
        $copyfiles[] = $path;
    }
    return $this->copyFiles($copyfiles);
}