定制 stellarwp/foundation-container 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stellarwp/foundation-container

Composer 安装命令:

composer require stellarwp/foundation-container

包简介

The Foundation DI container and its contracts.

README 文档

README

Warning

This is a read-only repository! For pull requests or issues, see stellarwp/foundation.

The DI Container configuration and Service Provider implementation, utilizing di52.

Installation

composer require stellarwp/foundation-container

Container Configuration

Create a new ContainerAdapter, by passing in an instance of di52:

<?php declare(strict_types=1);

namespace My\App;

use lucatume\DI52\Container;
use StellarWP\Foundation\Container\ContainerAdapter;

// Optionally, use the included vlucas/phpdotenv to load environment variables before the container.
$path = __DIR__;

if ( file_exists( $path . '/.env' )  ) {
    $dotenv = Dotenv::createImmutable( $path );
    $dotenv->load();
}

// This implements the Contracts/Container.php interface.
$container = new ContainerAdapter(new Container());

// Bind the concrete to the interface, so anytime we ask for a container we get this one.
$container->bind(Container::class, $container);

// Register our project's configuration. See "Making a config.php" below for more detail.
$container->bind(Dot::class, new Dot(require_once dirname(__FILE__) . '/config.php'));

// Register any service providers in the container.
$providers = [
   StellarWP\YourProject\ServiceProvider::class,
   // as many as you have made... 
];

foreach ( $providers as $provider ) {
    $container->register($provider);
}

Here is an example Service Provider:

<?php declare(strict_types=1);

namespace StellarWP\YourProject\Client;

use lucatume\DI52\ContainerException;
use StellarWP\Foundation\Container\Contracts\Provider;
use StellarWP\Foundation\Storage\Contracts\Storage;
use StellarWP\Foundation\Storage\Drivers\LocalStorage;

final class ServiceProvider extends Provider
{
	/**
	 * @throws ContainerException
	 */
	public function register(): void {
		$this->container->bind(Storage::class, LocalStorage::class);
		$this->container->when(LocalStorage::class)
						->needs('$storagePath')
						->give($this->config->get('storage_path'));
	}
}

Environment Variable Configuration

This library uses the Dot package to set and fetch configuration values, which are initially provided via Environment Variables, either manually set or via an .env file utilizing vlucas/phpdotenv to read them.

Each Service Provider will have access to Dot via the $this->config property, it is best practice to only access configuration variables from a Service Provider and never in your concrete classes.

Making a config.php

A sample config.php for a project. Note: we fall back to sane defaults if the environment variable isn't available.

<?php declare(strict_types=1);

return [
	'some_key'    => $_ENV['SOME_KEY'] ?? '',
	'log'             => [
		'level'    => $_ENV['LOG_LEVEL'] ?? 'debug',
		'channel'  => $_ENV['LOG_CHANNEL'] ?? 'null',
		'channels' => [
			'errorlog' => [],
			'console'  => [
				'with' => [
					'stream' => 'php://stdout',
				],
			],
		],
	],
];

Inside a Provider, we can then access deep variables with dot notation, e.g.

// Get the console log stream type from config.php.
$stream = $this->config->get('log.channels.console.with.stream');

// Get the log level.
$level = $this->config->get('log.level');

💡 If using a config.php in a WordPress plugin, simply add your configured $_ENV vars in your wp-config.php.

// Inside wp-config.php

// App configuration.
$_ENV['SOME_KEY'] = 'abcd-1234'; 
$_ENV['LOG_LEVEL'] = 'info';
$_ENV['LOG_CHANNEL'] = 'errorlog';

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2026-06-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固