Skip to content
590 changes: 52 additions & 538 deletions Sources/ErrorHandler.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

declare(strict_types=1);

namespace SMF;
namespace SMF\Infrastructure;

use League\Container\Container as LeagueContainer;

/**
* A wrapper for the League\Container dependency injection container.
* Its meant to be a temporary wrapper until all global vars are moved to dependencies.
*/
class Container
{
Expand All @@ -42,6 +43,7 @@ public static function init(): void
{
if (!isset(self::$instance)) {
self::$instance = new LeagueContainer();
self::$instance->addServiceProvider(new ServiceProvider());
}
}

Expand Down
41 changes: 41 additions & 0 deletions Sources/Infrastructure/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace SMF\Infrastructure;

use League\Container\ServiceProvider\AbstractServiceProvider;

class ServiceProvider extends AbstractServiceProvider
{
/*********************
* Internal properties
*********************/

private array $services;

/****************
* Public methods
****************/

public function __construct(array $services = [])
{
$coreServices = require __DIR__ . '/ServicesList.php';
$this->services = array_filter(array_merge($coreServices, $services));
}

public function provides(string $id): bool
{
return \array_key_exists($id, $this->services);
}

public function register(): void
{
$container = $this->getContainer();

foreach ($this->services as $id => $config) {
$method = ($config['shared'] ?? false) ? 'addShared' : 'add';

$container->$method($id)
->addArguments($config['arguments'] ?? []);
}
}
}
14 changes: 14 additions & 0 deletions Sources/Infrastructure/ServicesList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

use SMF\Services\ErrorHandlerService;

// List of all services registered for SMF, example:
//'db' => [
// 'arguments' => [$db_server, $db_user],
// 'shared' => true // false will create a new instance everytime
//],
return [
ErrorHandlerService::class => [
'shared' => true,
],
];
8 changes: 8 additions & 0 deletions Sources/Infrastructure/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

// Try to handle it with the upper level index.php. (it should know what to do.)
if (file_exists(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'index.php')) {
include dirname(__DIR__) . DIRECTORY_SEPARATOR . 'index.php';
} else {
exit;
}
Loading