HttpCorePlugin::createFacade()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Http;
15
16
use Micro\Component\DependencyInjection\Autowire\AutowireHelperFactory;
17
use Micro\Component\DependencyInjection\Autowire\AutowireHelperFactoryInterface;
18
use Micro\Component\DependencyInjection\Container;
19
use Micro\Framework\Kernel\KernelInterface;
20
use Micro\Framework\Kernel\Plugin\ConfigurableInterface;
21
use Micro\Framework\Kernel\Plugin\DependencyProviderInterface;
22
use Micro\Framework\Kernel\Plugin\PluginConfigurationTrait;
23
use Micro\Framework\Kernel\Plugin\PluginDependedInterface;
24
use Micro\Plugin\Http\Business\Executor\RouteExecutorFactory;
25
use Micro\Plugin\Http\Business\Executor\RouteExecutorFactoryInterface;
26
use Micro\Plugin\Http\Business\Generator\UrlGeneratorFactory;
27
use Micro\Plugin\Http\Business\Generator\UrlGeneratorFactoryInterface;
28
use Micro\Plugin\Http\Business\Locator\RouteLocatorFactory;
29
use Micro\Plugin\Http\Business\Locator\RouteLocatorFactoryInterface;
30
use Micro\Plugin\Http\Business\Matcher\Route\RouteMatcherFactory;
31
use Micro\Plugin\Http\Business\Matcher\Route\RouteMatcherFactoryInterface;
32
use Micro\Plugin\Http\Business\Matcher\UrlMatcherFactory;
33
use Micro\Plugin\Http\Business\Matcher\UrlMatcherFactoryInterface;
34
use Micro\Plugin\Http\Business\Response\Callback\ResponseCallbackFactory;
35
use Micro\Plugin\Http\Business\Response\Callback\ResponseCallbackFactoryInterface;
36
use Micro\Plugin\Http\Business\Response\Transformer\ResponseTransformerFactory;
37
use Micro\Plugin\Http\Business\Response\Transformer\ResponseTransformerFactoryInterface;
38
use Micro\Plugin\Http\Business\Route\RouteBuilderFactory;
39
use Micro\Plugin\Http\Business\Route\RouteBuilderFactoryInterface;
40
use Micro\Plugin\Http\Business\Route\RouteCollectionFactory;
41
use Micro\Plugin\Http\Business\Route\RouteCollectionFactoryInterface;
42
use Micro\Plugin\Http\Configuration\HttpCorePluginConfigurationInterface;
43
use Micro\Plugin\Http\Facade\HttpFacade;
44
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
45
use Micro\Plugin\Locator\LocatorPlugin;
46
47
/**
48
 * @author Stanislau Komar <[email protected]>
49
 *
50
 * @method HttpCorePluginConfigurationInterface configuration()
51
 */
52
class HttpCorePlugin implements DependencyProviderInterface, ConfigurableInterface, PluginDependedInterface
53
{
54
    use PluginConfigurationTrait;
55
56
    private KernelInterface $kernel;
57
58
    private Container $container;
59
60
    /**
61
     * {@inheritDoc}
62
     */
63
    public function provideDependencies(Container $container): void
64
    {
65
        $this->container = $container;
66
67
        $container->register(HttpFacadeInterface::class, function (
68
            KernelInterface $kernel
69
        ) {
70
            $this->kernel = $kernel;
71
72
            return $this->createFacade();
73
        });
74
    }
75
76
    protected function createFacade(): HttpFacadeInterface
77
    {
78
        $routeCollectionFactory = $this->createRouteCollectionFactory();
79
        $routeMatcherFactory = $this->createRouteMatcherFactory();
80
81
        $urlMatcherFactory = $this->createUrlMatcherFactory(
82
            $routeCollectionFactory,
83
            $routeMatcherFactory
84
        );
85
86
        return new HttpFacade(
87
            $urlMatcherFactory,
88
            $routeCollectionFactory,
89
            $this->createRouteExecutorFactory($urlMatcherFactory),
90
            $this->createRouteBuilderFactory(),
91
            $this->createUrlGeneratorFactory($routeCollectionFactory)
92
        );
93
    }
94
95
    protected function createUrlGeneratorFactory(RouteCollectionFactoryInterface $routeCollectionFactory): UrlGeneratorFactoryInterface
96
    {
97
        return new UrlGeneratorFactory($routeCollectionFactory);
98
    }
99
100
    protected function createRouteBuilderFactory(): RouteBuilderFactoryInterface
101
    {
102
        return new RouteBuilderFactory();
103
    }
104
105
    protected function createRouteMatcherFactory(): RouteMatcherFactoryInterface
106
    {
107
        return new RouteMatcherFactory();
108
    }
109
110
    protected function createUrlMatcherFactory(
111
        RouteCollectionFactoryInterface $routeCollectionFactory,
112
        RouteMatcherFactoryInterface $routeMatcherFactory
113
    ): UrlMatcherFactoryInterface {
114
        return new UrlMatcherFactory(
115
            $routeCollectionFactory,
116
            $routeMatcherFactory
117
        );
118
    }
119
120
    /**
121
     * @throws \RuntimeException
122
     */
123
    protected function createRouteCollectionFactory(): RouteCollectionFactoryInterface
124
    {
125
        return new RouteCollectionFactory(
126
            $this->createRouteLocatorFactory()
127
        );
128
    }
129
130
    protected function createRouteLocatorFactory(): RouteLocatorFactoryInterface
131
    {
132
        return new RouteLocatorFactory(
133
            $this->kernel,
134
            $this->configuration()
135
        );
136
    }
137
138
    protected function createResponseCallbackFactory(): ResponseCallbackFactoryInterface
139
    {
140
        return new ResponseCallbackFactory(
141
            $this->createAutowireHelperFactory()
142
        );
143
    }
144
145
    protected function createAutowireHelperFactory(): AutowireHelperFactoryInterface
146
    {
147
        return new AutowireHelperFactory($this->container);
148
    }
149
150
    protected function createRouteExecutorFactory(
151
        UrlMatcherFactoryInterface $urlMatcherFactory,
152
    ): RouteExecutorFactoryInterface {
153
        return new RouteExecutorFactory(
154
            $urlMatcherFactory,
155
            $this->container,
156
            $this->createResponseCallbackFactory(),
157
            $this->createResponseTransformerFactory()
158
        );
159
    }
160
161
    protected function createResponseTransformerFactory(): ResponseTransformerFactoryInterface
162
    {
163
        return new ResponseTransformerFactory(
164
            $this->kernel
165
        );
166
    }
167
168
    public function getDependedPlugins(): iterable
169
    {
170
        return [
171
            LocatorPlugin::class,
172
        ];
173
    }
174
}
175