joomla / application
Joomla Application Package
Fund package maintenance!
joomla
community.joomla.org/sponsorship-campaigns.html
Installs: 295 227
Dependents: 18
Suggesters: 2
Security: 1
Stars: 23
Watchers: 21
Forks: 31
Open Issues: 6
Requires
- php: ^8.1.0
- joomla/event: ^3.0
- joomla/registry: ^3.0
- laminas/laminas-diactoros: ^2.24.0
- psr/http-message: ^1.0
- psr/log: ^1.0|^2.0|^3.0
- symfony/deprecation-contracts: ^2|^3
Requires (Dev)
- ext-json: *
- joomla/controller: ^3.0
- joomla/di: ^3.0
- joomla/input: ^3.0
- joomla/router: ^3.0
- joomla/session: ^3.0
- joomla/test: ^3.0
- joomla/uri: ^3.0
- phan/phan: ^5.4
- phpstan/phpstan: ^1.10.7
- phpunit/phpunit: ^10.0
- rector/rector: ^1.0
- squizlabs/php_codesniffer: ~3.10.2
- symfony/phpunit-bridge: ^7.0
Suggests
- ext-json: To use JSON format, ext-json is required
- joomla/controller: ^3.0 To support resolving ControllerInterface objects in ControllerResolverInterface, install joomla/controller
- joomla/input: ^3.0 To use WebApplicationInterface, install joomla/input
- joomla/router: ^3.0 To use WebApplication or ControllerResolverInterface implementations, install joomla/router
- joomla/session: ^3.0 To use SessionAwareWebApplicationInterface, install joomla/session
- joomla/uri: ^3.0 To use AbstractWebApplication, install joomla/uri
- psr/container: ^1.0 To use the ContainerControllerResolver, install any PSR-11 compatible container
- dev-main
- dev-3.x-dev / 3.0.x-dev
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- dev-2.0-dev / 2.0.x-dev
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-rc
- 2.0.0-beta
- 1.9.3
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.1
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0
- 1.0-beta3
- 1.0-beta2
- 1.0-beta
- 1.0-alpha
- dev-4.x/dep-update
- dev-4.x-dev
- dev-improve-bailouts
- dev-1.x-dev
This package is auto-updated.
Last update: 2024-12-11 07:16:35 UTC
README
Initialising Applications
AbstractApplication
implements an initialise
method that is called at the end of the constructor. This method is intended to be overridden in derived classes as needed by the developer.
If you are overriding the __construct
method in your application class, remember to call the parent constructor last.
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. } }
Logging within Applications
AbstractApplication
implements the Psr\Log\LoggerAwareInterface
so is ready for intergrating with an logging package that supports that standard.
The following example shows how you could set up logging in your application using initialise
method from AbstractApplication
.
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); } }
The logger variable is private so you must use the getLogger
method to access it. If a logger has not been initialised, the getLogger
method will throw an exception.
To check if the logger has been set, use the hasLogger
method. This will return true
if the logger has been set.
Consider the following example:
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)); } } }
Mocking the Application Package
For more complicated mocking where you need to similate real behaviour, you can use the Application\Tests\Mocker
class to create robust mock objects.
There are three mocking methods available:
createMockBase
will create a mock forAbstractApplication
.createMockCli
will create a mock forAbstractCliApplication
.createMockWeb
will create a mock forAbstractWebApplication
.
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); } }
The createMockWeb
method will return a mock with the following methods mocked to roughly simulate real behaviour albeit with reduced functionality:
appendBody($content)
get($name [, $default])
getBody([$asArray])
getHeaders()
prependBody($content)
set($name, $value)
setBody($content)
setHeader($name, $value [, $replace])
You can provide customised implementations these methods by creating the following methods in your test class respectively:
mockWebAppendBody
mockWebGet
mockWebGetBody
mockWebGetHeaders
mockWebSet
mockWebSetBody
mockWebSetHeader
Web Application
Configuration options
The AbstractWebApplication
sets following application configuration:
-
Execution datetime and timestamp
execution.datetime
- Execution datetimeexecution.timestamp
- Execution timestamp
-
URIs
uri.request
- The request URIuri.base.full
- full URIuri.base.host
- URI hosturi.base.path
- URI pathuri.route
- Extended (non-base) part of the request URIuri.media.full
- full media URIuri.media.path
- relative media URI
and uses following ones during object construction:
gzip
to compress the outputsite_uri
to see if an explicit base URI has been set (helpful when chaining request uri using mod_rewrite)media_uri
to get an explicitly set media URI (relative values are appended touri.base
). If it's not set explicitly, it defaults to amedia/
path ofuri.base
.
The setHeader
method
Accepted parameters
$name
- The name of the header to set.$value
- The value of the header to set.$replace
- True to replace any headers with the same name.
Example: Using WebApplication::setHeader
to set a status header.
$app->setHeader('status', '401 Auhtorization required', true);
Will result in response containing header
Status Code: 401 Authorization required
Command Line Applications
The Joomla Framework provides an application class for making command line applications.
An example command line application skeleton:
use Joomla\Application\AbstractCliApplication; // Bootstrap the autoloader (adjust path as appropriate to your situation). require_once __DIR__ . '/../vendor/autoload.php'; class MyCli extends AbstractCliApplication { protected function doExecute() { // Output string $this->out('It works'); // Get user input $this->out('What is your name? ', false); $userInput = $this->in(); $this->out('Hello ' . $userInput); } } $app = new MyCli; $app->execute();
Colors for CLI Applications
It is possible to use colors on an ANSI enabled terminal.
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>'); } }
You can also create your own styles.
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>'); } }
Available foreground and background colors are: black, red, green, yellow, blue, magenta, cyan and white.
And available options are: bold, underscore, blink and reverse.
You can also set these colors and options inside the tagname:
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>'); } }
Installation via Composer
Add "joomla/application": "~3.0"
to the require block in your composer.json and then run composer install
.
{ "require": { "joomla/application": "~3.0" } }
Alternatively, you can simply run the following from the command line:
composer require joomla/application "~3.0"
If you want to include the test sources, use
composer require --prefer-source joomla/application "~3.0"