PHP code example of asika / jconsole

1. Go to this page and download the library: Download asika/jconsole library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

asika / jconsole example snippets

 bash
cd your/joomla/path

php composer.phar create-project asika/jconsole libraries/jconsole 1.0.*
 bash
php cli/console
 php


// no direct access
defined('_JEXEC') or die;

class plgConsoleMycommand extends JPlugin
{
	/**
     * onConsoleLoadCommand Event, called when auto added command.
     *
     * @param   string                     $context  The command class, example: 'Command\\Build\\Indexmaker'.
     * @param   JConsole\Command\JCommand  $command  The parent command, You can addArgument to it.
     *
     * @return  void
     */
    public function onConsoleLoadCommand($context, $command)
    {
        if ($context != 'Command\\System\\System')
        {
            return;
        }

        /** @var $command JCommand */
        $command->addArgument(
            'mycommand',             // Command name
            'This is my command.',   // Description
            null,                    // Options

            // Executing code.
            function($command)
            {
                $command->out('Hello World');
            }
        );
    }
}
 bash
$ php cli/console system mycommand
 php

namespace Command\Mycommand;

use JConsole\Command\JCommand;

class Mycommand extends JCommand
{
	/**
	 * An enabled flag.
	 *
	 * @var bool
	 */
	public static $isEnabled = true;

	protected $name = 'mycommand';

	protected $description = 'This is mycommand.';

	protected function doExecute()
	{
		$this->out('Hello World.');

		return;
	}
}
 php
public function onConsoleLoadCommand($context, $command)
{
    if ($context != 'Command\\System\\System')
    {
        return;
    }

    // Add autoload to plugin folder
    JLoader::registerNamespace('Command', __DIR__);

    // Namespace 'Command\Mycommand\Mycommand` will auto match `Command/Mycommand/Mycommand.php` path.
    $command->addArgument(new Command\Mycommand\MyCommand);
}