Completed
Push — master ( 8c5d81...eefcd6 )
by Dmitry
02:55
created

src/Application.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Basis;
4
5
use League\Container\Container;
6
use League\Container\ReflectionContainer;
7
use Tarantool\Mapper\Mapper;
8
use Tarantool\Mapper\Plugin\Annotation;
9
use Tarantool\Mapper\Plugin\Procedure as ProcedurePlugin;
10
use Tarantool\Mapper\Procedure;
11
use Tarantool\Mapper\Repository;
12
13
class Application extends Container
14
{
15
    private $reflection;
16
17
    public function __construct(string $root)
18
    {
19
        parent::__construct();
20
21
        $fs = new Filesystem($this, $root);
22
23
        $this->share(Application::class, $this);
24
        $this->share(Container::class, $this);
25
        $this->share(Filesystem::class, $fs);
26
27
        $this->addServiceProvider(Provider\ClickhouseProvider::class);
28
        $this->addServiceProvider(Provider\CoreProvider::class);
29
        $this->addServiceProvider(Provider\GuzzleProvider::class);
30
        $this->addServiceProvider(Provider\PoolProvider::class);
31
        $this->addServiceProvider(Provider\PredisProvider::class);
32
        $this->addServiceProvider(Provider\ServiceProvider::class);
33
        $this->addServiceProvider(Provider\TarantoolProvider::class);
34
35
        foreach ($fs->listClasses('Provider') as $provider) {
36
            $this->addServiceProvider($provider);
37
        }
38
39
        $this->delegate($this->reflection = new ReflectionContainer());
40
    }
41
42 46
    public function dispatch(string $job, array $params = [], string $service = null)
43
    {
44 46
        if ($service !== null) {
45
            if ($this->get(Service::class)->getName() == $service) {
46
                $service = null;
47
            }
48
        }
49
50 46
        return $this->get(Cache::class)
51 46
            ->wrap([$job, $params, $service], function() use ($job, $params, $service) {
52 46
                $runner = $this->get(Runner::class);
53 46
                if ($service === null) {
54 46
                    if ($runner->hasJob($job)) {
55 46
                        return $runner->dispatch($job, $params);
56
                    }
57 1
                    if (explode('.', $job)[0] == $this->get(Service::class)->getName()) {
58
                        return $runner->dispatch($job, $params);
59
                    }
60
                }
61
62 1
                $dispatcher = $this->get(Dispatcher::class);
63 1
                return $dispatcher->dispatch($job, $params, $service);
64 46
            });
65
    }
66
67 46
    public function get($alias, bool $new = false) : object
68
    {
69 46
        if (!$this->hasInstance($alias)) {
70 46
            $instance = null;
71 46
            if (is_subclass_of($alias, Procedure::class)) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Mapper\Procedure::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
72 4
                $instance = $this->get(Mapper::class)
73 4
                    ->getPlugin(ProcedurePlugin::class)
74 4
                    ->get($alias);
75
            }
76 46
            if (is_subclass_of($alias, Repository::class)) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Tarantool\Mapper\Repository::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
77 1
                $spaceName = $this->get(Mapper::class)
78 1
                    ->getPlugin(Annotation::class)
79 1
                    ->getRepositorySpaceName($alias);
80
81 1
                if ($spaceName) {
82 1
                    $instance = $this->get(Mapper::class)->getRepository($spaceName);
83
                }
84
            }
85 46
            if ($instance) {
86 5
                $this->share($alias, function () use ($instance) {
87 5
                    return $instance;
88 5
                });
89
            }
90
        }
91 46
        return parent::get($alias, $new);
92
    }
93
94 46
    public function hasInstance($id) : bool
95
    {
96 46
        if ($this->definitions->has($id)) {
97 46
            return true;
98
        }
99 46
        if ($this->definitions->hasTag($id)) {
100
            return true;
101
        }
102 46
        if ($this->providers->provides($id)) {
103 46
            return true;
104
        }
105 46
        return false;
106
    }
107
108 46
    public function has($id) : bool
109
    {
110 46
        if ($this->definitions->has($id)) {
111 46
            return true;
112
        }
113 46
        if ($this->definitions->hasTag($id)) {
114
            return true;
115
        }
116 46
        if ($this->providers->provides($id)) {
117 46
            return true;
118
        }
119 8
        if ($this->reflection && $this->reflection->has($id)) {
120 7
            return true;
121
        }
122 1
        return false;
123
    }
124
125 46
    public function call($callback)
126
    {
127 46
        return $this->reflection->call(...func_get_args());
128
    }
129
}
130