File size: 1,553 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace Plugin\Infrastructure;

use Easy\Container\Attributes\Inject;
use Plugin\Domain\Context;
use Plugin\Domain\Exceptions\PluginInterfaceNotImplementedException;
use Plugin\Domain\PluginInterface;
use Plugin\Domain\PluginWrapper;
use Plugin\Domain\Repositories\PluginRepositoryInterface;
use Plugin\Domain\ValueObjects\Status;
use Plugin\Domain\ValueObjects\Type;
use Psr\Container\ContainerInterface;

class PluginLoader
{
    public function __construct(
        private ContainerInterface $container,
        private PluginRepositoryInterface $repo,

        #[Inject('option.theme')]
        private string $theme = 'heyaikeedo/default',
    ) {
    }

    public function load(string $path): PluginWrapper
    {
        $context = new Context($path);

        $plugin = null;

        if ($context->entryClass->value) {
            $plugin = $this->container->get($context->entryClass->value);
        }

        if ($plugin && !($plugin instanceof PluginInterface)) {
            throw new PluginInterfaceNotImplementedException();
        }

        $pw = new PluginWrapper($context, $plugin);
        $this->repo->add($pw);

        if ($context->type == Type::THEME) {
            $context->setStatus(
                $context->name->value === $this->theme
                    ? Status::ACTIVE
                    : Status::INACTIVE
            );
        }

        if ($plugin && $context->getStatus() == Status::ACTIVE) {
            $pw->plugin->boot($context);
        }

        return $pw;
    }
}