Search by

ziraphp / service-provider

Subu-07

Service provider auto-discovery and configuration loading for ZiraPHP.

Package info

github.com/Subu-07/ziraphp-service-provider

pkg:composer/ziraphp/service-provider

Statistics

Installs: 26

Dependents: 1

Suggesters: 1

Stars: 0

Open Issues: 0

v0.1.0 2026-12-04 06:23 UTC

This package is auto-updated.

Last update: 2026-12-04 07:00:42 UTC


README

Service provider auto-discovery and configuration loading for ZiraPHP.

  • Packages declare their providers in composer.json; applications pick them up automatically after composer require.
  • Configuration lives in plain PHP files, read with dot notation and typed getters that fail loudly on the wrong type.
  • Per-environment overrides, and a compiled cache for production.
composer require ziraphp/service-provider

Requires PHP 8.2+ and ziraphp/framework ^0.1.

Quick start

// public/index.php
use ZiraPHP\Core\Application;
use ZiraPHP\ServiceProvider\Bootstrapper;
use ZiraPHP\ServiceProvider\Config\Env;

require dirname(__DIR__) . '/vendor/autoload.php';

$app = new Application(dirname(__DIR__));

$bootstrapper = new Bootstrapper($app);
$bootstrapper->loadConfig(
    environment: Env::get('APP_ENV'),
    cachePath: $app->basePath('var/cache/config.php'),
);
$bootstrapper->registerProviders($app->basePath('var/cache/providers.php'));

$app->run();

Writing a provider

Extend ServiceProvider and override the phases you need. register() only binds services; boot() runs after every provider is registered, so it can use services from other providers.

namespace Acme\Blog;

use ZiraPHP\Interfaces\ApplicationInterface;
use ZiraPHP\Interfaces\Container\ContainerInterface;
use ZiraPHP\ServiceProvider\ServiceProvider;

final class BlogServiceProvider extends ServiceProvider
{
    public function register(ContainerInterface $container): void
    {
        $container->singleton(PostRepository::class, PdoPostRepository::class);
    }

    public function boot(ApplicationInterface $application): void
    {
        $application->getRouter()->get('/blog', [BlogController::class, 'index']);
    }
}

Implementing ZiraPHP\Interfaces\Container\ServiceProviderInterface directly works too.

Package discovery

A package lists its providers under extra.ziraphp.providers:

{
    "name": "acme/blog",
    "extra": {
        "ziraphp": {
            "providers": ["Acme\\Blog\\BlogServiceProvider"]
        }
    }
}

The application's own composer.json can add providers and opt out of discovery:

{
    "extra": {
        "ziraphp": {
            "providers": ["App\\AppServiceProvider"],
            "dont-discover": ["acme/blog"]
        }
    }
}

Use "dont-discover": ["*"] to ignore every package.

registerProviders() registers, in this order and without duplicates:

  1. providers from installed packages (read from vendor/composer/installed.json, honouring config.vendor-dir);
  2. providers from the root composer.json;
  3. the list in the app.providers configuration value, when configuration is loaded (change the key with configKey:).

Pass discover: false to register only the configured list.

Manifest cache

With a manifest path, discovery results are stored in a PHP file that OPcache keeps in memory. The file records a hash of composer.json and installed.json and rebuilds itself when either changes, so it never needs clearing after composer require or composer remove.

ProviderManifest::rebuild() and clear() are available for tooling.

Configuration

Each config/*.php file returns an array, stored under the file's name.

// config/database.php
use ZiraPHP\ServiceProvider\Config\Env;

return [
    'default' => Env::get('DB_CONNECTION', 'pgsql'),
    'connections' => [
        'pgsql' => [
            'host' => Env::get('DB_HOST', '127.0.0.1'),
            'port' => (int) Env::get('DB_PORT', 5432),
        ],
    ],
];
use ZiraPHP\ServiceProvider\Config\ConfigInterface;

final class ReportService
{
    public function __construct(private readonly ConfigInterface $config)
    {
    }

    public function host(): string
    {
        return $this->config->string('database.connections.pgsql.host');
    }
}

loadConfig() binds the configuration as ConfigInterface, Repository and "config", so it can be autowired.

Method Returns
get($key, $default = null) Any value, or the default when the key is missing
has($key) true when the key exists, even if its value is null
string(), int(), float(), bool(), array() The value with that type. Throws ConfigException when the type is wrong, or when the key is missing and no default is given. float() also accepts integers.
set($key, $value) Sets a value, creating nested arrays
all() Everything

Environments

With environment: 'testing', files in config/testing/ are merged on top of config/. Associative arrays merge key by key; lists and scalars are replaced whole, so an override's provider list replaces the base list.

Env::get()

Reads $_ENV, then $_SERVER, then getenv(). The strings true, false, null and empty (any case, optionally in parentheses) become true, false, null and ''; matching surrounding quotes are removed; anything else stays a string.

Configuration cache

use ZiraPHP\ServiceProvider\Config\ConfigCache;
use ZiraPHP\ServiceProvider\Config\ConfigLoader;

$cache = new ConfigCache(__DIR__ . '/var/cache/config.php');
$cache->write((new ConfigLoader(__DIR__ . '/config'))->load('production'));

When loadConfig() is given a cachePath that exists, the cache is used and the config directory is not read. The cache is not refreshed automatically: write it during deployment and clear() it when configuration files or environment variables change. Values must be scalars, null or arrays; closures and objects are rejected with a CacheException that names the key.

Development

composer install
composer check   # PHP-CS-Fixer, PHPCS (PSR-12), PHPStan level 9, Psalm level 1, PHPUnit

License

MIT. See LICENSE.