https://github.com/windwalker-io/application
[DEPRECATED] Application package for Windwalker.
https://github.com/windwalker-io/application
application cli-app cli-application psr7-handler server
Last synced: 9 months ago
JSON representation
[DEPRECATED] Application package for Windwalker.
- Host: GitHub
- URL: https://github.com/windwalker-io/application
- Owner: windwalker-io
- Created: 2014-09-07T10:31:07.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2023-07-18T08:35:50.000Z (over 2 years ago)
- Last Synced: 2025-01-02T06:44:55.472Z (11 months ago)
- Topics: application, cli-app, cli-application, psr7-handler, server
- Language: PHP
- Homepage: https://github.com/ventoviro/windwalker
- Size: 122 KB
- Stars: 1
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Windwalker Application
Windwalker application is a kernel as main entry of your system.
## Installation via Composer
Add this to the require block in your `composer.json`.
``` json
{
"require": {
"windwalker/application": "~3.0"
}
}
```
## Create An Application
Create application and extends the `doExecute()` method to something.
``` php
use Windwalker\Application\AbstractApplication;
use Windwalker\IO\Input;
use Windwalker\Structure\Structure;
class MyApplication extends AbstractApplication
{
protected function init()
{
// Do stuff.
// Get config
$this->get('foo'); // bar
}
public function doExecute()
{
try
{
// Some code here...
}
catch (\Exception $e)
{
Error::renderErrorPage();
}
return true;
}
}
$app = new MyApplication(new Structure(array('foo' => 'bar')));
$app->execute();
```
Config is `Structure` object, see [Windwalker Structure](https://github.com/ventoviro/windwalker-structure)
## WebApplication
`AbstractWebApplication` contains `WebEnvironment` and `WenHttpServer` object that help us handle HTTP request and output.
### WebEnvironment
Use `WebEnvironment` to get information of browser or server.
``` php
$this->environment->browser->getBrowser(); // Get browser name
```
Use `Platform` to get server information.
``` php
$this->environment->platform->isUnix();
```
See: [Environment Package](https://github.com/ventoviro/windwalker-environment)
### PSR7 Handler
`dispatch()` is a standard PSR7 handler so we can write our logic here, just return Response object and the `WebHttpServer`
object which in Application will render it to client.
``` php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Windwalker\Application\AbstractWebApplication;
class MyHttpKernel extends AbstractWebApplication
{
public function dispatch(Request $request, Response $response, $next = null)
{
// Get request query
$query = $request->getQueryParams();
// Get Psr Uri
$uri = $request->getUri();
// Write body
$response->getBody()->write('
Hello World~~~!
');
return $response;
}
}
$app = new MyHttpKernel;
$app->execute();
```
Result:
``` html
Hello World~~~!
```
### Error Handler
Set error handler as final handler so we can use it in `dispatch()`.
``` php
class MyHttpKernel extends AbstractWebApplication
{
public function dispatch(Request $request, Response $response, $next = null)
{
try
{
throw new \Exception('Whoops~', 500);
}
catch (\Exception $e)
{
return $next($e, $request, $response);
}
return $response;
}
}
$app = new MyHttpKernel;
$app->setFinalHandler(function (Exception $e, Request $request, Response $response)
{
$response->getBody()->write(sprintf('
Error %s. Message: %s
', $e->getCode(), $e->getMessage()));
});
$app->execute();
```
Result:
``` html
Error 500. Message: Whoops~
```
See [Windwalker Http Package](https://github.com/ventoviro/windwalker-http)
## Cli Application
This is a example of a simple cli application.
``` php
// app.php
use Windwalker\Application\AbstractCliApplication;
class MyCliApp extends AbstractCliApplication
{
public function doExecute()
{
// Get options (-h)
$help = $this->io->get('h');
if ($help)
{
$msg = << [-options]
foo Description of this command.
bar Description of this command.
help Description of this command.
MSG;
$this->io->out($msg);
$this->close();
}
// Get arguments
$arg = $this->getArgument(0);
// Do some stuff...
return 0; // Exit code 0 means success
}
}
$app = new MyCliApp;
$app->execute();
```
Now we can access this app by PHP CLI:
``` bash
php app.php arg1 arg2 -h --option --foo bar --n=a
```
See: [Windwalker IO](https://github.com/ventoviro/windwalker-io)