wp-forge/container
Composer 安装命令:
composer require wp-forge/container
包简介
A lightweight, PHP 8.0+ compatible, PSR-11 dependency injection container.
README 文档
README
A lightweight, PHP 8.0+ compatible, PSR-11 dependency injection container.
Usage
Basic manipulation of items.
<?php use WP_Forge\Container\Container; // Create a new instance $container = new Container(); // Set a value $container->set('email', 'webmaster@site.com'); // Check if a value exists $exists = $container->has('email'); // Get a value $value = $container->get('email'); // Delete a value $container->delete('email');
Basic manipulation of items using array syntax.
<?php use WP_Forge\Container\Container; // Create a new instance $container = new Container(); // Set a value $container['email'] = 'webmaster@site.com'; // Check if a value exists $exists = isset( $container['email'] ); // Get a value $value = $container['email']; // Delete a value unset( $container['email'] );
Register a factory. Factories return a new class instance every time you fetch them.
<?php use WP_Forge\Container\Container; // Create a new instance $container = new Container(); // Add a factory $container->set( 'session', $container->factory( function( Container $c ) { return new Session( $c->get('session_id') ); } ) ); // Get a factory instance. $factory = $container->get( 'session' ); // Check if an item is a factory $isFactory = $container->isFactory( $factory );
Register a service. Services return the same class instance every time you fetch them.
<?php use WP_Forge\Container\Container; // Create a new instance $container = new Container(); // Add a service $container->set( 'session', $container->service( function( Container $c ) { return new Session( $c->get('session_id') ); } ) ); // Get a service instance. $service = $container->get( 'session' ); // Check if an item is a service $isService = $container->isService( $service );
Register a computed value callback.
<?php use WP_Forge\Container\Container; $container = new Container( [ 'first_name' => 'John', 'last_name' => 'Doe', ] ); $container->set( 'full_name', $container->computed( function ( Container $container ) { return implode( ' ', array_filter( [ $container->has( 'first_name' ) ? $container->get( 'first_name' ) : '', $container->has( 'last_name' ) ? $container->get( 'last_name' ) : '', ] ) ); } ) ); $full_name = $container->get( 'full_name' );
Extend a previously registered factory or service.
<?php $container->extend( 'session', function( $instance, Closure $c ) { $instance->setShoppingCart( $c->get('shopping_cart') ); return $instance; } );
wp-forge/container 适用场景与选型建议
wp-forge/container 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.72k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wp-forge/container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wp-forge/container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11.72k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 13
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0-or-later
- 更新时间: 2020-03-22