{"id":19357712,"url":"https://github.com/joomla-framework/application","last_synced_at":"2025-05-16T03:05:51.403Z","repository":{"id":7094423,"uuid":"8385701","full_name":"joomla-framework/application","owner":"joomla-framework","description":"Joomla Framework Application Package","archived":false,"fork":false,"pushed_at":"2024-12-11T07:16:32.000Z","size":641,"stargazers_count":23,"open_issues_count":6,"forks_count":32,"subscribers_count":20,"default_branch":"3.x-dev","last_synced_at":"2025-04-08T13:13:05.730Z","etag":null,"topics":["application","joomla","joomla-framework","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joomla-framework.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"joomla","custom":"https://community.joomla.org/sponsorship-campaigns.html"}},"created_at":"2013-02-24T03:13:22.000Z","updated_at":"2025-03-29T03:25:50.000Z","dependencies_parsed_at":"2024-06-18T12:42:40.773Z","dependency_job_id":"6ac3f1e7-d811-46fb-ab7a-f1d1de6ee899","html_url":"https://github.com/joomla-framework/application","commit_stats":{"total_commits":350,"total_committers":40,"mean_commits":8.75,"dds":0.7028571428571428,"last_synced_commit":"5cd9232325767893b405779ac4a41abdf6314890"},"previous_names":[],"tags_count":35,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fapplication","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fapplication/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fapplication/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joomla-framework%2Fapplication/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joomla-framework","download_url":"https://codeload.github.com/joomla-framework/application/tar.gz/refs/heads/3.x-dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254459088,"owners_count":22074605,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["application","joomla","joomla-framework","php"],"created_at":"2024-11-10T07:08:56.485Z","updated_at":"2025-05-16T03:05:46.393Z","avatar_url":"https://github.com/joomla-framework.png","language":"PHP","funding_links":["https://github.com/sponsors/joomla","https://community.joomla.org/sponsorship-campaigns.html"],"categories":[],"sub_categories":[],"readme":"# The Application Package [![Build Status](https://ci.joomla.org/api/badges/joomla-framework/application/status.svg?ref=refs/heads/3.x-dev)](https://ci.joomla.org/joomla-framework/application)\n\n[![Latest Stable Version](https://poser.pugx.org/joomla/application/v/stable.svg)](https://packagist.org/packages/joomla/application)\n[![Total Downloads](https://poser.pugx.org/joomla/application/downloads.svg)](https://packagist.org/packages/joomla/application)\n[![Latest Unstable Version](https://poser.pugx.org/joomla/application/v/unstable.svg)](https://packagist.org/packages/joomla/application)\n[![License](https://poser.pugx.org/joomla/application/license.svg)](https://packagist.org/packages/joomla/application)\n\n## Initialising Applications\n\n`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.\n\nIf you are overriding the `__construct` method in your application class, remember to call the parent constructor last.\n\n```php\nuse Joomla\\Application\\AbstractApplication;\nuse Joomla\\Input\\Input;\nuse Joomla\\Registry\\Registry;\n\nclass MyApplication extends AbstractApplication\n{\n\t/**\n\t * Customer constructor for my application class.\n\t *\n\t * @param   Input     $input\n\t * @param   Registry  $config\n\t *\n\t * @since   1.0\n\t */\n\tpublic function __construct(Input $input = null, Registry $config = null, Foo $foo)\n\t{\n\t\t// Do some extra assignment.\n\t\t$this-\u003efoo = $foo;\n\n\t\t// Call the parent constructor last of all.\n\t\tparent::__construct($input, $config);\n\t}\n\n\t/**\n\t * Method to run the application routines.\n\t *\n\t * @return  void\n\t *\n\t * @since   1.0\n\t */\n\tprotected function doExecute()\n\t{\n\t\ttry\n\t\t{\n\t\t\t// Do stuff.\n\t\t}\n\t\tcatch(\\Exception $e)\n\t\t{\n\t\t\t// Set status header of exception code and response body of exception message\n\t\t\t$this-\u003esetHeader('status', $e-\u003egetCode() ?: 500);\n\t\t\t$this-\u003esetBody($e-\u003egetMessage());\n\t\t}\n\t}\n\n\t/**\n\t * Custom initialisation for my application.\n\t *\n\t * @return  void\n\t *\n\t * @since   1.0\n\t */\n\tprotected function initialise()\n\t{\n\t\t// Do stuff.\n\t\t// Note that configuration has been loaded.\n\t}\n}\n\n```\n\n## Logging within Applications\n\n`AbstractApplication` implements the `Psr\\Log\\LoggerAwareInterface` so is ready for intergrating with an logging package that supports that standard.\n\nThe following example shows how you could set up logging in your application using `initialise` method from `AbstractApplication`.\n\n```php\nuse Joomla\\Application\\AbstractApplication;\nuse Monolog\\Logger;\nuse Monolog\\Handler\\NullHandler;\nuse Monolog\\Handler\\StreamHandler;\n\nclass MyApplication extends AbstractApplication\n{\n\t/**\n\t * Custom initialisation for my application.\n\t *\n\t * Note that configuration has been loaded.\n\t *\n\t * @return  void\n\t *\n\t * @since   1.0\n\t */\n\tprotected function initialise()\n\t{\n\t\t// Get the file logging path from configuration.\n\t\t$logPath = $this-\u003eget('logger.path');\n\t\t$log = new Logger('MyApp');\n\n\t\tif ($logPath)\n\t\t{\n\t\t\t// If the log path is set, configure a file logger.\n\t\t\t$log-\u003epushHandler(new StreamHandler($logPath, Logger::WARNING);\n\t\t}\n\t\telse\n\t\t{\n\t\t\t// If the log path is not set, just use a null logger.\n\t\t\t$log-\u003epushHandler(new NullHandler, Logger::WARNING);\n\t\t}\n\n\t\t$this-\u003esetLogger($logger);\n\t}\n}\n\n```\n\nThe 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.\n\nTo check if the logger has been set, use the `hasLogger` method. This will return `true` if the logger has been set.\n\nConsider the following example:\n\n```php\nuse Joomla\\Application\\AbstractApplication;\n\nclass MyApplication extends AbstractApplication\n{\n\tprotected function doExecute()\n\t{\n\t\t// In this case, we always want the logger set.\n\t\t$this-\u003egetLogger()-\u003elogInfo('Performed this {task}', array('task' =\u003e $task));\n\n\t\t// Or, in this case logging is optional, so we check if the logger is set first.\n\t\tif ($this-\u003eget('debug') \u0026\u0026 $this-\u003ehasLogger())\n\t\t{\n\t\t\t$this-\u003egetLogger()-\u003elogDebug('Performed {task}', array('task' =\u003e $task));\n\t\t}\n\t}\n}\n```\n\n## Mocking the Application Package\n\nFor more complicated mocking where you need to similate real behaviour, you can use the `Application\\Tests\\Mocker` class to create robust mock objects.\n\nThere are three mocking methods available:\n\n1. `createMockBase` will create a mock for `AbstractApplication`.\n2. `createMockCli` will create a mock for `AbstractCliApplication`.\n3. `createMockWeb` will create a mock for `AbstractWebApplication`.\n\n```php\nuse Joomla\\Application\\Tests\\Mocker as AppMocker;\n\nclass MyTest extends \\PHPUnit_Framework_TestCase\n{\n\tprivate $instance;\n\n\tprotected function setUp()\n\t{\n\t\tparent::setUp();\n\n\t\t// Create the mock input object.\n\t\t$appMocker = new AppMocker($this);\n\t\t$mockApp = $appMocker-\u003ecreateMockWeb();\n\n\t\t// Create the test instance injecting the mock dependency.\n\t\t$this-\u003einstance = new MyClass($mockApp);\n\t}\n}\n```\n\nThe `createMockWeb` method will return a mock with the following methods mocked to roughly simulate real behaviour albeit with reduced functionality:\n\n* `appendBody($content)`\n* `get($name [, $default])`\n* `getBody([$asArray])`\n* `getHeaders()`\n* `prependBody($content)`\n* `set($name, $value)`\n* `setBody($content)`\n* `setHeader($name, $value [, $replace])`\n\nYou can provide customised implementations these methods by creating the following methods in your test class respectively:\n\n* `mockWebAppendBody`\n* `mockWebGet`\n* `mockWebGetBody`\n* `mockWebGetHeaders`\n* `mockWebSet`\n* `mockWebSetBody`\n* `mockWebSetHeader`\n\n\n## Web Application\n\n### Configuration options\n\nThe `AbstractWebApplication` sets following application configuration:\n\n- Execution datetime and timestamp\n  - `execution.datetime` - Execution datetime\n  - `execution.timestamp` - Execution timestamp\n\n- URIs\n  - `uri.request` - The request URI\n  - `uri.base.full` - full URI\n  - `uri.base.host` - URI host\n  - `uri.base.path` - URI path\n  - `uri.route` - Extended (non-base) part of the request URI\n  - `uri.media.full` - full media URI\n  - `uri.media.path` - relative media URI\n\nand uses following ones during object construction:\n\n- `gzip` to compress the output\n- `site_uri` to see if an explicit base URI has been set\n  (helpful when chaining request uri using mod_rewrite)\n- `media_uri` to get an explicitly set media URI (relative values are appended to `uri.base` ).\n  If it's not set explicitly, it defaults to a `media/` path of `uri.base`.\n\n#### The `setHeader` method\n__Accepted parameters__\n\n- `$name` - The name of the header to set.\n- `$value` - The value of the header to set.\n- `$replace` - True to replace any headers with the same name.\n\nExample: Using `WebApplication::setHeader` to set a status header.\n\n```PHP\n$app-\u003esetHeader('status', '401 Auhtorization required', true);\n```\n\nWill result in response containing header\n```\nStatus Code: 401 Authorization required\n```\n\n## Command Line Applications\n\nThe Joomla Framework provides an application class for making command line applications.\n\nAn example command line application skeleton:\n\n```php\nuse Joomla\\Application\\AbstractCliApplication;\n\n// Bootstrap the autoloader (adjust path as appropriate to your situation).\nrequire_once __DIR__ . '/../vendor/autoload.php';\n\nclass MyCli extends AbstractCliApplication\n{\n\tprotected function doExecute()\n\t{\n\t\t// Output string\n\t\t$this-\u003eout('It works');\n\n\t\t// Get user input\n\t\t$this-\u003eout('What is your name? ', false);\n\n\t\t$userInput = $this-\u003ein();\n\t\t$this-\u003eout('Hello ' . $userInput);\n\t}\n}\n\n$app = new MyCli;\n$app-\u003eexecute();\n\n```\n\n### Colors for CLI Applications\n\nIt is possible to use colors on an ANSI enabled terminal.\n\n```php\nuse Joomla\\Application\\AbstractCliApplication;\n\nclass MyCli extends AbstractCliApplication\n{\n\tprotected function doExecute()\n\t{\n\t\t// Green text\n\t\t$this-\u003eout('\u003cinfo\u003efoo\u003c/info\u003e');\n\n\t\t// Yellow text\n\t\t$this-\u003eout('\u003ccomment\u003efoo\u003c/comment\u003e');\n\n\t\t// Black text on a cyan background\n\t\t$this-\u003eout('\u003cquestion\u003efoo\u003c/question\u003e');\n\n\t\t// White text on a red background\n\t\t$this-\u003eout('\u003cerror\u003efoo\u003c/error\u003e');\n\t}\n}\n```\n\nYou can also create your own styles.\n\n```php\nuse Joomla\\Application\\AbstractCliApplication;\nuse Joomla\\Application\\Cli\\Colorstyle;\n\nclass MyCli extends AbstractCliApplication\n{\n\t/**\n\t * Override to initialise the colour styles.\n\t *\n\t * @return  void\n\t *\n\t * @since   1.0\n\t */\n\tprotected function initialise()\n\t{\n\t\t$style = new Colorstyle('yellow', 'red', array('bold', 'blink'));\n\t\t$this-\u003egetOutput()-\u003eaddStyle('fire', $style);\n\t}\n\n\tprotected function doExecute()\n\t{\n\t\t$this-\u003eout('\u003cfire\u003efoo\u003c/fire\u003e');\n\t}\n}\n\n```\n\nAvailable foreground and background colors are: black, red, green, yellow, blue, magenta, cyan and white.\n\nAnd available options are: bold, underscore, blink and reverse.\n\nYou can also set these colors and options inside the tagname:\n\n```php\nuse Joomla\\Application\\AbstractCliApplication;\n\nclass MyCli extends AbstractCliApplication\n{\n\tprotected function doExecute()\n\t{\n\t\t// Green text\n\t\t$this-\u003eout('\u003cfg=green\u003efoo\u003c/fg=green\u003e');\n\n\t\t// Black text on a cyan background\n\t\t$this-\u003eout('\u003cfg=black;bg=cyan\u003efoo\u003c/fg=black;bg=cyan\u003e');\n\n\t\t// Bold text on a yellow background\n\t\t$this-\u003eout('\u003cbg=yellow;options=bold\u003efoo\u003c/bg=yellow;options=bold\u003e');\n\t}\n}\n```\n\n## Installation via Composer\n\nAdd `\"joomla/application\": \"~3.0\"` to the require block in your composer.json and then run `composer install`.\n\n```json\n{\n\t\"require\": {\n\t\t\"joomla/application\": \"~3.0\"\n\t}\n}\n```\n\nAlternatively, you can simply run the following from the command line:\n\n```sh\ncomposer require joomla/application \"~3.0\"\n```\n\nIf you want to include the test sources, use\n\n```sh\ncomposer require --prefer-source joomla/application \"~3.0\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Fapplication","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoomla-framework%2Fapplication","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoomla-framework%2Fapplication/lists"}