Test Setup Failed
Push — master ( 663cdc...37ef7f )
by Php Easy Api
03:52
created

ServiceProvider::resolveProviders()   B

Complexity

Conditions 8
Paths 12

Size

Total Lines 29
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 10
nc 12
nop 1
dl 0
loc 29
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Provider;
4
5
use Resta\Support\Utils;
6
use Resta\Foundation\ApplicationProvider;
7
8
class ServiceProvider extends  ApplicationProvider
9
{
10
    /**
11
     * all service providers
12
     *
13
     * @var array
14
     */
15
    protected $providers = [];
16
17
    /**
18
     * apply provider class
19
     *
20
     * @param $key
21
     * @param $provider
22
     * @param string $method
23
     *
24
     */
25
    private function applyProvider($key,$provider,$method='register')
26
    {
27
        // If the provider classes are a real object
28
        // we will run them.
29
        if(Utils::isNamespaceExists($provider)){
30
31
            // after determining whether the register or boot methods
32
            // we are running the provider.
33
            /** @scrutinizer ignore-call */
34
            $providerInstance = $this->app->resolve($provider);
35
36
            //we need to do method check for provider.
37
            if(method_exists($providerInstance,$method)){
38
                $providerInstance->{$method}();
39
40
                if($method=="register"){
41
                    /** @scrutinizer ignore-call */
42
                    $this->app->register('loadedProviders',$key,$provider);
43
                }
44
            }
45
        }
46
    }
47
48
    /**
49
     * assign loadedProviders core value
50
     *
51
     * @return mixed|void
52
     */
53
    private function assignerLoadedProvidersInitialCoreValue()
54
    {
55
        if(!isset($this->app['loadedProviders'])){
56
57
            // for loaded providers,
58
            // we register an empty array for the container object.
59
            /** @scrutinizer ignore-call */
60
            $this->app->register('loadedProviders',[]);
61
        }
62
    }
63
64
    /**
65
     * get all service providers
66
     *
67
     * @return array
68
     */
69
    public function getServiceProviders()
70
    {
71
        //set service providers for providers property
72
        /** @scrutinizer ignore-call */
73
        $providers = $this->app->serviceProviders();
74
75
        if(count($providers)){
76
            $this->providers = $providers;
77
        }
78
79
        return $this->providers;
80
    }
81
82
    /**
83
     * handle service providers
84
     *
85
     */
86
    public function handle()
87
    {
88
        define ('serviceprovider',true);
89
90
        //check providers and resolve
91
        $this->resolveProviders($this->getServiceProviders());
92
    }
93
94
    /**
95
     * resolve providers
96
     *
97
     * @param array $providers
98
     *
99
     */
100
    public function resolveProviders($providers=array())
101
    {
102
        // for loaded providers,
103
        // we register an empty array for the container object.
104
        $this->assignerLoadedProvidersInitialCoreValue();
105
106
        //first we are running register methods of provider classes.
107
        foreach($providers as $key=>$provider){
108
109
            // providers can only be installed once.
110
            // apply providers and register for kernel
111
            if(!isset($this->app['loadedProviders'][$key])){
112
113
                if(is_array($provider) && isset($provider['status']) && $provider['status']){
114
                    $this->applyProvider($key,$provider['class']);
115
                }
116
                else{
117
                    $this->applyProvider($key,$provider);
118
                }
119
120
            }
121
        }
122
123
        //then we are running boot methods of provider classes.
124
        foreach($providers as $key=>$provider){
125
126
            //if the providers register is already booted.
127
            if(isset($this->app['loadedProviders'][$key])){
128
                $this->applyProvider($key,$provider,'boot');
129
            }
130
        }
131
    }
132
}