-
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathDefaultContainer.php
More file actions
104 lines (94 loc) · 3.26 KB
/
DefaultContainer.php
File metadata and controls
104 lines (94 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
/**
* Copyright (c) 2024 Kai Sassnowski
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/roach-php/roach
*/
namespace RoachPHP\Core;
use League\Container\Container;
use League\Container\ReflectionContainer;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use RoachPHP\Http\Client;
use RoachPHP\Http\ClientInterface;
use RoachPHP\ItemPipeline\ItemPipeline;
use RoachPHP\ItemPipeline\ItemPipelineInterface;
use RoachPHP\Scheduling\ArrayRequestScheduler;
use RoachPHP\Scheduling\RequestSchedulerInterface;
use RoachPHP\Scheduling\Timing\ClockInterface;
use RoachPHP\Scheduling\Timing\SystemClock;
use RoachPHP\Shell\Resolver\NamespaceResolverInterface;
use RoachPHP\Shell\Resolver\StaticNamespaceResolver;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* @internal
*/
final class DefaultContainer implements ContainerInterface
{
private Container $container;
public function __construct()
{
$this->container = (new Container())->delegate(new ReflectionContainer());
$this->registerDefaultBindings();
}
public function get(string $id)
{
return $this->container->get($id);
}
public function has(string $id): bool
{
return $this->container->has($id);
}
private function registerDefaultBindings(): void
{
$this->container->addShared(
ContainerInterface::class,
$this->container,
);
$this->container->addShared(
LoggerInterface::class,
static fn () => (new Logger('roach'))->pushHandler(new StreamHandler('php://stdout')),
);
$this->container->addShared(EventDispatcher::class, EventDispatcher::class);
$this->container->addShared(
EventDispatcherInterface::class,
EventDispatcher::class,
);
$this->container->add(ClockInterface::class, SystemClock::class);
$this->container->addShared(
RequestSchedulerInterface::class,
/** @phpstan-ignore return.type */
fn (): RequestSchedulerInterface => $this->container->get(ArrayRequestScheduler::class),
);
$this->container->add(ClientInterface::class, Client::class);
$this->container->add(
ItemPipelineInterface::class,
/** @phpstan-ignore return.type */
fn (): ItemPipelineInterface => $this->container->get(ItemPipeline::class),
);
$this->container->add(
NamespaceResolverInterface::class,
StaticNamespaceResolver::class,
);
$this->container->add(
EngineInterface::class,
/** @phpstan-ignore return.type */
fn (): EngineInterface => $this->container->get(Engine::class),
);
$this->container->add(
RunnerInterface::class,
fn (): RunnerInterface => new Runner(
$this->container,
/** @phpstan-ignore argument.type */
$this->container->get(EngineInterface::class),
),
);
}
}