• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tochka-developers / jsonrpc / 4135501466

pending completion
4135501466

push

github

darkdarin
Merge remote-tracking branch 'origin/v5.0'

209 of 813 new or added lines in 51 files covered. (25.71%)

233 of 1307 relevant lines covered (17.83%)

1.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/JsonRpcServiceProvider.php
1
<?php
2

3
namespace Tochka\JsonRpc;
4

5
use Doctrine\Common\Annotations\AnnotationReader as DoctrineAnnotationReader;
6
use Illuminate\Container\Container;
7
use Illuminate\Support\Facades\Config;
8
use Illuminate\Support\ServiceProvider;
9
use phpDocumentor\Reflection\DocBlockFactory as ReflectionDocBlockFactory;
10
use Spiral\Attributes\AnnotationReader as SpiralAnnotationReader;
11
use Spiral\Attributes\AttributeReader;
12
use Spiral\Attributes\Composite\MergeReader;
13
use Tochka\JsonRpc\Casters\BenSampoEnumCaster;
14
use Tochka\JsonRpc\Casters\CarbonCaster;
15
use Tochka\JsonRpc\Casters\EnumCaster;
16
use Tochka\JsonRpc\Console\RouteCacheCommand;
17
use Tochka\JsonRpc\Console\RouteClearCommand;
18
use Tochka\JsonRpc\Console\RouteListCommand;
19
use Tochka\JsonRpc\Contracts\AnnotationReaderInterface;
20
use Tochka\JsonRpc\Contracts\AuthInterface;
21
use Tochka\JsonRpc\Contracts\CasterRegistryInterface;
22
use Tochka\JsonRpc\Contracts\DocBlockFactoryInterface;
23
use Tochka\JsonRpc\Contracts\ExceptionHandlerInterface;
24
use Tochka\JsonRpc\Contracts\HandleResolverInterface;
25
use Tochka\JsonRpc\Contracts\JsonRpcServerInterface;
26
use Tochka\JsonRpc\Contracts\MiddlewareRegistryInterface;
27
use Tochka\JsonRpc\Contracts\ParamsResolverInterface;
28
use Tochka\JsonRpc\Contracts\ParserInterface;
29
use Tochka\JsonRpc\Contracts\RouteAggregatorInterface;
30
use Tochka\JsonRpc\Contracts\RouteCacheInterface;
31
use Tochka\JsonRpc\Contracts\RouterInterface;
32
use Tochka\JsonRpc\Contracts\ValidatorInterface;
33
use Tochka\JsonRpc\Route\CacheParamsResolver;
34
use Tochka\JsonRpc\Route\CacheRouter;
35
use Tochka\JsonRpc\Route\ControllerFinder;
36
use Tochka\JsonRpc\Route\ParamsResolver;
37
use Tochka\JsonRpc\Route\RouteAggregator;
38
use Tochka\JsonRpc\Route\Router;
39
use Tochka\JsonRpc\Support\AnnotationReader;
40
use Tochka\JsonRpc\Support\Auth;
41
use Tochka\JsonRpc\Support\CasterRegistry;
42
use Tochka\JsonRpc\Support\DefaultHandleResolver;
43
use Tochka\JsonRpc\Support\DocBlockFactory;
44
use Tochka\JsonRpc\Support\MiddlewareRegistry;
45
use Tochka\JsonRpc\Support\Parser;
46
use Tochka\JsonRpc\Support\RouteCache;
47
use Tochka\JsonRpc\Support\ServerConfig;
48
use Tochka\JsonRpc\Support\ServersConfig;
49
use Tochka\JsonRpc\Support\Validator;
50

51
/**
52
 * @psalm-api
53
 * @psalm-import-type ServerConfigArray from ServerConfig
54
 */
55
class JsonRpcServiceProvider extends ServiceProvider
56
{
57
    private const IGNORED_ANNOTATIONS = [
58
        'apiGroupName',
59
        'apiIgnoreMethod',
60
        'apiName',
61
        'apiDescription',
62
        'apiNote',
63
        'apiWarning',
64
        'apiParam',
65
        'apiRequestExample',
66
        'apiResponseExample',
67
        'apiReturn',
68
        'apiTag',
69
        'apiEnum',
70
        'apiObject',
71
        'mixin',
72
    ];
73

74
    public function register(): void
75
    {
76
        $this->registerIgnoredAnnotations();
×
77

NEW
78
        $this->app->when(ControllerFinder::class)
×
NEW
79
            ->needs('$appBasePath')
×
NEW
80
            ->give($this->app->basePath());
×
81

NEW
82
        $this->app->singleton(ServersConfig::class, function () {
×
83
            /** @var array<string, ServerConfigArray> $servers */
NEW
84
            $servers = Config::get('jsonrpc', []);
×
85

NEW
86
            return new ServersConfig($servers);
×
87
        });
×
88

NEW
89
        $this->app->singleton(ValidatorInterface::class, Validator::class);
×
NEW
90
        $this->app->singleton(ParserInterface::class, Parser::class);
×
NEW
91
        $this->app->singleton(AuthInterface::class, Auth::class);
×
NEW
92
        $this->app->singleton(RouteCacheInterface::class, function (): RouteCacheInterface {
×
NEW
93
            return new RouteCache($this->app->bootstrapPath('cache'), 'jsonrpc_routes');
×
NEW
94
        });
×
95

NEW
96
        $this->app->singleton(AnnotationReaderInterface::class, function (): AnnotationReaderInterface {
×
NEW
97
            return new AnnotationReader(
×
NEW
98
                new MergeReader(
×
NEW
99
                    [
×
NEW
100
                        new SpiralAnnotationReader(),
×
NEW
101
                        new AttributeReader(),
×
NEW
102
                    ]
×
NEW
103
                )
×
NEW
104
            );
×
105
        });
×
106

107
        $this->app->singleton(
×
NEW
108
            DocBlockFactoryInterface::class,
×
NEW
109
            function (Container $container): DocBlockFactoryInterface {
×
110
                /** @var DocBlockFactory */
NEW
111
                return $container->make(
×
NEW
112
                    DocBlockFactory::class,
×
NEW
113
                    ['docBlockFactory' => ReflectionDocBlockFactory::createInstance()]
×
NEW
114
                );
×
115
            }
×
116
        );
×
117

118
        $this->app->singleton(
×
NEW
119
            CasterRegistryInterface::class,
×
NEW
120
            function (CasterRegistry $registry): CasterRegistryInterface {
×
NEW
121
                if (class_exists('\BenSampo\Enum\Enum')) {
×
NEW
122
                    $registry->addCaster(new BenSampoEnumCaster());
×
123
                }
NEW
124
                if (class_exists('\Carbon\Carbon')) {
×
NEW
125
                    $registry->addCaster(new CarbonCaster());
×
126
                }
NEW
127
                if (function_exists('enum_exists')) {
×
NEW
128
                    $registry->addCaster(new EnumCaster());
×
129
                }
130

NEW
131
                return $registry;
×
132
            }
×
133
        );
×
134

NEW
135
        $this->app->singleton(ParamsResolverInterface::class, ParamsResolver::class);
×
NEW
136
        $this->app->extend(
×
NEW
137
            ParamsResolverInterface::class,
×
NEW
138
            function (ParamsResolverInterface $paramsResolver, Container $container): ParamsResolverInterface {
×
139
                /** @var CacheParamsResolver */
NEW
140
                return $container->make(CacheParamsResolver::class, ['paramsResolver' => $paramsResolver]);
×
NEW
141
            }
×
NEW
142
        );
×
143

NEW
144
        $this->app->singleton(RouterInterface::class, Router::class);
×
NEW
145
        $this->app->extend(
×
NEW
146
            RouterInterface::class,
×
NEW
147
            function (RouterInterface $router, Container $container): RouterInterface {
×
148
                /** @var CacheRouter */
NEW
149
                return $container->make(CacheRouter::class, ['router' => $router]);
×
150
            }
×
151
        );
×
152

153
        $this->app->singleton(
×
NEW
154
            MiddlewareRegistryInterface::class,
×
NEW
155
            function (ServersConfig $config, MiddlewareRegistry $registry): MiddlewareRegistryInterface {
×
NEW
156
                foreach ($config->serversConfig as $serverName => $serverConfig) {
×
NEW
157
                    $registry->addMiddlewaresFromConfig($serverName, $serverConfig);
×
158
                }
159

NEW
160
                return $registry;
×
161
            }
×
162
        );
×
163

164
        $this->app->singleton(
×
NEW
165
            RouteAggregatorInterface::class,
×
NEW
166
            function (ServersConfig $config, RouteAggregator $aggregator): RouteAggregatorInterface {
×
NEW
167
                foreach ($config->serversConfig as $serverName => $serverConfig) {
×
NEW
168
                    $aggregator->addServer($serverName, $serverConfig);
×
169
                }
170

NEW
171
                return $aggregator;
×
172
            }
×
173
        );
×
174

NEW
175
        $this->app->singleton(HandleResolverInterface::class, DefaultHandleResolver::class);
×
176

NEW
177
        $this->app->singleton(JsonRpcServerInterface::class, JsonRpcServer::class);
×
NEW
178
        $this->app->singleton(ExceptionHandlerInterface::class, ExceptionHandler::class);
×
179
    }
180

181
    public function boot(): void
182
    {
183
        if ($this->app->runningInConsole()) {
×
NEW
184
            $this->commands(
×
NEW
185
                [
×
NEW
186
                    RouteCacheCommand::class,
×
NEW
187
                    RouteClearCommand::class,
×
NEW
188
                    RouteListCommand::class
×
NEW
189
                ]
×
NEW
190
            );
×
191
        }
192

193
        // публикуем конфигурации
NEW
194
        $this->publishes(
×
NEW
195
            [
×
NEW
196
                __DIR__ . '/../config/jsonrpc.php' => $this->app->configPath('jsonrpc.php')
×
NEW
197
            ],
×
NEW
198
            'jsonrpc-config'
×
NEW
199
        );
×
200
    }
201

202
    private function registerIgnoredAnnotations(): void
203
    {
204
        foreach (self::IGNORED_ANNOTATIONS as $annotationName) {
×
205
            DoctrineAnnotationReader::addGlobalIgnoredName($annotationName);
×
206
        }
207
    }
208
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc