1. Go to this page and download the library: Download joomla/application 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/ */
joomla / application example snippets
use Joomla\Application\AbstractApplication;
use Joomla\Input\Input;
use Joomla\Registry\Registry;
class MyApplication extends AbstractApplication
{
/**
* Customer constructor for my application class.
*
* @param Input $input
* @param Registry $config
*
* @since 1.0
*/
public function __construct(Input $input = null, Registry $config = null, Foo $foo)
{
// Do some extra assignment.
$this->foo = $foo;
// Call the parent constructor last of all.
parent::__construct($input, $config);
}
/**
* Method to run the application routines.
*
* @return void
*
* @since 1.0
*/
protected function doExecute()
{
try
{
// Do stuff.
}
catch(\Exception $e)
{
// Set status header of exception code and response body of exception message
$this->setHeader('status', $e->getCode() ?: 500);
$this->setBody($e->getMessage());
}
}
/**
* Custom initialisation for my application.
*
* @return void
*
* @since 1.0
*/
protected function initialise()
{
// Do stuff.
// Note that configuration has been loaded.
}
}
use Joomla\Application\AbstractApplication;
use Monolog\Logger;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
class MyApplication extends AbstractApplication
{
/**
* Custom initialisation for my application.
*
* Note that configuration has been loaded.
*
* @return void
*
* @since 1.0
*/
protected function initialise()
{
// Get the file logging path from configuration.
$logPath = $this->get('logger.path');
$log = new Logger('MyApp');
if ($logPath)
{
// If the log path is set, configure a file logger.
$log->pushHandler(new StreamHandler($logPath, Logger::WARNING);
}
else
{
// If the log path is not set, just use a null logger.
$log->pushHandler(new NullHandler, Logger::WARNING);
}
$this->setLogger($logger);
}
}
use Joomla\Application\AbstractApplication;
class MyApplication extends AbstractApplication
{
protected function doExecute()
{
// In this case, we always want the logger set.
$this->getLogger()->logInfo('Performed this {task}', array('task' => $task));
// Or, in this case logging is optional, so we check if the logger is set first.
if ($this->get('debug') && $this->hasLogger())
{
$this->getLogger()->logDebug('Performed {task}', array('task' => $task));
}
}
}
use Joomla\Application\Tests\Mocker as AppMocker;
class MyTest extends \PHPUnit_Framework_TestCase
{
private $instance;
protected function setUp()
{
parent::setUp();
// Create the mock input object.
$appMocker = new AppMocker($this);
$mockApp = $appMocker->createMockWeb();
// Create the test instance injecting the mock dependency.
$this->instance = new MyClass($mockApp);
}
}
$app->setHeader('status', '401 Auhtorization
use Joomla\Application\AbstractCliApplication;
// Bootstrap the autoloader (adjust path as appropriate to your situation).
);
// Get user input
$this->out('What is your name? ', false);
$userInput = $this->in();
$this->out('Hello ' . $userInput);
}
}
$app = new MyCli;
$app->execute();
use Joomla\Application\AbstractCliApplication;
class MyCli extends AbstractCliApplication
{
protected function doExecute()
{
// Green text
$this->out('<info>foo</info>');
// Yellow text
$this->out('<comment>foo</comment>');
// Black text on a cyan background
$this->out('<question>foo</question>');
// White text on a red background
$this->out('<error>foo</error>');
}
}
use Joomla\Application\AbstractCliApplication;
use Joomla\Application\Cli\Colorstyle;
class MyCli extends AbstractCliApplication
{
/**
* Override to initialise the colour styles.
*
* @return void
*
* @since 1.0
*/
protected function initialise()
{
$style = new Colorstyle('yellow', 'red', array('bold', 'blink'));
$this->getOutput()->addStyle('fire', $style);
}
protected function doExecute()
{
$this->out('<fire>foo</fire>');
}
}
use Joomla\Application\AbstractCliApplication;
class MyCli extends AbstractCliApplication
{
protected function doExecute()
{
// Green text
$this->out('<fg=green>foo</fg=green>');
// Black text on a cyan background
$this->out('<fg=black;bg=cyan>foo</fg=black;bg=cyan>');
// Bold text on a yellow background
$this->out('<bg=yellow;options=bold>foo</bg=yellow;options=bold>');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.