forked from pavlokomarov/roach-php-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoach.php
More file actions
104 lines (89 loc) · 3.28 KB
/
Roach.php
File metadata and controls
104 lines (89 loc) · 3.28 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) 2021 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;
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\Core\Engine;
use RoachPHP\Core\RunFactory;
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\Spider\Configuration\Overrides;
use RoachPHP\Spider\SpiderInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
final class Roach
{
private static ?ContainerInterface $container = null;
public static function useContainer(ContainerInterface $container): void
{
self::$container = $container;
}
/**
* @psalm-param class-string<SpiderInterface> $spiderClass
*/
public static function startSpider(string $spiderClass, ?Overrides $overrides = null): void
{
self::$container ??= self::defaultContainer();
$spider = self::resolve($spiderClass);
$runFactory = new RunFactory(self::$container);
$engine = self::resolve(Engine::class);
$run = $runFactory->fromSpider($spider, $overrides);
$engine->start($run);
}
private static function defaultContainer(): ContainerInterface
{
$container = (new Container())->delegate(new ReflectionContainer());
$container->share(
LoggerInterface::class,
static fn () => (new Logger('roach'))->pushHandler(new StreamHandler('php://stdout')),
);
$container->share(EventDispatcher::class, EventDispatcher::class);
$container->share(EventDispatcherInterface::class, EventDispatcher::class);
$container->add(ClockInterface::class, SystemClock::class);
$container->share(
RequestSchedulerInterface::class,
/** @psalm-suppress MixedReturnStatement, MixedInferredReturnType */
static fn (): RequestSchedulerInterface => $container->get(ArrayRequestScheduler::class),
);
$container->add(ClientInterface::class, Client::class);
$container->add(
ItemPipelineInterface::class,
/** @psalm-suppress MixedReturnStatement, MixedInferredReturnType */
static fn (): ItemPipelineInterface => $container->get(ItemPipeline::class),
);
return $container;
}
/**
* @template T
* @psalm-param class-string<T> $class
* @psalm-suppress MixedInferredReturnType
*
* @return T
*/
private static function resolve(string $class): mixed
{
if (null === self::$container) {
self::$container = self::defaultContainer();
}
/** @psalm-suppress MixedReturnStatement */
return self::$container->get($class);
}
}