Skip to content

🏗️ Facilitates the routine step of bootstrapping PHP applications.

License

Notifications You must be signed in to change notification settings

nikolaposa/phoundation

Repository files navigation

Phoundation

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.