nikolaposa/phoundation

Facilitates the routine step of bootstrapping PHP applications.

3.1.0 2023-02-09 20:24 UTC

This package is auto-updated.

Last update: 2024-09-10 00:12:33 UTC


README

Build Status Code Quality Code Coverage Latest Version PDS Skeleton

Phoundation (pronounced the same way as "foundation") facilitates the routine step of bootstrapping PHP applications.

Installation

The preferred method of installation is via Composer. Run the following command to install the latest version of a package and add it to your project's composer.json:

composer require nikolaposa/phoundation

Purpose

Bootstrapping of today's PHP applications typically comes down to:

  1. Configuration loading
  2. Dependency Injection Container initialization

Phoundation aims to reduce the amount of repetitive code needed for the application startup logic by abstracting bootstrapping process.

Usage

Given the configuration files:

config/global.php

return [
    'db' => [
        'driver' => 'pdo_mysql',
        'host' => 'localhost',
        'user' => 'root',
        'password' => 'secret',
        'dbname' => 'test',
    ],
    'dependencies' => [
        'factories' => [
            \PDO::class => function () {
                return new \PDO('sqlite::memory:');
            },
            'My\\Web\\Application' => My\Web\ApplicationFactory::class,
        ]
    ],
];

config/local.php

return [
    'db' => [
        'user' => 'admin',
        'password' => '1234',
    ],
];

Create bootstrap script which typically lives in src/bootstrap.php:

use Phoundation\Bootstrap;
use Phoundation\Config\FileConfigLoader;
use Phoundation\DependencyInjection\LaminasServiceManagerFactory;

$bootstrap = new Bootstrap(
    new FileConfigLoader(glob(sprintf('config/{{,*.}global,{,*.}%s}.php', getenv('APP_ENV') ?: 'local'), GLOB_BRACE)),
    new LaminasServiceManagerFactory()
);

return $bootstrap();

Load bootstrap in your web application root (for example public/index.php):

/* @var \Psr\Container\ContainerInterface $diContainer */
$diContainer = require  __DIR__ . '/../src/bootstrap.php';

$diContainer->get('My\\Web\\Application')->run();

Credits

License

Released under MIT License - see the License File for details.