融合器/craftcms-bootstrap

Craft CMS Bootstrap

2.0.0 2018-03-27 12:39 UTC

README

Boot by Ben Davis from the Noun Project

功能

通过将常见任务抽象到简单的API中,减少了启动和配置的样板代码。由Fusionary的Craft CMS样板代码使用。

Bootstrap

例如 @webroot/index.php

  • 将您的应用程序启动样板代码缩减为单个链式语句。
  • 设置PHP常量,并提供合理的回退。
  • 优雅地加载.env文件环境变量。

配置文件

例如 @root/config/general.php 或任何配置文件

  • 使用回退和内容感知类型转换检索环境变量。例如
    • export MY_BOOL=truebool
    • export MY_INT=3int
  • 提供访问HTTP请求头的方法(通过yii\web\Request),如果您的配置依赖于它。
  • 提供将您的整个配置映射到任何匹配/前缀环境变量的方法。
    • 例如,$config['allowAutoUpdates'] 将匹配环境中的 CRAFT_ALLOW_AUTO_UPDATES

先决条件

"php": ">=7.1.0",
"craftcms/cms": "^3.0.0-RC1",

安装

composer require fusionary/craftcms-bootstrap

API文档

类参考/ API文档

示例

Web应用程序

例如 @root/public/index.php

<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
fusionary\craftcms\bootstrap\Bootstrap::run();

多站点Web应用程序

例如 @root/public/site-handle/index.php

<?php
require_once dirname(__DIR__, 2) . '/vendor/autoload.php';
fusionary\craftcms\bootstrap\Bootstrap
    ->setDepth(2) // Set the depth of this script from your project root (`CRAFT_BASE_PATH`) to determine paths
    ->setSite('site-handle') // If the containing folder matches the site handle, you could dynamically set this with `basename(__DIR__)`
    ->run();

控制台应用程序

例如 @root/craft

<?php
require_once dirname(__DIR__) . '/vendor/autoload.php';
exit(Bootstrap::run('console')->setDepth(0)->run()); // Override the default depth of 1, since this script is in `@root`.

环境变量映射

通过传递您的配置到Config::mapMultiEnvConfigConfig::mapConfig,将所有设置映射到相应的环境变量(如果存在)。

设置从Craft/PHP版本(驼峰式)转换为环境变量版本(全部大写,蛇形_case,前缀 — 例如 CRAFT_DB_)。

通用配置

例如 @root/config/general.php

<?php
// Example environment:
// export CRAFT_ALLOW_AUTO_UPDATES=true;

use fusionary\craftcms\bootstrap\helpers\Config;

return Config::mapMultiEnvConfig([
    '*' => [
        'allowAutoUpdates' => true,
        'someOtherSetting' => 'foo',

        // Example: get HTTP header from request
        'devServerProxy' => Config::getHeader('x-dev-server-proxy') ?? false,
    ],
    'production' => [
        'allowAutoUpdates' => false,
    ]
]);

// Result:
// return [
//  '*' => [
//    'allowAutoUpdates' => true,
//    'someOtherSetting' => 'foo'
//  ],
//  'production' => [
//    'allowAutoUpdates' => true
//  ]
// ];

数据库配置

例如 @root/config/db.php

<?php
// Example environment:
// export DB_DRIVER=mysql
// export DB_SERVER=mysql
// export DB_USER=my_app_user
// export DB_PASSWORD=secret
// export DB_DATABASE=my_app_production
// export DB_SCHEMA=public

use fusionary\craftcms\bootstrap\helpers\Config;

// Pass prefix as 2nd argument, defaults to 'CRAFT_'
return Config::mapConfig([
  'driver' => null,
  'server' => null,
  'user' => null,
  'password' => null,
  'database' => null,
  'schema' => null,
], 'DB_');

// Result:
// return [
//   'driver' => 'mysql',
//   'server' => 'mysql',
//   'user' => 'my_app_user',
//   'password' => 'secret',
//   'database' => 'my_app_production',
//   'schema' => 'public',
// ]

生成文档

composer run-script build-docs

致谢

"Boot" 图标由Ben Davis来自 The Noun Project