0

I have a component that needs a CLI file for use with cron. Is there a proper way to use the components manifest file to specify that the component includes a CLI file and that CLI file should be placed into the root CLI directory?

I know that I could easily do this via using the install stub in my components script.php file and just directly copy my file into the correct location, but I was wondering if there was a proper way to do this using the manifest file?

1

Use file manifest to install files not belonging to any extension:

<?xml version="1.0" encoding="UTF-8"?>
<extension version="3.9" type="file" method="upgrade">
    <name>files_cli_example</name>
    <author>Example</author>
    <version>1.0.0</version>
    <creationDate>January 2019</creationDate>
    <description>FILES_CLI_EXAMPLE_XML_DESCRIPTION</description>
    <fileset>
        <files target="cli">
            <file>cli_example.php</file>
        </files>
    </fileset>
</extension>

Create a Package to install the component and custom files at once.

  • Thanks I wasn't aware of the extension type 'file', that's very useful. The CLI file is very much part of my extension, so separating it into a separate extension within a package is less than optimal as Joomla doesn't yet have a package dependency mechanism. Admin's would be able to uninstall either the component or cli file, but leave the other on the system. At best, this would leave the system in inconsistent state and at worse it would start throwing errors. Ideally I was hoping for something like the <media> node, which allows a component to place files outside of its own directories. – Dom Jan 30 at 10:53
0

In the end I went with the script method I'd suggested in my question. Id did this because it meant that I could keep the cli file within the component, add it to the correct location when the component installed and remove it on uninstall.

I did this by adding a new cli folder to the root of my component, which contained the cli file I wanted to add com_mycomponent_cron.php.

Then in the script file, pointed to by the <scriptfile>script.php</scriptfile> in the components manifest xml, I added the following to both the install and update methods.

function install($parent)
{
    $source = \JPath::clean($parent->getParent()->getPath('source') . '/cli/com_mycomponent_cron.php');
    if ( !file_exists($source) ) {
        throw new RuntimeException('File cli/com_mycomponent_cron.php does not exist', 500);
    }
    $dest = \JPath::clean(JPATH_ROOT . '/cli/com_mycomponent_cron.php');

    //attempt to copy the file
    if ( !\JFile::copy($source,$dest) ) {
        throw new RuntimeException('Could not copy com_mycomponent_cron.php to cli folder.',500);

    }
}

and for the uninstall method I have this:

function uninstall($parent)
{
    $cli_script_path = \JPath::clean(JPATH_ROOT . '/cli/com_mycomponent_cron.php');
    if ( file_exists($cli_script_path) ) {
        \JFile::delete($cli_script_path);
    }
}

I've decided not to accept @Sharky's answer or mark my own as correct, as I don't think either are the 'correct' Joomla way to do things. Although I suspect that there probably isn't a 'correct' way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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