windwalker/application

Windwalker 应用程序包

安装: 231

依赖项: 1

建议者: 0

安全: 0

星标: 2

关注者: 5

分支: 0

类型:windwalker-package


README

Windwalker 应用程序是作为系统主入口的核心。

通过 Composer 安装

将以下内容添加到您的 composer.json 文件的 require 块中。

{
    "require": {
        "windwalker/application": "~3.0"
    }
}

创建应用程序

创建应用程序并扩展 doExecute() 方法。

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();

配置是 Structure 对象,请参阅 Windwalker Structure

Web 应用程序

AbstractWebApplication 包含 WebEnvironmentWenHttpServer 对象,帮助我们处理 HTTP 请求和输出。

Web 环境

使用 WebEnvironment 获取浏览器或服务器信息。

$this->environment->browser->getBrowser(); // Get browser name

使用 Platform 获取服务器信息。

$this->environment->platform->isUnix();

请参阅:环境包

PSR7 处理器

dispatch() 是一个标准的 PSR7 处理器,因此我们可以在其中编写逻辑,只需返回 Response 对象和 Application 中的 WebHttpServer 对象,它将渲染给客户端。

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();

结果

<h1>Hello World~~~!</h1>

错误处理器

将错误处理器设置为最终处理器,以便我们可以在 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();

结果

<h1>Error 500. Message: Whoops~</h1>

请参阅:Windwalker Http 包

CLI 应用程序

这是一个简单的 CLI 应用程序示例。

// 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();

现在我们可以通过 PHP CLI 访问此应用程序

php app.php arg1 arg2 -h --option --foo bar --n=a

请参阅:Windwalker IO