|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Charcoal\App\ServiceProvider; |
|
4
|
|
|
|
|
5
|
|
|
// From Pimple |
|
6
|
|
|
use Pimple\ServiceProviderInterface; |
|
7
|
|
|
use Pimple\Container; |
|
8
|
|
|
|
|
9
|
|
|
// From 'league/climate' |
|
10
|
|
|
use League\CLImate\CLImate; |
|
11
|
|
|
|
|
12
|
|
|
// From 'charcoal-factory' |
|
13
|
|
|
use Charcoal\Factory\GenericFactory as Factory; |
|
14
|
|
|
|
|
15
|
|
|
use Charcoal\App\Route\ScriptRoute; |
|
16
|
|
|
use Charcoal\App\Script\ScriptInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Script Service Provider |
|
20
|
|
|
*/ |
|
21
|
|
|
class ScriptServiceProvider implements ServiceProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Registers services on the given container. |
|
25
|
|
|
* |
|
26
|
|
|
* This method should only be used to configure services and parameters. |
|
27
|
|
|
* It should not get services. |
|
28
|
|
|
* |
|
29
|
|
|
* @param Container $container A container instance. |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function register(Container $container) |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var string The default route controller for templates. */ |
|
35
|
|
|
$container['route/controller/script/class'] = ScriptRoute::class; |
|
36
|
|
|
|
|
37
|
|
|
$this->registerScriptFactory($container); |
|
|
|
|
|
|
38
|
|
|
$this->registerClimate($container); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param Container $container The DI container. |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
private function registerScriptFactory(Container $container) |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* The Script Factory service is used to instantiate new scripts. |
|
49
|
|
|
* |
|
50
|
|
|
* - Scripts are `ScriptInterface` and must be suffixed with `Script`. |
|
51
|
|
|
* - The container is passed to the created script constructor, which will call `setDependencies()`. |
|
52
|
|
|
* |
|
53
|
|
|
* @param Container $container A container instance. |
|
54
|
|
|
* @return \Charcoal\Factory\FactoryInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
$container['script/factory'] = function (Container $container) { |
|
57
|
|
|
return new Factory([ |
|
58
|
|
|
'base_class' => ScriptInterface::class, |
|
59
|
|
|
'resolver_options' => [ |
|
60
|
|
|
'suffix' => 'Script' |
|
61
|
|
|
], |
|
62
|
|
|
'arguments' => [[ |
|
63
|
|
|
'container' => $container, |
|
64
|
|
|
'logger' => $container['logger'], |
|
65
|
|
|
'climate' => $container['script/climate'], |
|
66
|
|
|
'climate_reader' => $container['script/climate/reader'] |
|
67
|
|
|
]] |
|
68
|
|
|
]); |
|
69
|
|
|
}; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Container $container A container instance. |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
private function registerClimate(Container $container) |
|
77
|
|
|
{ |
|
78
|
|
|
/** |
|
79
|
|
|
* @param Container $container A container instance. |
|
80
|
|
|
* @return null|\League\CLImate\Util\Reader\ReaderInterface |
|
81
|
|
|
*/ |
|
82
|
|
|
$container['script/climate/reader'] = function () { |
|
83
|
|
|
return null; |
|
84
|
|
|
}; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Container $container A container instance. |
|
88
|
|
|
* @return CLImate |
|
89
|
|
|
*/ |
|
90
|
|
|
$container['script/climate'] = function () { |
|
91
|
|
|
$climate = new CLImate(); |
|
92
|
|
|
return $climate; |
|
93
|
|
|
}; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
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: