mattferris/application
Composer 安装命令:
composer require mattferris/application
包简介
A PHP application loader
README 文档
README
A PHP application bootstrapping framework.
The framework is built on the concept of components. Each component is
responsible for configuring itself, registering services, subscribing to events,
etc. This means you just add the components you want to your application and
run it! The only thing that is required to configure an application is a dependency
injection container that implements MattFerris\Di\ContainerInterface (which in
turn extends Interop\Container\ContainerInterface)
A simple HTTP application might look something like:
use MattFerris\Application\Application; use MattFerris\Di\Di; // setup your application by adding components to it $app = new Application(new Di(), [ '\MattFerris\Events\EventsComponent', // event handling '\MattFerris\Http\Routing\HttpRoutingComponent' // HTTP request handling ]); // then run it by passing a startup callable to run() $app->run(['\MattFerris\Http\Routing\HttpRoutingComponent', 'run']);
The HttpRoutingComponent has a static method called run() that provides some
additional bootstrapping to start the request handling process. You could also
supply your own callable to $app->run() if you wanted.
Components
A component might be anything from an individual function to an entire library.
In Application terms, a project (i.e. the code) only becomes a component when
the projects's configuration and initialization are wrapped into a neat package,
represented as an class implementing MattFerris\Component\ComponentInterface.
Typically, this component class would live in the top-most namespace of your
project.
For simplicity, your component class can just extend
MattFerris\Application\Component.
namespace My\Project; use MattFerris\Application\Component; class MyProjectComponent extends Component { }
When extending MattFerris\Application\Component you get a component that will
automatically load providers from within the same namespace. So given the
above component, you can just create providers that live within the My\Project
namespace.
namespace My\Project; use MattFerris\Provider\ProviderInterface; class EventsProvider implements ProviderInterface { public function provides($consumer) { // register events, etc... } }
This EventsProvider will now be automatically loaded at application boot.
Providers
A components only real job is to plug providers into the framework. Providers
are where the heavy-lifting happens, and are responsible for registering
services with service containers, registering event listeners with event
dispatchers, supplying routing information to HTTP request dispatchers, etc. A
provider must implement MattFerris\Provider\ProviderInterface which requires a
single method be present called provides().
Be default, there are no provider types. Components must register provider types during initialization.
// provided by mattferris/bridge-components use MattFerris\Bridge\Components\Di\DiComponent; use MattFerris\Di\Di; use MattFerris\Application\Application; use My\Project\MyProjectComponent; $app = new Application(new Di(), [ DiComponent::class, MyProjectComponent::class ]);
DiComponent registers a Services provider type. You could now defined a
ServicesProvider in order to register services.
namespace My\Project; use MattFerris\Provider\ProviderInterface; class ServicesProvider implements ProviderInterface { public function provides($consumer) { // $consumer will contain an instance of the service container $container = $consumer; // register a service $container->set('MyProjectService', new MyProjectService()); } }
ServicesProvider::provides() is always passed an instance of the configured
service container. Likewise, EventsProviders will be passed an instance of the
event dispatcher, and so on.
Provider Types
So far, we've talked about global provider types. Global provider types can
be registered by components in two way. For components extending
MattFerris\Application\Components, it's as easy as defining the $provides
property in the component's class definition.
namespace My\Project; use MattFerris\Application\Component; class MyProjectComponent extends Component { protected $providers = [ [ 'MyProjectType' => [ 'consumer' => MyProjectConsumer::class, 'scope' => 'global' ] ] ]; // ... }
The consumer is the type of instance that will be passed to a providers
provides() method. In this case, it will be an instance of
My\Project\MyProjectConsumer. Because scope is set to global, the component
will automatically register the provider.
For standalone components (those not extending MattFerris\Application\Component),
registration of provider types is done by having the component class implement
MattFerris\Provider\ProviderInterface, and using provides() to manually
register the types.
namespace My\Project; use MattFerris\Component\ComponentInterface; use MattFerris\Provider\ProviderInterface; class MyProjectComponent implements ComponentInterface, ProviderInterface { public function provides($consumer) { // $consumer will contain an instance of MattFerris\Application\Application $consumer->addProvider('MyProjectType', MyProjectConsumer::class); } // ... }
For components extending MattFerris\Application\Component, you can also define
local providers for use within the scope of the component. This gives you some
flexibility in isolating intialization of your component as required.
namespace My\Project; use MattFerris\Application\Component; class MyProjectComponent extends Component { protected $providers = [ [ 'MyLocalType' => [ 'consumer' => MyLocalProjectConsumer::class, 'scope' => 'local' ] ] ]; // ... }
Advanced Component Initialization
In some cases, components may have dependencies on each other. It's possible to defined different intialization passes so that components can be initialized in such a way that these dependencies can be satisfied.
use MattFerris\Application\Application; $app = new Application($di, [ [ 'Component\Satisfying\DependenciesComponent' ], // pass 1 [ 'Another\Component\DependingOnFirstComponent' ] // pass 2 ]);
By simply passing an array of arrays, you can break down component intializtion into as many passes as is required to successfully bootstrap your application.
mattferris/application 适用场景与选型建议
mattferris/application 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 132 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mattferris/application 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mattferris/application 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 132
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2017-01-05