Passed
Push — master ( 59ac3b...959853 )
by Biao
02:01
created

CleanerManager::addCleaner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Illuminate;
4
5
use Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface;
6
use Hhxsv5\LaravelS\Illuminate\Cleaners\ConfigCleaner;
7
use Hhxsv5\LaravelS\Illuminate\Cleaners\CookieCleaner;
8
use Hhxsv5\LaravelS\Illuminate\Cleaners\RequestCleaner;
9
10
class CleanerManager
11
{
12
    /**
13
     * @var \Illuminate\Contracts\Container\Container
14
     */
15
    protected $app;
16
17
    /**
18
     * All cleaners
19
     *
20
     * @var \Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface[]
21
     */
22
    protected $cleaners = [
23
        ConfigCleaner::class,
24
        CookieCleaner::class,
25
        RequestCleaner::class,
26
    ];
27
28
    /**
29
     * @var array
30
     */
31
    protected $providers;
32
33
    /**
34
     * @var array
35
     */
36
    protected $config = [];
37
38
    /**
39
     * CleanerManager constructor.
40
     *
41
     * @param \Illuminate\Contracts\Container\Container $app
42
     * @param array $config
43
     */
44
    public function __construct($app, array $config)
45
    {
46
        $this->app = $app;
47
        $this->config = $config;
48
49
        $cleaners = isset($this->config['cleaners']) ? $this->config['cleaners'] : [];
50
        $this->addCleaner($cleaners);
51
52
        $this->registerCleaners();
53
        $this->registerCleanProviders($config['register_providers']);
54
    }
55
56
    /**
57
     * Add cleaners.
58
     *
59
     * @param array|\Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface $cleaner
60
     */
61
    protected function addCleaner($cleaner)
62
    {
63
        $cleaners = is_array($cleaner) ? $cleaner : [$cleaner];
64
65
        $this->cleaners = array_unique(array_merge($cleaners, $this->cleaners));
66
    }
67
68
    /**
69
     * Register singleton cleaners to application container.
70
     */
71
    protected function registerCleaners()
72
    {
73
        foreach ($this->cleaners as $cleaner) {
74
            $this->app->singleton($cleaner, function () use ($cleaner) {
0 ignored issues
show
Bug introduced by
$cleaner of type Hhxsv5\LaravelS\Illumina...eaners\CleanerInterface is incompatible with the type array|string expected by parameter $abstract of Illuminate\Container\Container::singleton(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            $this->app->singleton(/** @scrutinizer ignore-type */ $cleaner, function () use ($cleaner) {
Loading history...
75
                if (!isset(class_implements($cleaner)[CleanerInterface::class])) {
76
                    throw new \InvalidArgumentException(sprintf(
77
                            '%s must implement the interface %s',
78
                            $cleaner,
0 ignored issues
show
Bug introduced by
$cleaner of type Hhxsv5\LaravelS\Illumina...eaners\CleanerInterface is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
                            /** @scrutinizer ignore-type */ $cleaner,
Loading history...
79
                            CleanerInterface::class
80
                        )
81
                    );
82
                }
83
                return new $cleaner();
84
            });
85
        }
86
    }
87
88
    /**
89
     * Register providers for cleaning.
90
     *
91
     * @param array $registerProviders
92
     */
93
    protected function registerCleanProviders($registerProviders = [])
94
    {
95
        $this->providers = $registerProviders;
96
    }
97
98
    /**
99
     * Clean Providers.
100
     *
101
     * @param \Hhxsv5\LaravelS\Illuminate\ReflectionApp $reflectionApp
102
     */
103
    protected function cleanProviders(ReflectionApp $reflectionApp)
104
    {
105
        $loadedProviders = $reflectionApp->loadedProviders();
106
107
        foreach ($this->providers as $provider) {
108
            if (class_exists($provider, false)) {
109
                if ($this->isLumen()) {
110
                    unset($loadedProviders[get_class(new $provider($this->app))]);
111
                }
112
113
                switch ($reflectionApp->registerMethodParameterCount()) {
114
                    case 1:
115
                        $this->app->register($provider);
116
                        break;
117
                    case 2:
118
                        $this->app->register($provider, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type array expected by parameter $options of Illuminate\Foundation\Application::register(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
                        $this->app->register($provider, /** @scrutinizer ignore-type */ true);
Loading history...
119
                        break;
120
                    case 3:
121
                        $this->app->register($provider, [], true);
122
                        break;
123
                    default:
124
                        throw new \RuntimeException('The number of parameters of the register method is unknown.');
125
                }
126
            }
127
        }
128
129
        if ($this->isLumen()) {
130
            $reflectionApp->setLoadedProviders($loadedProviders);
131
        }
132
    }
133
134
    /**
135
     * Determine if is lumen.
136
     *
137
     * @return bool
138
     */
139
    protected function isLumen()
140
    {
141
        return $this->config['is_lumen'];
142
    }
143
144
    /**
145
     * Clean app after request finished.
146
     *
147
     * @param \Illuminate\Contracts\Container\Container $snapshotApp
148
     * @param \Hhxsv5\LaravelS\Illuminate\ReflectionApp $reflectionApp
149
     */
150
    public function clean($snapshotApp, ReflectionApp $reflectionApp)
151
    {
152
        foreach ($this->cleaners as $cleanerCls) {
153
            $cleaner = $this->app->make($cleanerCls);
0 ignored issues
show
Bug introduced by
$cleanerCls of type Hhxsv5\LaravelS\Illumina...eaners\CleanerInterface is incompatible with the type string expected by parameter $abstract of Illuminate\Foundation\Application::make(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
            $cleaner = $this->app->make(/** @scrutinizer ignore-type */ $cleanerCls);
Loading history...
154
            $cleaner->clean($this->app, $snapshotApp);
155
        }
156
157
        $this->cleanProviders($reflectionApp);
158
    }
159
}
160