Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/timkelty/craftcms-bootstrap

Streamline Craft CMS bootstrapping and configuration.
https://github.com/timkelty/craftcms-bootstrap

craft3 craftcms dotenv

Last synced: 3 months ago
JSON representation

Streamline Craft CMS bootstrapping and configuration.

Awesome Lists containing this project

README

        

# Craft CMS Bootstrap

![Boot by Ben Davis from the Noun Project](resources/boot-logo.svg)

## What it does

Reduces boilerplate for bootstrapping and configuration by abstracting common tasks to a simple api.
Used by [Fusionary's Craft CMS Boilerplate](https://github.com/timkelty/craftcms-boilerplate).

### Bootstrap

> e.g `@webroot/index.php`

- Reduces your app bootstrap boilerplate code to a single chainable statement.
- This is especially helpful for achieving consistency when dealing with multiple access points (e.g. [multi-site](https://craftcms.com/news/craft-3-multi-site), [console app](https://craftcms.com/classreference/etc/console/ConsoleApp))
- Sets [PHP constants](https://github.com/craftcms/docs/blob/v3/en/configuration.md#php-constants), with sensible fallbacks.
- Gracefully loads .env file environment variables.

### Configuration files

> e.g. `@root/config/general.php` or any [configuration files](https://docs.craftcms.com/api/v3/craft-config-generalconfig.html#properties)

- Retrieves environment variables with fallbacks and [content-aware type conversion](https://github.com/jpcercal/environment#examples). For example:
- `export MY_BOOL=true` → `bool`
- `export MY_INT=3` → `int`
- Provides access to HTTP request headers (via `yii\web\Request`), should your configuration rely on it.
- Provides method to map your entire config to any matching/prefixed environment variables.
- For example, `$config['allowAutoUpdates']` will match `CRAFT_ALLOW_AUTO_UPDATES` from environment

## Prerequisites

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

## Installation

```
composer require fusionary/craftcms-bootstrap
```

## API Documentation
[Class Reference / API Documentation](http://htmlpreview.github.io/?https://github.com/timkelty/craftcms-bootstrap/blob/master/docs/api/fusionary-craftcms-bootstrap-bootstrap.html)

## Examples

### Web app

> e.g. `@root/public/index.php`

```php
e.g. `@root/public/site-handle/index.php`

```php
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();
```

### Console app

> e.g. `@root/craft`

```php
setDepth(0)->run()); // Override the default depth of 1, since this script is in `@root`.
```

### Environment variable mapping

Passing your config through `Config::mapMultiEnvConfig` or `Config::mapConfig`
will map all settings to corresponding environment variables (if they exist).

Settings are converted from their Craft/PHP versions (camel-case) to their environment variable versions (all-caps, snake-case, prefixed — e.g. **CRAFT_**, **DB_**).

#### General config

> e.g. @root/config/general.php

```php
[
'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
// ]
// ];
```

#### Database config

> e.g. @root/config/db.php

```php
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',
// ]
```

## Generate documentation

```
composer run-script build-docs
```

## Acknowledgements

"[Boot](https://thenounproject.com/term/boot/1466612/)" icon by Ben Davis from [The Noun Project](https://thenounproject.com/)