1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the "php-ipfs" package. |
7
|
|
|
* |
8
|
|
|
* (c) Robert Schönthal <[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 IPFS\Container; |
15
|
|
|
|
16
|
|
|
use Http\Client\Common\PluginClient; |
17
|
|
|
use Http\Client\HttpAsyncClient; |
18
|
|
|
use Http\Discovery\HttpAsyncClientDiscovery; |
19
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
20
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
21
|
|
|
use Http\Message\MessageFactory; |
22
|
|
|
use Http\Message\UriFactory; |
23
|
|
|
use IPFS\Api; |
24
|
|
|
use IPFS\Console\ApiBuildCommand; |
25
|
|
|
use IPFS\Console\CommandBuilder; |
26
|
|
|
use IPFS\Driver\Cli; |
27
|
|
|
use IPFS\Driver\Http; |
28
|
|
|
use IPFS\Utils\AnnotationReader; |
29
|
|
|
use phpDocumentor\Reflection\DocBlockFactory; |
30
|
|
|
use PhpParser\BuilderFactory; |
31
|
|
|
use Pimple\Container; |
32
|
|
|
use Pimple\ServiceProviderInterface; |
33
|
|
|
use Symfony\Component\Console\Application; |
34
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
35
|
|
|
use Symfony\Component\Process\Process; |
36
|
|
|
|
37
|
|
|
class ServiceProvider implements ServiceProviderInterface |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
4 |
|
public function register(Container $pimple) |
43
|
|
|
{ |
44
|
4 |
|
$this->registerDriver($pimple); |
|
|
|
|
45
|
4 |
|
$this->registerConsole($pimple); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$pimple[AnnotationReader::class] = function () { |
48
|
|
|
return new AnnotationReader( |
49
|
|
|
new \Doctrine\Common\Annotations\AnnotationReader(), |
50
|
|
|
DocBlockFactory::createInstance() |
51
|
|
|
); |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
$pimple[Api\ApiBuilder::class] = function () use ($pimple) { |
55
|
|
|
$this->registerHttp($pimple); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$parser = new Api\ApiParser($pimple[HttpAsyncClient::class], $pimple[MessageFactory::class], new Crawler()); |
58
|
|
|
$generator = new Api\ApiGenerator(new BuilderFactory()); |
59
|
|
|
|
60
|
|
|
return new Api\ApiBuilder($parser, $generator); |
61
|
|
|
}; |
62
|
4 |
|
} |
63
|
|
|
|
64
|
|
|
public function registerHttp(Container $pimple) |
65
|
|
|
{ |
66
|
|
|
$pimple[HttpAsyncClient::class] = function () { |
67
|
|
|
return new PluginClient( |
68
|
|
|
HttpAsyncClientDiscovery::find(), |
69
|
|
|
[] |
70
|
|
|
); |
71
|
|
|
}; |
72
|
|
|
|
73
|
|
|
$pimple[MessageFactory::class] = function () { |
74
|
|
|
return MessageFactoryDiscovery::find(); |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
$pimple[UriFactory::class] = function () { |
78
|
|
|
return UriFactoryDiscovery::find(); |
79
|
|
|
}; |
80
|
|
|
} |
81
|
|
|
|
82
|
4 |
|
private function registerDriver(Container $pimple) |
83
|
|
|
{ |
84
|
|
|
$pimple[Http::class] = function () use ($pimple) { |
85
|
|
|
$this->registerHttp($pimple); |
|
|
|
|
86
|
|
|
|
87
|
|
|
return new Http( |
88
|
|
|
$pimple[HttpAsyncClient::class], |
89
|
|
|
$pimple[MessageFactory::class], |
90
|
|
|
$pimple[UriFactory::class], |
91
|
|
|
$pimple[AnnotationReader::class], |
92
|
|
|
getenv('IPFS_API') ?: 'http://localhost:5001/api/v0' |
93
|
|
|
); |
94
|
|
|
}; |
95
|
|
|
|
96
|
|
|
$pimple[Cli::class] = function () use ($pimple) { |
97
|
|
|
return new Cli(new Process([]), $pimple[AnnotationReader::class], getenv('IPFS_BINARY') ?: 'ipfs'); |
98
|
|
|
}; |
99
|
4 |
|
} |
100
|
|
|
|
101
|
4 |
|
private function registerConsole(Container $pimple) |
102
|
|
|
{ |
103
|
|
|
$pimple[CommandBuilder::class] = function (Container $pimple) { |
104
|
|
|
$builder = new CommandBuilder([ |
105
|
|
|
new Api\Basics(), |
106
|
|
|
new Api\Bitswap(), |
107
|
|
|
new Api\Block(), |
108
|
|
|
new Api\Bootstrap(), |
109
|
|
|
new Api\Config(), |
110
|
|
|
new Api\Dag(), |
111
|
|
|
new Api\Dht(), |
112
|
|
|
new Api\Diag(), |
113
|
|
|
new Api\File(), |
114
|
|
|
new Api\Files(), |
115
|
|
|
new Api\Filestore(), |
116
|
|
|
new Api\Key(), |
117
|
|
|
new Api\Log(), |
118
|
|
|
new Api\Name(), |
119
|
|
|
new Api\CObject(), |
120
|
|
|
new Api\P2p(), |
121
|
|
|
new Api\Pin(), |
122
|
|
|
new Api\Pubsub(), |
123
|
|
|
new Api\Refs(), |
124
|
|
|
new Api\Repo(), |
125
|
|
|
new Api\Stats(), |
126
|
|
|
new Api\Swarm(), |
127
|
|
|
new Api\Tar(), |
128
|
|
|
], $pimple[AnnotationReader::class]); |
129
|
|
|
|
130
|
|
|
$builder->addDriver(Cli::class, $pimple->raw(Cli::class)); |
131
|
|
|
$builder->addDriver(Http::class, $pimple->raw(Http::class)); |
132
|
|
|
|
133
|
|
|
return $builder; |
134
|
|
|
}; |
135
|
|
|
|
136
|
|
|
$pimple[Application::class] = function (Container $pimple) { |
137
|
|
|
$app = new Application('ipfs', '@git-version@'); |
138
|
|
|
$app->addCommands($pimple[CommandBuilder::class]->generateCommands()); |
139
|
|
|
$app->add(new ApiBuildCommand($pimple->raw(Api\ApiBuilder::class))); |
140
|
|
|
|
141
|
|
|
return $app; |
142
|
|
|
}; |
143
|
4 |
|
} |
144
|
|
|
} |
145
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: