Code Monkey home page Code Monkey logo

application's Introduction

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.

{
    "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>

See Windwalker Http Package

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

application's People

Contributors

asika32764 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.