https://github.com/rancoud/application
Application Package
https://github.com/rancoud/application
application composer coverage database env environment http packagist php php7 php8 phpdotenv phpunit psr-15 psr-7 router session skeleton
Last synced: 2 months ago
JSON representation
Application Package
- Host: GitHub
- URL: https://github.com/rancoud/application
- Owner: rancoud
- License: mit
- Created: 2018-08-15T14:05:27.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-08-21T06:30:22.000Z (2 months ago)
- Last Synced: 2025-08-21T09:23:47.633Z (2 months ago)
- Topics: application, composer, coverage, database, env, environment, http, packagist, php, php7, php8, phpdotenv, phpunit, psr-15, psr-7, router, session, skeleton
- Language: PHP
- Homepage: https://packagist.org/packages/rancoud/application
- Size: 641 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
- Security: SECURITY.md
Awesome Lists containing this project
README
# Application Package

[](https://packagist.org/packages/rancoud/application)
[](https://packagist.org/packages/rancoud/application)
[](https://github.com/rancoud/application/blob/master/composer.json)
[](https://github.com/rancoud/application/actions/workflows/test.yml)
[](https://codecov.io/gh/rancoud/application)
Application skeleton with strict minimum Router and Environment.
## Dependencies
Environment package: [https://github.com/rancoud/Environment](https://github.com/rancoud/Environment)
Router package: [https://github.com/rancoud/Router](https://github.com/rancoud/Router)
## Installation
```php
composer require rancoud/application
```
## How to use it?
### General
#### Requirements
You need `.env` file and route file called for example `routes.php`
Content of default `.env` (all values are optionals, you can use an empty `.env` file if you want)
```dotenv
# setup timezone, by default use UTC (valid timezones are checked against DateTimeZone::listIdentifiers())
TIMEZONE=null
# file names that contain route configurations, if null it will load all files in current folder provided to the Application constructor
ROUTES=null
# to enable all DEBUG_* parameters
DEBUG=false
# enable error_reporting: show php errors
DEBUG_PHP=false
# save PSR-7 request object for Application->getDebugInfos()
DEBUG_REQUEST=false
# save PSR-7 response object for Application->getDebugInfos()
DEBUG_RESPONSE=false
# save queries for Application->getDebugInfos()
DEBUG_DATABASE=false
# add all values of Session object to Application->getDebugInfos()
DEBUG_SESSION=false
# add memory usage/limit/percentage to Application->getDebugInfos()
DEBUG_MEMORY=false
# save elapsed time of each call of Application->run() for Application->getDebugInfos()
DEBUG_RUN_ELAPSED_TIMES=false
# add all included files included to Application->getDebugInfos()
DEBUG_INCLUDED_FILES=false
```
`DEBUG_*` infos are available with `Application->getDebugInfos();` (except `DEBUG_PHP`)
Content of `routes.php`
```php
any('/home', static function ($request, $next) {
return (new Factory())->createResponse()->withBody(Stream::create('hello world'));
});
```
#### Usage
```php
// you have to set thoses required folders
$folders = [
'ROOT' => 'folder/path/of/env', // ROOT is used to read the .env file (must be a folder)
'ROUTES' => 'folder/path/of/routes' // ROUTES is used to initialize the router with routes (must be a folder, so it will read php files inside it)
];
$app = new Application($folders);
// create server request from globals
$request = (new \Rancoud\Http\Message\Factory\Factory())->createServerRequestFromGlobals();
// you can also create request from scratch for example
// $request = new \Rancoud\Http\Message\ServerRequest('GET', '/home');
// $response can be null if no route AND no default404 has been found by the Router
$response = $app->run($request);
// you can send response output directly (if not null of course)
$response->send();
```
You can add more folders in constructor
```php
$folders = [
'ROOT' => 'folder/path/of/env',
'ROUTES' => 'folder/path/of/routes',
'EXTRA' => 'folder/path/of/extra'
];
$app = new Application($folders);
// you can access to specific folders
$folderExtra = Application::getFolder('EXTRA');
```
### Environment File
You can specify another environment file instead of using .env in ROOT folder
```php
$env = new Environment(['folder/path/of/env'], 'another.env');
$app = new Application($folders, $env);
// you can access to the environment
$config = Application::getConfig();
```
### Routes
By default it will load all php files in the ROUTES folder.
You can specify in .env file which routes files you want to use.
```dotenv
# in this example, it will require() 3 files: www.php , backoffice.php and api.php in the routes folder
ROUTES=www,backoffice,api
```
This example show how to add routes for the router
```php
$config = [
'routes' => [
[
'methods' => ['GET'],
'url' => '/',
'callback' => function($a,$b){
return (new MessageFactory())->createResponse(200, null, [], 'home');
},
'name' => 'test_home'
]
]
];
/* @var \Rancoud\Router\Router $router */
$router->setupRouterAndRoutesWithConfigArray($config);
```
### Router
```php
$app = new Application($folders, $env);
$router = Application::getRouter();
```
### Database
You have to use this [Database package](https://github.com/rancoud/Database)
```php
$app = new Application($folders, $env);
Application::setDatabase($database);
$db = Application::getDatabase();
$infos = $app->getDebugInfos();
// when enabled, all saved queries will be display in $infos['database']
```
You are free to use something else if you don't use functions `setDatabase` and `getDatabase`.
### Session
You can use this [Session package](https://github.com/rancoud/Session)
It is used in the function `getDebugInfos()`
You are free to use something else.
### Timezone
By default the timezone used will be UTC
You can specify a timezone in .env file
```dotenv
# valid timezones are checked with DateTimeZone::listIdentifiers()
TIMEZONE="Europe/Paris"
```
### Debug Infos
You have to enable it in .env file
```dotenv
# to enable all DEBUG_* parameters
DEBUG=true
# enable error_reporting: show php errors
DEBUG_PHP=true
# save PSR-7 request object for Application->getDebugInfos()
DEBUG_REQUEST=true
# save PSR-7 response object for Application->getDebugInfos()
DEBUG_RESPONSE=true
# save queries for Application->getDebugInfos()
DEBUG_DATABASE=true
# add all values of Session object to Application->getDebugInfos()
DEBUG_SESSION=true
# add memory usage/limit/percentage to Application->getDebugInfos()
DEBUG_MEMORY=true
# save elapsed time of each call of Application->run() for Application->getDebugInfos()
DEBUG_RUN_ELAPSED_TIMES=true
# add all included files included to Application->getDebugInfos()
DEBUG_INCLUDED_FILES=true
```
### Bags
You can put whatever you want in "bags", like your own database driver
```php
$app = new Application($folders, $env);
Application::setInBag('db', $mydb);
$dbInBag = Application::getFromBag('db');
Application::removeFromBag('db');
```
## Application Constructor
### Settings
#### Mandatory
| Parameter | Type | Description |
|-----------|-------|----------------------------------------------|
| folders | array | Folder's list. ROOT and ROUTES are mandatory |
#### Optionals
| Parameter | Type | Default value | Description |
|-----------|---------------------------------|---------------|-----------------------------|
| env | Rancoud\Environment\Environment | null | Setup a different .env file |
## Application Methods
### General Commands
* run(request: \Psr\Http\Message\ServerRequestInterface): ?\Rancoud\Http\Message\Response
* getDebugInfos(): array
### Static Methods
* getFolder(index: string): string
* getInstance(): Rancoud\Application\Application
* getConfig(): Rancoud\Environment\Environment
* setDatabase(database: Rancoud\Database\Database): void
* getDatabase(): ?Rancoud\Database\Database
* getRouter(): Rancoud\Router\Router
* getFromBag(name: string): mixed
* removeFromBag(name: string): void
* setInBag(name: string, object: mixed): void
## Optionals Dependencies
[Database package: https://github.com/rancoud/Database](https://github.com/rancoud/Database)
[Session package: https://github.com/rancoud/Session](https://github.com/rancoud/Session)
## How to Dev
`composer ci` for php-cs-fixer and phpunit and coverage
`composer lint` for php-cs-fixer
`composer test` for phpunit and coverage