Proton -一个兼容的StackPHP微型的PHP框架

网友投稿 1149 2022-10-25 15:28:09

Proton -一个兼容的StackPHP微型的PHP框架

Proton

Proton is a StackPHP compatible micro framework.

Under the hood it uses League\Route for routing, League\Container for dependency injection, and League\Event for event dispatching.

Installation

Just add "alexbilbie/proton": "~1.4" to your composer.json file.

Setup

Basic usage with anonymous functions:

// index.phpget('/', function ($request, $response) { $response->setContent('

It works!

'); return $response;});$app->get('/hello/{name}', function ($request, $response, $args) { $response->setContent( sprintf('

Hello, %s!

', $args['name']) ); return $response;});$app->run();

Basic usage with controllers:

// index.phpget('/', 'HomeController::index'); // calls index method on HomeController class$app->run();

// HomeController.phpsetContent('

It works!

'); return $response; }}

Basic usage with StackPHP (using Stack\Builder and Stack\Run):

// index.phpget('/', function ($request, $response) { $response->setContent('

It works!

'); return $response;});$stack = (new Stack\Builder()) ->push('Some/MiddleWare') // This will execute first ->push('Some/MiddleWare') // This will execute second ->push('Some/MiddleWare'); // This will execute third$app = $stack->resolve($app);Stack\run($app); // The app will run after all the middlewares have run

Debugging

By default Proton runs with debug options disabled. To enable debugging add

$app['debug'] = true;

Proton has built in support for Monolog. To access a channel call:

$app->getLogger('channel name');

For more information about channels read this guide - https://github.com/Seldaek/monolog/blob/master/doc/usage.md#leveraging-channels.

Custom exception decoration

$app->setExceptionDecorator(function (\Exception $e) { $response = new \Symfony\Component\HttpFoundation\Response; $response->setStatusCode(500); $response->setContent('Epic fail!'); return $response;});

Events

You can intercept requests and responses at three points during the lifecycle:

request.received

$app->subscribe('request.received', function ($event) { // access the request using $event->getRequest()})

This event is fired when a request is received but before it has been processed by the router.

response.created

$app->subscribe('response.created', function ($event) { // access the request using $event->getRequest() // access the response using $event->getResponse()})

This event is fired when a response has been created but before it has been output.

response.sent

$app->subscribe('response.sent', function ($event) { // access the request using $event->getRequest() // access the response using $event->getResponse()})

This event is fired when a response has been output and before the application lifecycle is completed.

Custom Events

You can fire custom events using the event emitter directly:

// Subscribe$app->subscribe('custom.event', function ($event, $time) { return 'the time is '.$time;});// Publish$app->getEventEmitter()->emit('custom.event', time());

Dependency Injection Container

Proton uses League/Container as its dependency injection container.

You can bind singleton objects into the container from the main application object using ArrayAccess:

$app['db'] = function () { $manager = new Illuminate\Database\Capsule\Manager; $manager->addConnection([ 'driver' => 'mysql', 'host' => $config['db_host'], 'database' => $config['db_name'], 'username' => $config['db_user'], 'password' => $config['db_pass'], 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci' ], 'default'); $manager->setAsGlobal(); return $manager;};

or by accessing the container directly:

$app->getContainer()->singleton('db', function () { $manager = new Illuminate\Database\Capsule\Manager; $manager->addConnection([ 'driver' => 'mysql', 'host' => $config['db_host'], 'database' => $config['db_name'], 'username' => $config['db_user'], 'password' => $config['db_pass'], 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci' ], 'default'); $manager->setAsGlobal(); return $manager;});

Multitons can be added using the add method on the container:

$app->getContainer()->add('foo', function () { return new Foo();});

Service providers can be registered using the register method on the Proton app or addServiceProvider on the container:

$app->register('\My\Service\Provider');$app->getContainer()->addServiceProvider('\My\Service\Provider');

For more information about service providers check out this page - http://container.thephpleague.com/service-providers/.

For easy testing down the road it is recommended you embrace constructor injection:

$app->getContainer()->add('Bar', function () { return new Bar();});$app->getContainer()->add('Foo', function () use ($app) { return new Foo( $app->getContainer()->get('Bar') );});

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:公司微服务项目部署调试
下一篇:#yyds干货盘点# 面试必刷TOP101:判断链表中是否有环
相关文章