windwalker / application
Windwalker Application package
Installs: 231
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 5
Forks: 0
Type:windwalker-package
Requires
- php: >=7.1.3
- psr/log: ~1.0
- windwalker/environment: ~3.0
- windwalker/io: ~3.0
- windwalker/structure: ~3.0
- windwalker/uri: ~3.0
Requires (Dev)
- windwalker/http: ~3.0
- windwalker/test: ~3.0
- 3.x-dev
- dev-master / 3.x-dev
- 3.5.25-beta2
- 3.5.23
- 3.5.22
- 3.5.21
- 3.5.20
- 3.5.19
- 3.5.18
- 3.5.17
- 3.5.16
- 3.5.15
- 3.5.14
- 3.5.13
- 3.5.12
- 3.5.11
- 3.5.10
- 3.5.9
- 3.5.8
- 3.5.7
- 3.5.6
- 3.5.5
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4
- 3.3.2
- 3.3.1
- 3.3
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1
- 3.0.1
- 3.0
- 3.0-beta2
- 3.0-beta
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.2
- 2.1.1
- 2.1
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 2.0.0-beta2
- 2.0.0-beta1
- 2.0.0-alpha
- dev-test
This package is auto-updated.
Last update: 2024-10-18 11:17:03 UTC
README
Windwalker application is a kernel as main entry of your system.
Installation via Composer
Add this to the require block in your composer.json
.
{ "require": { "windwalker/application": "~3.0" } }
Create An Application
Create application and extends the doExecute()
method to something.
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
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.
$this->environment->browser->getBrowser(); // Get browser name
Use Platform
to get server information.
$this->environment->platform->isUnix();
See: Environment Package
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.
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('<h1>Hello World~~~!</h1>'); return $response; } } $app = new MyHttpKernel; $app->execute();
Result:
<h1>Hello World~~~!</h1>
Error Handler
Set error handler as final handler so we can use it in dispatch()
.
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('<h1>Error %s. Message: %s</h1>', $e->getCode(), $e->getMessage())); }); $app->execute();
Result:
<h1>Error 500. Message: Whoops~</h1>
Cli Application
This is a example of a simple cli application.
// app.php use Windwalker\Application\AbstractCliApplication; class MyCliApp extends AbstractCliApplication { public function doExecute() { // Get options (-h) $help = $this->io->get('h'); if ($help) { $msg = <<<MSG Help message: version 1.0 ------------------------------------ myapp.php <command> [-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:
php app.php arg1 arg2 -h --option --foo bar --n=a
See: Windwalker IO