VOOZH about

URL: https://deepwiki.com/calevans/staticforge/3.1-bootstrap-and-initialization

⇱ Bootstrap & Initialization | calevans/staticforge | DeepWiki


Loading...
Last indexed: 11 February 2026 (5f6a2a)
Menu

Bootstrap & Initialization

Purpose and Scope

This document describes the bootstrap process in StaticForge, which initializes the application environment before any commands execute. The bootstrap is responsible for autoloader discovery, environment configuration, container setup, and core service registration.

For information about the dependency injection container itself, see Dependency Injection Container. For details on how features are discovered and loaded after bootstrap, see Feature Architecture. For CLI command initialization, see Site Commands.


Bootstrap Execution Flow

The bootstrap process is implemented as a procedural script at src/bootstrap.php1-258 that executes in a defined sequence and returns a fully configured Container instance.

Bootstrap Sequence Diagram


Sources: src/bootstrap.php1-258 bin/staticforge.php1-60


Autoloader Discovery

The bootstrap checks if Composer's autoloader is already loaded before attempting to load it, preventing duplicate autoloader registration.

Autoloader Search Paths

The system searches for autoload.php in the following locations:

PriorityPathUse Case
1__DIR__ . '/../vendor/autoload.php'Development install (git clone)
2__DIR__ . '/../../../autoload.php'Library install (composer require)
3getcwd() . '/vendor/autoload.php'Fallback to current directory

The check at src/bootstrap.php25 verifies if Composer\Autoload\ClassLoader exists before attempting autoloader discovery. If no autoloader is found, a RuntimeException is thrown at src/bootstrap.php42

Sources: src/bootstrap.php23-44 composer.json38-46


Environment Variable Loading

Environment variables are loaded from .env files using vlucas/phpdotenv and stored in the $_ENV superglobal.

Environment File Discovery


The createUnsafeMutable() method at src/bootstrap.php72 allows overwriting existing environment variables, which is necessary when the script is invoked multiple times in tests.

Default Environment Variables

If environment variables are not set, the bootstrap establishes defaults at src/bootstrap.php113-126:

SOURCE_DIR = "content"
TEMPLATE_DIR = "templates"
OUTPUT_DIR = "public"
LOG_DIR = "logs"
LOG_FILE = "{LOG_DIR}/staticforge.log"
LOG_LEVEL = "INFO"
TEMPLATE = "staticforce"

Sources: src/bootstrap.php60-77 src/bootstrap.php111-127 .env.example1-42


Path Normalization

All directory paths are normalized to absolute paths to ensure consistent path resolution regardless of the working directory when the CLI is invoked.

Normalization Logic

The $normalizePath closure at src/bootstrap.php96-109 implements the following rules:

Path PatternDetectionAction
Absolute UnixStarts with /Return as-is
Absolute WindowsMatches /^[a-zA-Z]:\\/Return as-is
Stream wrapperContains ://Return as-is (e.g., vfs:// for tests)
RelativeNone of abovePrepend $appRoot

Application Root Calculation

The $appRoot is determined by traversing upward from getcwd() until finding a directory containing either composer.json or .env (lines src/bootstrap.php80-93):


This allows StaticForge to be invoked from subdirectories while correctly locating project resources.

Sources: src/bootstrap.php80-109 src/bootstrap.php113-123


Container Initialization

The Container class from the eicc/utils package is instantiated at src/bootstrap.php129 and serves as the central service registry and configuration store.

Container Population

The bootstrap populates the container in this order:

  1. Application Root (src/bootstrap.php130): Stores app_root as the first variable
  2. Environment Variables (src/bootstrap.php134-141): Copies all $_ENV entries except TEMPLATE
  3. Site Configuration (src/bootstrap.php171): Stores parsed siteconfig.yaml as site_config
  4. Template Resolution (src/bootstrap.php186): Stores final TEMPLATE value

Template Resolution Priority

The TEMPLATE variable uses a three-tier priority system at src/bootstrap.php173-186:

Priority 1: siteconfig.yaml['site']['template']
Priority 2: $_ENV['TEMPLATE'] 
Priority 3: Default "staticforce"

This allows siteconfig.yaml (version-controlled) to override .env (environment-specific).

Sources: src/bootstrap.php129-187


Site Configuration Loading

The siteconfig.yaml file is loaded using Symfony's YAML parser at src/bootstrap.php146-168

Configuration File Search

PriorityPathContext
1getcwd() . '/siteconfig.yaml'Current working directory
2$appRoot . 'siteconfig.yaml'Application root directory

If parsing fails, a RuntimeException is thrown with the parse error message at src/bootstrap.php160-164

Configuration Storage

The parsed YAML structure (or empty array if file doesn't exist) is stored as the site_config container variable at src/bootstrap.php171 This data structure is accessed by features for configuration values.

For details on the siteconfig.yaml structure and options, see Site Configuration (siteconfig.yaml).

Sources: src/bootstrap.php143-171 siteconfig.yaml1-556


Service Registration

The bootstrap registers all core services as singleton instances in the container. This ensures that all components share the same service instances throughout the application lifecycle.

Core Services Registry


Service Registration Details

ServiceRegistration MethodClassLines
loggerstuff()EICC\Utils\Logsrc/bootstrap.php191-205
twigstuff()Twig\Environmentsrc/bootstrap.php208-232
EventManageradd()EICC\StaticForge\Core\EventManagersrc/bootstrap.php235-236
AssetManageradd()EICC\StaticForge\Core\AssetManagersrc/bootstrap.php238-239
FeatureManageradd()EICC\StaticForge\Core\FeatureManagersrc/bootstrap.php241-242
ExtensionRegistryadd()EICC\StaticForge\Core\ExtensionRegistrysrc/bootstrap.php244-245
FileDiscoveryadd()EICC\StaticForge\Core\FileDiscoverysrc/bootstrap.php247-248
ErrorHandleradd()EICC\StaticForge\Core\ErrorHandlersrc/bootstrap.php250-251
FileProcessoradd()EICC\StaticForge\Core\FileProcessorsrc/bootstrap.php253-254

Logger Service Configuration

The logger service factory at src/bootstrap.php191-205 reads configuration from $_ENV directly:

  • Creates log directory if it doesn't exist
  • Initializes EICC\Utils\Log with log file path and level
  • Throws RuntimeException if directory creation fails

Twig Service Configuration

The Twig service factory at src/bootstrap.php208-232 performs the following:

  1. Reads TEMPLATE_DIR from container
  2. Creates template directory if missing
  3. Initializes FilesystemLoader with template directory
  4. Adds active template subdirectory to loader path if it exists
  5. Configures Twig environment with debug mode, strict variables disabled, HTML autoescape, and caching disabled

Sources: src/bootstrap.php190-254


Bootstrap Invocation Patterns

The bootstrap is invoked in two primary contexts: CLI execution and test execution.

CLI Invocation

The CLI entry point at bin/staticforge.php1-60 demonstrates the standard invocation pattern:


The bootstrap must be called exactly once per process. Multiple invocations will cause service registration conflicts.

Test Invocation

Test cases use a modified bootstrap process to support virtual filesystems and test-specific configuration.

The UnitTestCase base class at tests/Unit/UnitTestCase.php demonstrates the pattern:


Tests can use virtual filesystem (vfsStream) for most directories, but TEMPLATE_DIR must use real filesystem paths because Twig's loader cannot read from stream wrappers (tests/Unit/Features/HtmlRenderer/FeatureTest.php35-36).

Sources: bin/staticforge.php1-60 tests/Unit/Features/HtmlRenderer/FeatureTest.php25-68


Configuration Priority Hierarchy

The bootstrap implements a multi-tier configuration resolution system where later sources override earlier ones.

Configuration Source Priority


Variable Resolution Examples

VariableSource 1 (Default)Source 2 (.env)Source 3 (siteconfig.yaml)Final Value
SOURCE_DIR"content""src/pages"N/A"src/pages"
TEMPLATE"staticforce""sample""custom""custom"
SITE_BASE_URLNone"http://localhost"N/A"http://localhost"
LOG_LEVEL"INFO"Not setN/A"INFO"

Note that TEMPLATE has special resolution logic at src/bootstrap.php178-184 where siteconfig.yaml['site']['template'] takes precedence over $_ENV['TEMPLATE'].

For complete configuration documentation, see Configuration Resolution.

Sources: src/bootstrap.php111-187 content/development/templates.md173-195


Return Value

The bootstrap script returns the fully configured Container instance at src/bootstrap.php257 This container contains:

  • All environment variables (except TEMPLATE)
  • Computed app_root path
  • Parsed site_config from YAML
  • Resolved TEMPLATE value
  • Registered singleton services (logger, twig)
  • Instantiated core services (7 services registered with their class names as keys)

The calling code (typically bin/staticforge.php or test setup methods) receives this container and uses it to construct application-level objects like commands and the Symfony Console Application.

Sources: src/bootstrap.php257 bin/staticforge.php1-60