dmt-software/app-skeleton

基于 Slim 框架创建新应用

dev-main 2023-11-27 13:54 UTC

This package is auto-updated.

Last update: 2024-08-27 15:34:29 UTC


README

build

安装

在以下命令中替换 [path] 以在指定位置创建新应用。

composer create-project dmt-software/app-skeleton [path] --remove-vcs

依赖注入

依赖由 Service Providers 处理,这些提供者在 App::init 方法中包含。在此方法中,您可以添加更多提供程序,它们在 依赖容器 中列出其依赖项。

请参阅 dmt-software/di-plug 了解支持的容器实现。

容器

默认情况下,应用使用 Pimple 容器来存储依赖注入。这可以更改为另一个容器实现。

PHP-DI 容器

composer remove pimple/pimple
composer require php-di/php-di

容器将自动发现,但为确保使用所选容器实现,可以手动配置。

use DMT\Apps\App;
use DMT\DependencyInjection\ContainerFactory;
use DI\Container;
use Slim\Factory\AppFactory;

$app = new App(
    responseFactory: AppFactory::determineResponseFactory(),
    container: (new ContainerFactory())->createContainer(new Container())
);

配置

获取/设置

配置中的配置选项可以轻松通过点分缩写来访问。

$config->get(); // will return all the options as array
$config->get(option: 'option.slug'); // will return the value stored in Config::options['option']['slug'] if set
$config->get(option: 'not.set.option', default: 'value'); // will return the default when it is not set

设置新选项将使用与选项标识符相同的点分缩写。

// all will store the same value in the config
$config->set(value: ['option' => ['slug' => 'value']]);
$config->set(option: 'option', value: ['slug' => 'value']);
$config->set(option: 'option.slug', value: 'value');

设置配置中的选项将使用 array_replace 策略。

加载配置

配置可以从文件加载。默认情况下,这是一个返回数组或返回包含配置选项数组的闭包的 PHP 文件。

// file: config/application.php
return static function () {
    return [
        // settings
    ];
};

// within the application or service provider
$config->load('config/application.php');

Yaml 配置

composer require symfony/yaml

使用 ChainLoader 启用文件包含和 yaml 配置文件。

use DMT\Config\Loaders\FileLoader;
use DMT\Config\Loaders\FileLoaderInterface;
use DMT\Config\Loaders\LoaderChain;
use Symfony\Component\Yaml\Yaml;

$container->set(
    id: FileLoaderInterface::class, 
    value: fn() => new LoaderChain(loaders: [
        new FileLoader(),
        new FileLoader('yaml', Yaml::parseFile(...))
    ])
);